-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (83 loc) · 3.48 KB
/
app.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
86
87
88
89
90
91
92
93
94
95
96
# imports
from typing import Annotated, Any, List
from fastapi import Depends, FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from starlette import status
from database import engine, SessionLocal
from sqlalchemy.orm import Session
import models
import auth
from auth import get_current_user
from models import User, Todo, Studies
from base_models import CreateTodoRequest
# FastAPI app
app: FastAPI = FastAPI()
app.include_router(auth.router)
app.add_middleware(CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
async def on_startup() -> None:
models.Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
SessionDep = Annotated[Session, Depends(get_db)]
@app.get("/api/")
async def index() -> dict[str, str]:
return {"message": "This is a Response from FastAPI from Vercel"}
@app.get("/api/info")
async def info() -> dict[str, str]:
return {
"app_name": "Pomodoro Planner",
"app_version": "1.0.0",
"app_description": "Pomodoro Planner API for Pomodoro Planner App",
"app_author": "Dasun Nethsara",
"app_author_website": "http://techsaralk.epizy.com/",
"app_author_github": "https://github.com/DasunNethsara-04/"
}
# users related endpoints
@app.get("/api/user", status_code=status.HTTP_200_OK)
async def get_user(user: Annotated[dict, Depends(get_current_user)], session: SessionDep) -> dict[str, dict[str, str]]:
if user is None:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials")
return {"User": user}
@app.get("/api/users")
async def get_users(user: Annotated[dict, Depends(get_current_user)], session: SessionDep) -> list[str, User]:
users: list[User] = session.query(User).all()
return {"User": user}
# todos related endpoints
@app.get("/api/todos")
async def get_todos(session: SessionDep, user: dict = Depends(get_current_user)) -> dict[str, list[dict[str, Any]]]:
todos: List[Todo] = session.query(Todo).filter(Todo.user_id == user["id"]).all()
return {"todos": [todo.to_dict() for todo in todos]}
@app.get("/api/todo/{todo_id}")
async def get_todo_by_id(todo_id: str|int, session: SessionDep, user: Annotated[dict, Depends(get_current_user)]) -> dict[str, dict[str, Any]]:
todo: Todo = session.query(Todo).filter(Todo.id == todo_id).first()
if todo is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Todo not Found!")
return {"todo": todo.to_dict()}
@app.post("/api/todo/")
async def create_todo(todo: CreateTodoRequest, session: SessionDep, user: Annotated[dict, Depends(get_current_user)])-> dict[str, str|bool]:
new_todo: Todo = Todo(
title=todo.title,
description=todo.description,
completed=todo.completed,
dueDate = todo.dueDate,
created_at = todo.created_at,
user_id=user["id"]
)
session.add(new_todo)
session.commit()
return {"success": True, "message": "New Todo Created Successfully!"}
# studies related endpoints
@app.get("/api/studies")
async def get_studies(user: Annotated[dict, Depends(get_current_user)], session: SessionDep) -> dict:
studies: list[Studies] = session.query(Studies).filter(Studies.user_id == user["id"]).all()
return {"studies": studies}