Skip to content

Commit

Permalink
boilerplate for /users/me/groups and projects
Browse files Browse the repository at this point in the history
  • Loading branch information
reyniersbram committed Mar 11, 2024
1 parent ac13307 commit eefb3f4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 16 deletions.
8 changes: 3 additions & 5 deletions backend/src/subject/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Sequence

from fastapi import Depends
from sqlalchemy.orm import Session
from src.auth.exceptions import NotAuthorized
Expand All @@ -9,7 +7,7 @@

from . import service
from .exceptions import SubjectNotFound
from .schemas import Subject
from .schemas import Subject, SubjectList


async def retrieve_subject(subject_id: int, db: Session = Depends(get_db)) -> Subject:
Expand All @@ -22,9 +20,9 @@ async def retrieve_subject(subject_id: int, db: Session = Depends(get_db)) -> Su

async def retrieve_subjects(
_: User = Depends(get_authenticated_user), db: Session = Depends(get_db)
) -> Sequence[Subject]:
) -> SubjectList:
subjects = await service.get_subjects(db)
return subjects
return SubjectList(subjects=subjects)


async def user_permission_validation(
Expand Down
6 changes: 3 additions & 3 deletions backend/src/subject/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
retrieve_subjects,
user_permission_validation,
)
from .schemas import Subject, SubjectCreate
from .schemas import Subject, SubjectCreate, SubjectList

router = APIRouter(
prefix="/api/subject",
Expand All @@ -19,8 +19,8 @@
)


@router.get("/", response_model=list[Subject])
async def get_subjects(subjects: list[Subject] = Depends(retrieve_subjects)):
@router.get("/", response_model=SubjectList)
async def get_subjects(subjects: SubjectList = Depends(retrieve_subjects)):
return subjects


Expand Down
6 changes: 6 additions & 0 deletions backend/src/subject/schemas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Sequence

from pydantic import BaseModel, Field


Expand All @@ -14,3 +16,7 @@ class Subject(SubjectBase):

class Config:
from_attributes = True


class SubjectList(BaseModel):
subjects: Sequence[Subject]
20 changes: 17 additions & 3 deletions backend/src/user/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from src.dependencies import get_db

from .exceptions import UserNotFound
from .schemas import SubjectList, User
from .schemas import User, UserGroupList, UserProjectList, UserSubjectList


async def get_authenticated_user(
Expand Down Expand Up @@ -37,8 +37,22 @@ async def user_id_validation(user_id: str, db: Session = Depends(get_db)):

async def retrieve_subjects(
user: User = Depends(get_authenticated_user), db: Session = Depends(get_db)
) -> SubjectList:
) -> UserSubjectList:
teacher_subjects, student_subjects = await subject_service.get_subjects_by_user(
db, user.uid
)
return SubjectList(as_student=student_subjects, as_teacher=teacher_subjects)
return UserSubjectList(as_student=student_subjects, as_teacher=teacher_subjects)


async def retrieve_groups(
user: User = Depends(get_authenticated_user), db: Session = Depends(get_db)
) -> UserGroupList:
# TODO: Implement this
return UserGroupList(groups=[])


async def retrieve_projects(
user: User = Depends(get_authenticated_user), db: Session = Depends(get_db)
) -> UserProjectList:
# TODO: Implement this
return UserProjectList(projects=[])
33 changes: 29 additions & 4 deletions backend/src/user/router.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
from fastapi import APIRouter, Depends

from .dependencies import get_authenticated_user, retrieve_subjects
from .schemas import SubjectList, User
from .dependencies import (
get_authenticated_user,
retrieve_groups,
retrieve_projects,
retrieve_subjects,
)
from .schemas import User, UserGroupList, UserProjectList, UserSubjectList

router = APIRouter(
prefix="/api/users", tags=["user"], responses={404: {"description": "Not Found"}}
)


@router.get("/me")
async def profile(user=Depends(get_authenticated_user)) -> User:
async def profile(user: User = Depends(get_authenticated_user)) -> User:
"""
Get information about the current user
"""
return user


@router.get("/me/subjects")
async def subjects(subjects: SubjectList = Depends(retrieve_subjects)) -> SubjectList:
async def subjects(
subjects: UserSubjectList = Depends(retrieve_subjects),
) -> UserSubjectList:
"""
Get the subjects of the current user
"""
return subjects


@router.get("/me/groups")
async def groups(groups: UserGroupList = Depends(retrieve_groups)) -> UserGroupList:
"""
Get the groups of the current user
"""
return groups


@router.get("/me/projects")
async def projects(
projects: UserProjectList = Depends(retrieve_projects),
) -> UserProjectList:
"""
Get the projects of the current user
"""
return projects
16 changes: 15 additions & 1 deletion backend/src/user/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@ class Config:
from_attributes = True


class SubjectList(BaseModel):
class UserSubjectList(BaseModel):
as_teacher: Sequence[Subject]
as_student: Sequence[Subject]

class Config:
from_attributes = True


class UserProjectList(BaseModel):
projects: Sequence # TODO: Add project schema

class Config:
from_attributes = True


class UserGroupList(BaseModel):
groups: Sequence # TODO: Add group schema

class Config:
from_attributes = True

0 comments on commit eefb3f4

Please sign in to comment.