195 lines
5.6 KiB
Python
195 lines
5.6 KiB
Python
import sqlite3
|
|
|
|
from fastapi import FastAPI, status
|
|
from fastapi.responses import JSONResponse, RedirectResponse, Response
|
|
|
|
from backend.adapters.sqlite import create_db
|
|
from backend.api.model import StudentModel, TribeModel
|
|
from backend.model.student import Student
|
|
from backend.model.tribe import Tribe
|
|
from backend.repository.student_sqlite_repository import StudentSQLiteRepository
|
|
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
|
|
# import backend.adapters.orm as orm
|
|
# from backend.repository.tribe_sqlalchemy_repository import TribeSQLAlchemyRepository
|
|
|
|
# orm.start_mappers()
|
|
# engine = create_engine("sqlite:///:memory:")
|
|
# orm.metadata.create_all(engine)
|
|
# session = sessionmaker(bind=engine)()
|
|
# tribe_repo = TribeSQLAlchemyRepository(session)
|
|
|
|
conn = sqlite3.connect("sqlite.db")
|
|
create_db(conn)
|
|
|
|
tribe_repo = TribeSQLiteRepository(conn)
|
|
student_repo = StudentSQLiteRepository(conn)
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.post("/tribes", response_class=RedirectResponse, status_code=status.HTTP_302_FOUND)
|
|
async def post_tribe(item: TribeModel):
|
|
try:
|
|
tribe = 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 {item.name} already exists",
|
|
)
|
|
|
|
return f"/tribes/{tribe.name}"
|
|
|
|
|
|
@app.put(
|
|
"/tribes/{name}", response_class=RedirectResponse, status_code=status.HTTP_302_FOUND
|
|
)
|
|
async def put_tribe(name: str, item: TribeModel):
|
|
try:
|
|
tribe = services.update_tribe(
|
|
name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn
|
|
)
|
|
except TribeDoesNotExist:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
content=f"The tribe {name} does not exists",
|
|
)
|
|
|
|
return f"/tribes/{tribe.name}"
|
|
|
|
|
|
@app.delete("/tribes/{name}")
|
|
async def delete_tribe(name: str):
|
|
try:
|
|
services.delete_tribe(name=name, tribe_repo=tribe_repo, conn=conn)
|
|
except TribeDoesNotExist:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
content=f"The tribe {name} does not exists",
|
|
)
|
|
|
|
return Response(
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
)
|
|
|
|
|
|
@app.get("/tribes", response_model=list[TribeModel])
|
|
async def list_tribes():
|
|
tribes = tribe_repo.list()
|
|
|
|
return [t.to_dict() for t in tribes]
|
|
|
|
|
|
@app.get("/tribes/{name}", response_model=TribeModel)
|
|
async def get_tribe(name: str):
|
|
tribe = tribe_repo.get(name)
|
|
|
|
return tribe.to_dict()
|
|
|
|
|
|
@app.post(
|
|
"/students", response_class=RedirectResponse, status_code=status.HTTP_302_FOUND
|
|
)
|
|
async def post_student(item: StudentModel):
|
|
if item.id is not None:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
content=f"You can't post a student with an id. It is already registrered. Use PUT to modify it.",
|
|
)
|
|
|
|
try:
|
|
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 {item.tribe_name} does not exists. You can't add a student in it.",
|
|
)
|
|
|
|
return f"/students/{student.id}"
|
|
|
|
|
|
@app.get("/students/{id}", status_code=status.HTTP_200_OK, response_model=StudentModel)
|
|
async def get_student(id: str):
|
|
tribes = tribe_repo.list()
|
|
student = student_repo.get(id, tribes)
|
|
return student.to_dict()
|
|
|
|
|
|
@app.get("/students", status_code=status.HTTP_200_OK, response_model=list[StudentModel])
|
|
async def list_students():
|
|
tribes = tribe_repo.list()
|
|
students = student_repo.list(tribes)
|
|
|
|
return [t.to_dict() for t in students]
|
|
|
|
|
|
@app.put(
|
|
"/students/{student_id}",
|
|
response_class=RedirectResponse,
|
|
status_code=status.HTTP_302_FOUND,
|
|
)
|
|
async def put_student(student_id, item: StudentModel):
|
|
if student_id != item.id:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
content=f"Url and student id are the same",
|
|
)
|
|
|
|
try:
|
|
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.",
|
|
)
|
|
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 f"/students/{student.id}"
|
|
|
|
|
|
@app.delete(
|
|
"/students/{student_id}",
|
|
)
|
|
async def delete_student(student_id):
|
|
try:
|
|
student = services.delete_student(
|
|
id=student_id,
|
|
student_repo=student_repo,
|
|
conn=conn,
|
|
)
|
|
except StudentDoesExist:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
content=f"The student ({student_id=}) does not exists. You can't delete it.",
|
|
)
|
|
|
|
return Response(
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
)
|