API Фикс
This commit is contained in:
parent
47cb2fb2c7
commit
58d934884e
1 changed files with 27 additions and 12 deletions
39
API/main.py
39
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"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue