From 3b98a881e7cfefb5ee33eb9bd59d039151d79f3e Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 30 Dec 2022 10:59:21 +0100 Subject: [PATCH] Refact: use service for post and put requests --- backend/api/main.py | 55 ++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/backend/api/main.py b/backend/api/main.py index 6fdf1c8..e90faba 100644 --- a/backend/api/main.py +++ b/backend/api/main.py @@ -12,6 +12,8 @@ from backend.repository.tribe_sqlite_repository import ( TribeRepositoryError, TribeSQLiteRepository, ) +from backend.service import services +from backend.service.services import StudentDoesExist, TribeDoesNotExist, TribeExists # from sqlalchemy import create_engine # from sqlalchemy.orm import clear_mappers, sessionmaker @@ -38,13 +40,14 @@ async def post_tribe(item: TribeModel): tribe = Tribe(**item.dict()) try: - tribe_repo.add(tribe) - except TribeRepositoryError: + services.add_tribe( + name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn + ) + except TribeExists: return JSONResponse( status_code=status.HTTP_409_CONFLICT, content=f"The tribe {tribe.name} already exists", ) - conn.commit() return tribe.to_dict() @@ -71,20 +74,20 @@ async def post_student(item: StudentModel): content=f"You can't post a student with an id. It is already registrered. Use PUT to modify it.", ) - tribe_name = item.tribe_name try: - tribe = tribe_repo.get(tribe_name) - except TribeRepositoryError: + student = services.add_student( + name=item.name, + tribe=item.tribe_name, + tribe_repo=tribe_repo, + student_repo=student_repo, + conn=conn, + ) + except TribeDoesNotExist: return JSONResponse( status_code=status.HTTP_409_CONFLICT, - content=f"The tribe {tribe_name} does not exists. You can't add a student in it.", + content=f"The tribe {item.tribe_name} does not exists. You can't add a student in it.", ) - student = Student(item.name, tribe) - - student_repo.add(student) - conn.commit() - return student.to_dict() @@ -94,18 +97,30 @@ async def post_student(item: StudentModel): response_model=StudentModel, ) async def put_student(student_id, item: StudentModel): - tribe_name = item.tribe_name + if student_id != item.id: + return JSONResponse( + status_code=status.HTTP_400_BAD_REQUEST, + content=f"Url and student id are the same", + ) + try: - tribe = tribe_repo.get(tribe_name) - except TribeRepositoryError: + student = services.update_student( + id=item.id, + name=item.name, + tribe=item.tribe_name, + tribe_repo=tribe_repo, + student_repo=student_repo, + conn=conn, + ) + except TribeDoesNotExist: return JSONResponse( status_code=status.HTTP_409_CONFLICT, content=f"The tribe {tribe_name} does not exists. You can't add a student in it.", ) - - student = Student(name=item.name, tribe=tribe, id=student_id) - - student_repo.update(student) - conn.commit() + except StudentDoesExist: + return JSONResponse( + status_code=status.HTTP_409_CONFLICT, + content=f"The student {item.name} ({item.id=}) does not exists. You can't modify it.", + ) return student.to_dict()