-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (40 loc) · 1.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from database import *
from fastapi import FastAPI, Body, status, Depends
from fastapi.responses import JSONResponse, FileResponse
app = FastAPI()
# определяем зависимость
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get("/")
def main():
return FileResponse("Public/index.html")
@app.get("/api/users")
def get_users(db: Session = Depends(get_db)):
return db.query(User).all()
@app.get("/api/users/{id}")
def get_user(id: int, db: Session = Depends(get_db)):
return db.query(User).filter(User.id == id).first()
@app.post("/api/users")
def create_user(data = Body(), db: Session = Depends(get_db)):
user = User(name = data['name'], age=data['age'])
db.add(user)
db.commit()
db.refresh(user)
return user
@app.delete("/api/users/{id}")
def delete_user(id: int, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == id).first()
db.delete(user)
db.commit()
return user
@app.put("/api/users")
def edit_user(data = Body(), db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == data['id']).first()
user.name = data['name']
user.age = data['age']
db.commit()
# return user