Skip to content

Commit

Permalink
sessions endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshuamjv2 committed Sep 17, 2024
1 parent 6723523 commit 645b77b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 34 deletions.
Binary file modified __pycache__/main.cpython-39.pyc
Binary file not shown.
Binary file modified deps/auth/__pycache__/auth.cpython-39.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion deps/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file modified sessions/__pycache__/models.cpython-39.pyc
Binary file not shown.
Binary file modified sessions/__pycache__/routes.cpython-39.pyc
Binary file not shown.
39 changes: 25 additions & 14 deletions sessions/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
48 changes: 36 additions & 12 deletions sessions/routes.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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.")

0 comments on commit 645b77b

Please sign in to comment.