diff --git a/__pycache__/main.cpython-39.pyc b/__pycache__/main.cpython-39.pyc index 9564195..84549a3 100644 Binary files a/__pycache__/main.cpython-39.pyc and b/__pycache__/main.cpython-39.pyc differ diff --git a/deps/auth/__pycache__/auth.cpython-39.pyc b/deps/auth/__pycache__/auth.cpython-39.pyc index 98a5de0..6b30b23 100644 Binary files a/deps/auth/__pycache__/auth.cpython-39.pyc and b/deps/auth/__pycache__/auth.cpython-39.pyc differ diff --git a/deps/auth/auth.py b/deps/auth/auth.py index 51d3ec0..2bb5c54 100644 --- a/deps/auth/auth.py +++ b/deps/auth/auth.py @@ -4,6 +4,7 @@ from fastapi import HTTPException, Depends from pydantic import EmailStr from .tokens import decode_jwt_token +from app.db import db oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login", auto_error=False) password_context = CryptContext(schemes=["bcrypt"], deprecated="auto") @@ -60,5 +61,5 @@ def get_current_user(token: str = Depends(oauth2_scheme)): raise credentials_exception # check redis by user_id - user = {"email": "some email in redis"} + user = db["users"].find_one({"_id": user_id}) return user diff --git a/main.py b/main.py index fb8e62f..3cbce92 100644 --- a/main.py +++ b/main.py @@ -8,13 +8,13 @@ app = FastAPI() -# app.add_middleware( -# CORSMiddleware, -# allow_origins=["*"], -# allow_credentials=True, -# allow_methods=["*"], -# allow_headers=["*"], -# ) +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) @app.get("/", status_code=200) diff --git a/sessions/__pycache__/models.cpython-39.pyc b/sessions/__pycache__/models.cpython-39.pyc index d14881c..81ff673 100644 Binary files a/sessions/__pycache__/models.cpython-39.pyc and b/sessions/__pycache__/models.cpython-39.pyc differ diff --git a/sessions/__pycache__/routes.cpython-39.pyc b/sessions/__pycache__/routes.cpython-39.pyc index ed542e5..2142968 100644 Binary files a/sessions/__pycache__/routes.cpython-39.pyc and b/sessions/__pycache__/routes.cpython-39.pyc differ diff --git a/sessions/models.py b/sessions/models.py index 04f9c1b..e6c4ab1 100644 --- a/sessions/models.py +++ b/sessions/models.py @@ -1,6 +1,23 @@ from pydantic import BaseModel from enum import Enum -from datetime import datetime, timezone +from datetime import datetime +from datetime import date + + +class SessionType(str, Enum): + start_now = "start_now" + start_later = "start_later" + recurring = "recurring" + + +class RecurringDays(str, Enum): + monday = "monday" + tuesday = "tuesday" + wednesday = "wednesday" + thursday = "thursday" + friday = "friday" + saturday = "saturday" + sunday = "sunday" class WeekDays(BaseModel): @@ -14,16 +31,10 @@ class WeekDays(BaseModel): class Session(BaseModel): - name: str - block_lists: list - - -class ScheduledSession(Session): - start: datetime - end: datetime - - -class RecurringSession(Session): - start: datetime - end: datetime - days: WeekDays + device_id: str = None + type: SessionType + start_time: datetime = None + end_time: datetime = None + block_lists: list[str] + start_date: date + recurring_days: list[RecurringDays] = None diff --git a/sessions/routes.py b/sessions/routes.py index a53ec6d..f39a27a 100644 --- a/sessions/routes.py +++ b/sessions/routes.py @@ -1,5 +1,8 @@ -from fastapi import APIRouter -from .models import ScheduledSession, RecurringSession, Session +from fastapi import APIRouter, Depends, HTTPException +from .models import Session +from deps.auth.auth import get_current_user +from uuid import uuid4 +from app.db import db # nest sessions in the database like: @@ -14,20 +17,41 @@ @router.post("", status_code=201, description="Create session") -async def create_session(session: Session): - pass +async def create_session(session: Session, user=Depends): + id = str(uuid4()) + session_dict = session.model_dump(exclude_none=True) + session_dict["_id"] = id + session_dict["user"] = user["_id"] + db["sessions"].insert_one(session_dict) + session_dict["id"] = session_dict.pop("_id") + return session_dict -@router.post("/schedule", status_code=201, description="Create session") -async def schedule_session(session: ScheduledSession): - pass +@router.get("/", status_code=200) +async def get_sessions(user=Depends(get_current_user)): + sessions = db["sessions"].find({"user": user["_id"]}) + for session in sessions: + session["id"] = session.pop("_id") + return sessions -@router.post("/recurring", status_code=201, description="Create session") -async def schedule_recurring_session(session: RecurringSession): - pass +@router.get("/{id}", status_code=200) +async def get_single_session(id: str, user=Depends(get_current_user)): + session = db["sessions"].find_one({"_id": id}) + if session: + session["id"] = session.pop("_id") + return session + raise HTTPException(status_code=400, detail="Session not found.") -@router.get("/", status_code=200) -async def get_sessions(): +@router.patch("/{id}", status_code=200, dependencies=[Depends(get_current_user)]) +async def update_session(id: str): pass + + +@router.delete("/{id}", status_code=200, dependencies=[Depends(get_current_user)]) +async def delete_session(id: str): + session = db["sessions"].find_one_and_delete({"_id": id}) + if session: + return f"{session['name']} has been deleted successfully" + raise HTTPException(status_code=400, detail="Session with this ID does not exist.")