first commit
This commit is contained in:
66
server/backend/endpoints.py
Normal file
66
server/backend/endpoints.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from . import pydentic
|
||||
from server.database import db
|
||||
import asyncio
|
||||
|
||||
api = FastAPI()
|
||||
|
||||
api.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # "*" — разрешить всем; можно указать список конкретных доменов
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"], # GET, POST, PUT, DELETE и т.д.
|
||||
allow_headers=["*"], # Разрешить любые заголовки
|
||||
)
|
||||
|
||||
@api.get("/", response_model=pydentic.IdofPersons)
|
||||
async def get_all_rows():
|
||||
for row in await db.get_all_rows():
|
||||
if row:
|
||||
return row
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
@api.get("/get_user/{id}", response_model=pydentic.IdofPersons)
|
||||
async def get_user(id:int):
|
||||
user = await db.GetUser(id)
|
||||
if user:
|
||||
return user
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
@api.post("/user_create", response_model=pydentic.IdofPersons)
|
||||
async def create_user(row:pydentic.CreateUser):
|
||||
new_user_id = max(item.id for item in await db.get_all_rows())
|
||||
new_row = pydentic.IdofPersons(id = new_user_id, email=row.email, description=row.description, activated = row.activated, password = row.password)
|
||||
await db.CreateUser(new_row)
|
||||
return new_row
|
||||
@api.delete("/user_delete/{id}", response_model=pydentic.IdofPersons)
|
||||
async def delete_user(id: int):
|
||||
user = await db.GetUser(id)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
await db.DeleteUser(id)
|
||||
return user
|
||||
@api.put("/user_update/{id}", response_model=pydentic.IdofPersons)
|
||||
async def update_user(id: int, updated_row: pydentic.UserUpdate):
|
||||
user = await db.GetUser(id)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
changed = False
|
||||
if updated_row.email is not None and updated_row.email != user.email:
|
||||
user.email = updated_row.email
|
||||
changed = True
|
||||
if updated_row.description is not None and updated_row.description != user.description:
|
||||
user.description = updated_row.description
|
||||
changed = True
|
||||
if updated_row.activated is not None and updated_row.activated != user.activated:
|
||||
user.activated = updated_row.activated
|
||||
changed = True
|
||||
if updated_row.password is not None and updated_row.password != user.password:
|
||||
user.password = updated_row.password
|
||||
changed = True
|
||||
if changed:
|
||||
await db.UpdateUser(user)
|
||||
else:
|
||||
pass
|
||||
return user
|
||||
Reference in New Issue
Block a user