Feat: add post student
This commit is contained in:
parent
9ec183c3a5
commit
4a16444835
@ -1,27 +1,40 @@
|
|||||||
|
import sqlite3
|
||||||
|
|
||||||
from fastapi import FastAPI, status
|
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.adapters.sqlite import create_db
|
||||||
from backend.api.model import TribeModel
|
from backend.api.model import StudentModel, TribeModel
|
||||||
|
from backend.model.student import Student
|
||||||
from backend.model.tribe import Tribe
|
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()
|
# from sqlalchemy import create_engine
|
||||||
engine = create_engine("sqlite:///:memory:")
|
# from sqlalchemy.orm import clear_mappers, sessionmaker
|
||||||
orm.metadata.create_all(engine)
|
# import backend.adapters.orm as orm
|
||||||
session = sessionmaker(bind=engine)()
|
# from backend.repository.tribe_sqlalchemy_repository import TribeSQLAlchemyRepository
|
||||||
tribe_repo = TribeSQLAlchemyRepository(session)
|
|
||||||
|
# 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 = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
@app.put("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
|
@app.post("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
|
||||||
async def put_tribe(item: TribeModel):
|
async def post_tribe(item: TribeModel):
|
||||||
tribe = Tribe(**item.dict())
|
tribe = Tribe(**item.dict())
|
||||||
|
|
||||||
tribe_repo.add(tribe)
|
tribe_repo.add(tribe)
|
||||||
session.commit()
|
conn.commit()
|
||||||
|
|
||||||
return tribe.to_dict()
|
return tribe.to_dict()
|
||||||
|
|
||||||
@ -38,3 +51,16 @@ async def get_tribe(name: str):
|
|||||||
tribe = tribe_repo.get(name)
|
tribe = tribe_repo.get(name)
|
||||||
|
|
||||||
return tribe.to_dict()
|
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()
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class StudentModel(BaseModel):
|
||||||
|
name: str
|
||||||
|
tribe_name: str
|
||||||
|
id: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
class TribeModel(BaseModel):
|
class TribeModel(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
level: str
|
level: str
|
||||||
students: list | None = []
|
students: list[StudentModel] | None = []
|
||||||
assessments: list | None = []
|
assessments: list | None = []
|
||||||
|
@ -29,5 +29,5 @@ class Student:
|
|||||||
def to_tuple(self) -> tuple:
|
def to_tuple(self) -> tuple:
|
||||||
return (self.id, self.name, self.tribe.name)
|
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}
|
return {"id": self.id, "name": self.name, "tribe_name": self.tribe.name}
|
||||||
|
Loading…
Reference in New Issue
Block a user