Skip to content

Commit

Permalink
criacao de repositorie e endpoint para criar turma. #32
Browse files Browse the repository at this point in the history
Co-Authored-by: JoaoP-Coelho <[email protected]>
  • Loading branch information
LamequeFernandes and JoaoP-Coelho committed Sep 1, 2022
1 parent cd01a24 commit 41e244d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/api/endpoints/turma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from fastapi import APIRouter, status, Depends

from typing import List

from src.schemas.turma import TurmaSchema
from src.repositories.turma import TurmaModel
from src.repositories.turma import TurmaRepository

from sqlalchemy.ext.asyncio import AsyncSession

from db.database import get_session


router = APIRouter()

repository_turma = TurmaRepository()

@router.post('/', status_code=status.HTTP_201_CREATED, response_model=TurmaSchema)
async def post_turma(turma: TurmaSchema, db: AsyncSession=Depends(get_session)):
return await repository_turma.create(turma, db)
32 changes: 32 additions & 0 deletions src/repositories/turma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from src.models.turma import TurmaModel
from src.schemas.turma import TurmaSchema

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select

from fastapi import status, HTTPException


class TurmaRepository:

async def turma_existe(self, id: int, db: AsyncSession):
async with db as session:
query_turma = select(TurmaModel).filter(TurmaModel.id == id)
result = await session.execute(query_turma)
turma: TurmaModel = result.scalar()

if not turma:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="Turma não encontrada")
return turma

async def create(self, turma: TurmaSchema, db: AsyncSession):
nova_turma: TurmaModel = TurmaModel(**turma.dict())

try:
db.add(nova_turma)
await db.commit()
except Exception as error:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=error)
return nova_turma
6 changes: 3 additions & 3 deletions src/schemas/turma.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

class TurmaSchema(BaseModel):
id: Optional[int]
professor: str
ano: str
professor: int
ano: int
semestre: int
nome_disciplina: Optional[str]
nome_disciplina: str
created_at: Optional[datetime]

class Config:
Expand Down

0 comments on commit 41e244d

Please sign in to comment.