From 4a1644483539f3c0cc5f45e5b984e71e4e840a60 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Tue, 27 Dec 2022 11:57:50 +0100 Subject: [PATCH] Feat: add post student --- backend/api/main.py | 52 ++++++++++++++++++++++++++++++---------- backend/api/model.py | 10 +++++++- backend/model/student.py | 2 +- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/backend/api/main.py b/backend/api/main.py index 8dc7a79..21b9623 100644 --- a/backend/api/main.py +++ b/backend/api/main.py @@ -1,27 +1,40 @@ +import sqlite3 + from fastapi import FastAPI, status -from sqlalchemy import create_engine -from sqlalchemy.orm import clear_mappers, sessionmaker -import backend.adapters.orm as orm -from backend.api.model import TribeModel +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.tribe_sqlalchemy_repository import TribeSQLAlchemyRepository +from backend.repository.student_sqlite_repository import StudentSQLiteRepository +from backend.repository.tribe_sqlite_repository import TribeSQLiteRepository -orm.start_mappers() -engine = create_engine("sqlite:///:memory:") -orm.metadata.create_all(engine) -session = sessionmaker(bind=engine)() -tribe_repo = TribeSQLAlchemyRepository(session) +# 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(":memory:") +create_db(conn) + +tribe_repo = TribeSQLiteRepository(conn) +student_repo = StudentSQLiteRepository(conn) app = FastAPI() -@app.put("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel) -async def put_tribe(item: TribeModel): +@app.post("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel) +async def post_tribe(item: TribeModel): tribe = Tribe(**item.dict()) tribe_repo.add(tribe) - session.commit() + conn.commit() return tribe.to_dict() @@ -38,3 +51,16 @@ async def get_tribe(name: str): tribe = tribe_repo.get(name) return tribe.to_dict() + + +@app.post("/students", status_code=status.HTTP_201_CREATED, response_model=StudentModel) +async def post_student(item: StudentModel): + tribe_name = item.tribe_name + tribe = tribe_repo.get(tribe_name) + + student = Student(item.name, tribe) + + student_repo.add(student) + conn.commit() + + return student.to_dict() diff --git a/backend/api/model.py b/backend/api/model.py index a20a4e3..0918d99 100644 --- a/backend/api/model.py +++ b/backend/api/model.py @@ -1,8 +1,16 @@ +from typing import Optional + from pydantic import BaseModel +class StudentModel(BaseModel): + name: str + tribe_name: str + id: Optional[str] + + class TribeModel(BaseModel): name: str level: str - students: list | None = [] + students: list[StudentModel] | None = [] assessments: list | None = [] diff --git a/backend/model/student.py b/backend/model/student.py index b66e5e3..954bd8d 100644 --- a/backend/model/student.py +++ b/backend/model/student.py @@ -29,5 +29,5 @@ class Student: def to_tuple(self) -> tuple: return (self.id, self.name, self.tribe.name) - def to_dict(self) -> dict: + def to_dict(self, full_tribe=False) -> dict: return {"id": self.id, "name": self.name, "tribe_name": self.tribe.name}