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
|
import utils
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
db = utils.DataBase("")
|
db = utils.DataBase("")
|
||||||
|
|
||||||
@app.post("/registration")
|
class RegisterBody(BaseModel) :
|
||||||
def registration(email: str, password: str) :
|
email : EmailStr
|
||||||
answer = db.registration(email, password)
|
password : str
|
||||||
if answer["code"] == 1 :
|
|
||||||
HTTPException(400, "Email is busy.")
|
class LoginBody(BaseModel) :
|
||||||
else :
|
email : EmailStr
|
||||||
return answer["data"]
|
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")
|
@app.post("/login", status_code=status.HTTP_200_OK)
|
||||||
def login(email: str, password: str) :
|
def login(body: LoginBody) :
|
||||||
answer = db.login(email, password)
|
answer = db.login(body.email, body.password)
|
||||||
if answer["code"] == 1 :
|
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 :
|
else :
|
||||||
return answer["data"]
|
return answer["data"]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue