-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
criacao de repositorie e endpoint para criar turma. #32
Co-Authored-by: JoaoP-Coelho <[email protected]>
- Loading branch information
1 parent
cd01a24
commit 41e244d
Showing
3 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters