From 58d934884ebc5e40007977b133a0e30acf45d3d3 Mon Sep 17 00:00:00 2001 From: Lev Date: Thu, 27 Nov 2025 21:15:10 +0300 Subject: [PATCH] =?UTF-8?q?API=20=D0=A4=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API/main.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/API/main.py b/API/main.py index a0653d9..24bb15e 100644 --- a/API/main.py +++ b/API/main.py @@ -1,21 +1,36 @@ -from fastapi import FastAPI, HTTPException +from fastapi import FastAPI, HTTPException, status +from pydantic import BaseModel, EmailStr import utils app = FastAPI() db = utils.DataBase("") -@app.post("/registration") -def registration(email: str, password: str) : - answer = db.registration(email, password) - if answer["code"] == 1 : - HTTPException(400, "Email is busy.") - else : - return answer["data"] +class RegisterBody(BaseModel) : + email : EmailStr + password : str + +class LoginBody(BaseModel) : + email : EmailStr + password : str + + +@app.post("/registration", status_code=status.HTTP_201_CREATED) +def registration(body: RegisterBody) : + result = db.registration(body.email, body.password) + if result["code"] == 1: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Email is busy." + ) + return result["data"] -@app.post("/login") -def login(email: str, password: str) : - answer = db.login(email, password) +@app.post("/login", status_code=status.HTTP_200_OK) +def login(body: LoginBody) : + answer = db.login(body.email, body.password) if answer["code"] == 1 : - HTTPException(400, "Incorrect email or password.") + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Incorrect email or password." + ) else : return answer["data"] \ No newline at end of file