Skip to content

Commit

Permalink
join a school
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTraveylan committed Aug 24, 2024
1 parent 3ec4a5e commit 18ad591
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions app/api/school/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from fastapi import APIRouter, Depends

from app.api.list_link.models import LIST_LINK_SERVICE, ListLink
from app.api.list_link.models import LIST_LINK_SERVICE, ListLink, SchoolRelation
from app.api.school.models import SCHOOL_SERVICE, School
from app.api.school.schemas import SchoolSchemaIn
from app.api.school.schemas import SchoolSchemaIn, SchoolSchemaOut
from app.auth.models import User
from app.auth.token import get_current_user
from app.database.unit_of_work import unit_api
from app.exceptions import CannotCreateStillExistsException
from app.exceptions import CannotCreateStillExistsException, RessourceNotFoundException

school_router = APIRouter(
tags=["School"],
Expand All @@ -29,7 +29,7 @@ def get_all_schools() -> list[School]:
def create_school(
current_user: Annotated[User, Depends(get_current_user)],
payload: SchoolSchemaIn,
):
) -> SchoolSchemaOut:
"""
Create a school
Expand All @@ -40,7 +40,9 @@ def create_school(
with unit_api("Trying to create school") as session:
user_link = LIST_LINK_SERVICE.get_or_none(session, user_id=current_user.id)
if user_link is not None:
raise CannotCreateStillExistsException("User has no link")
raise CannotCreateStillExistsException(
"User still has a link, delete it first"
)

school = School(
school_name=payload.school_name,
Expand All @@ -63,4 +65,37 @@ def create_school(

session.expunge(created_school)

return created_school
return created_school.to_decrypted()


@school_router.get("/join/{school_id}")
def join_school(
current_user: Annotated[User, Depends(get_current_user)],
school_id: int,
) -> SchoolSchemaOut:
"""
Join a school
User must be logger in to create a school.
He must've a link object
"""
with unit_api("Trying to join school") as session:
user_link = LIST_LINK_SERVICE.get_or_none(session, user_id=current_user.id)
if user_link is None:
raise CannotCreateStillExistsException("User has no link, delete it first")

school = SCHOOL_SERVICE.get_or_none(session, id=school_id)
if school is None:
raise RessourceNotFoundException("School not found")

list_link = ListLink(
user_id=current_user.id,
school_id=school.id,
school_relation=SchoolRelation.PARENT,
)

LIST_LINK_SERVICE.create(session, list_link)

session.expunge(school)

return school.to_decrypted()

0 comments on commit 18ad591

Please sign in to comment.