-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (73 loc) · 2.93 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import List
import fastapi as _fastapi
import fastapi.security as _security
import sqlalchemy.orm as _orm
import services as _services, schemas as _schemas
from fastapi.middleware.cors import CORSMiddleware
app = _fastapi.FastAPI()
# add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/api/users")
async def create_user(
user: _schemas.UserCreate, db: _orm.Session = _fastapi.Depends(_services.get_db)
):
db_user = await _services.get_user_by_email(user.email, db)
if db_user:
raise _fastapi.HTTPException(status_code=400, detail="Email already in use")
user = await _services.create_user(user, db)
return await _services.create_token(user)
@app.post("/api/token")
async def generate_token(
form_data: _security.OAuth2PasswordRequestForm = _fastapi.Depends(),
db: _orm.Session = _fastapi.Depends(_services.get_db),
):
user = await _services.authenticate_user(form_data.username, form_data.password, db)
if not user:
raise _fastapi.HTTPException(status_code=401, detail="Invalid Credentials")
return await _services.create_token(user)
@app.get("/api/users/me", response_model=_schemas.User)
async def get_user(user: _schemas.User = _fastapi.Depends(_services.get_current_user)):
return user
@app.post("/api/tasks", response_model=_schemas.Task)
async def create_task(
task: _schemas.TaskCreate,
db: _orm.Session = _fastapi.Depends(_services.get_db),
current_user: _schemas.User = _fastapi.Depends(_services.get_current_user),
):
return await _services.create_task(task, current_user, db)
@app.get("/api/tasks/{task_id}", response_model=_schemas.Task)
async def get_task(
task_id: int,
db: _orm.Session = _fastapi.Depends(_services.get_db),
current_user: _schemas.User = _fastapi.Depends(_services.get_current_user),
):
return await _services.get_task(task_id, db)
@app.get("/api/tasks", response_model=List[_schemas.Task])
async def get_tasks(
db: _orm.Session = _fastapi.Depends(_services.get_db),
current_user: _schemas.User = _fastapi.Depends(_services.get_current_user),
):
return await _services.get_tasks(current_user.id, db)
@app.put("/api/tasks/{task_id}", response_model=_schemas.Task)
async def update_task(
task_id: int,
task: _schemas.TaskCreate,
db: _orm.Session = _fastapi.Depends(_services.get_db),
current_user: _schemas.User = _fastapi.Depends(_services.get_current_user),
):
return await _services.update_task(task_id, task, current_user.id, db)
@app.delete("/api/tasks/{task_id}", response_model=_schemas.Task)
async def delete_task(
task_id: int,
db: _orm.Session = _fastapi.Depends(_services.get_db),
current_user: _schemas.User = _fastapi.Depends(_services.get_current_user),
):
return await _services.delete_task(task_id, current_user.id, db)
@app.get("/api")
def root():
return {"message": "SOUTA INTRANET"}