Refact: use service for post and put requests
This commit is contained in:
parent
356db507eb
commit
3b98a881e7
@ -12,6 +12,8 @@ from backend.repository.tribe_sqlite_repository import (
|
|||||||
TribeRepositoryError,
|
TribeRepositoryError,
|
||||||
TribeSQLiteRepository,
|
TribeSQLiteRepository,
|
||||||
)
|
)
|
||||||
|
from backend.service import services
|
||||||
|
from backend.service.services import StudentDoesExist, TribeDoesNotExist, TribeExists
|
||||||
|
|
||||||
# from sqlalchemy import create_engine
|
# from sqlalchemy import create_engine
|
||||||
# from sqlalchemy.orm import clear_mappers, sessionmaker
|
# from sqlalchemy.orm import clear_mappers, sessionmaker
|
||||||
@ -38,13 +40,14 @@ async def post_tribe(item: TribeModel):
|
|||||||
tribe = Tribe(**item.dict())
|
tribe = Tribe(**item.dict())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tribe_repo.add(tribe)
|
services.add_tribe(
|
||||||
except TribeRepositoryError:
|
name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn
|
||||||
|
)
|
||||||
|
except TribeExists:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
content=f"The tribe {tribe.name} already exists",
|
content=f"The tribe {tribe.name} already exists",
|
||||||
)
|
)
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
return tribe.to_dict()
|
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.",
|
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:
|
try:
|
||||||
tribe = tribe_repo.get(tribe_name)
|
student = services.add_student(
|
||||||
except TribeRepositoryError:
|
name=item.name,
|
||||||
|
tribe=item.tribe_name,
|
||||||
|
tribe_repo=tribe_repo,
|
||||||
|
student_repo=student_repo,
|
||||||
|
conn=conn,
|
||||||
|
)
|
||||||
|
except TribeDoesNotExist:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
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()
|
return student.to_dict()
|
||||||
|
|
||||||
|
|
||||||
@ -94,18 +97,30 @@ async def post_student(item: StudentModel):
|
|||||||
response_model=StudentModel,
|
response_model=StudentModel,
|
||||||
)
|
)
|
||||||
async def put_student(student_id, item: 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:
|
try:
|
||||||
tribe = tribe_repo.get(tribe_name)
|
student = services.update_student(
|
||||||
except TribeRepositoryError:
|
id=item.id,
|
||||||
|
name=item.name,
|
||||||
|
tribe=item.tribe_name,
|
||||||
|
tribe_repo=tribe_repo,
|
||||||
|
student_repo=student_repo,
|
||||||
|
conn=conn,
|
||||||
|
)
|
||||||
|
except TribeDoesNotExist:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
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 {tribe_name} does not exists. You can't add a student in it.",
|
||||||
)
|
)
|
||||||
|
except StudentDoesExist:
|
||||||
student = Student(name=item.name, tribe=tribe, id=student_id)
|
return JSONResponse(
|
||||||
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
student_repo.update(student)
|
content=f"The student {item.name} ({item.id=}) does not exists. You can't modify it.",
|
||||||
conn.commit()
|
)
|
||||||
|
|
||||||
return student.to_dict()
|
return student.to_dict()
|
||||||
|
Loading…
Reference in New Issue
Block a user