Skip to content

Commit

Permalink
feat: add /events/:id/notes route
Browse files Browse the repository at this point in the history
  • Loading branch information
seelengxd committed Sep 23, 2024
1 parent 681535b commit 46be709
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
30 changes: 30 additions & 0 deletions backend/src/events/dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from http import HTTPStatus
from fastapi import Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import selectinload
from src.common.dependencies import get_session
from src.events.models import Analysis, Event, GPQuestion


def retrieve_event(
id: int,
session=Depends(get_session),
):
event = session.scalar(
select(Event)
.where(Event.id == id)
.options(
selectinload(
Event.gp_questions,
GPQuestion.categories,
),
selectinload(
Event.categories,
),
selectinload(Event.analysises, Analysis.category),
)
)
if not event:
raise HTTPException(HTTPStatus.NOT_FOUND)

return event
44 changes: 22 additions & 22 deletions backend/src/events/router.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from datetime import datetime
from http import HTTPStatus
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi import APIRouter, Depends, Query
from sqlalchemy import select
from sqlalchemy.orm import selectinload
from src.auth.dependencies import get_current_user
from src.auth.models import User
from src.events.models import Analysis, Article, Category, Event, GPQuestion
from src.events.dependencies import retrieve_event
from src.events.models import Article, Category, Event
from src.common.dependencies import get_session
from src.events.schemas import EventDTO, EventIndexResponse
from src.notes.models import Note, NoteType
from src.notes.schemas import NoteDTO


router = APIRouter(prefix="/events", tags=["events"])
Expand Down Expand Up @@ -55,25 +57,23 @@ def get_events(

@router.get("/:id")
def get_event(
id: int,
_: Annotated[User, Depends(get_current_user)],
session=Depends(get_session),
event=Depends(retrieve_event),
) -> EventDTO:
event = session.scalar(
select(Event)
.where(Event.id == id)
.options(
selectinload(
Event.gp_questions,
GPQuestion.categories,
),
selectinload(
Event.categories,
),
selectinload(Event.analysises, Analysis.category),
)
)
if not event:
raise HTTPException(HTTPStatus.NOT_FOUND)

return event


@router.get("/:id/notes")
def get_event_notes(
id: int,
user: Annotated[User, Depends(get_current_user)],
_=Depends(retrieve_event),
session=Depends(get_session),
) -> list[NoteDTO]:
notes = session.scalars(
select(Note)
.where(Note.parent_id == id)
.where(Note.parent_type == NoteType.EVENT)
.where(Note.user_id == user.id)
)
return notes

0 comments on commit 46be709

Please sign in to comment.