Skip to content

Commit

Permalink
⚡️ update methods to use SQLAlchemy Core and async
Browse files Browse the repository at this point in the history
  • Loading branch information
agn-7 committed Nov 30, 2023
1 parent ce3d184 commit ca65ce5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
36 changes: 19 additions & 17 deletions ifsguid/crud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import List
from uuid import UUID

from sqlalchemy.orm import Session, joinedload, selectinload
from sqlalchemy import delete, update
from sqlalchemy.orm import joinedload
from sqlalchemy.future import select
from sqlalchemy.ext.asyncio import AsyncSession

Expand Down Expand Up @@ -40,13 +41,11 @@ async def create_interaction(


async def delete_interaction(db: AsyncSession, id: UUID) -> None:
interaction = (
db.query(models.Interaction).filter(models.Interaction.id == id).first()
)
stmt = delete(models.Interaction).where(models.Interaction.id == id)
result = await db.execute(stmt)

if interaction is not None:
db.delete(interaction)
db.commit()
if result.rowcount:
await db.commit()
return True

return False
Expand All @@ -55,30 +54,33 @@ async def delete_interaction(db: AsyncSession, id: UUID) -> None:
async def update_interaction(
db: AsyncSession, id: UUID, settings: schemas.Settings
) -> models.Interaction:
interaction: models.Interaction = (
db.query(models.Interaction).filter(models.Interaction.id == id).first()
stmt = (
update(models.Interaction)
.where(models.Interaction.id == id)
.values(settings=settings)
)
result = await db.execute(stmt)

if interaction is not None:
interaction.settings = settings
db.commit()
return interaction
if result.rowcount:
await db.commit()
return True

return None


async def get_messages(
db: AsyncSession, interaction_id: UUID = None, page: int = None, per_page: int = 10
) -> List[models.Message]:
query = db.query(models.Message)
stmt = select(models.Message)

if interaction_id is not None:
query = query.filter(models.Message.interaction_id == interaction_id)
stmt = stmt.where(models.Message.interaction_id == interaction_id)

if page is not None:
query = query.offset((page - 1) * per_page).limit(per_page)
stmt = stmt.offset((page - 1) * per_page).limit(per_page)

return query.all()
result = await db.execute(stmt)
return result.scalars().all()


async def create_message(
Expand Down
13 changes: 6 additions & 7 deletions ifsguid/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,18 @@ async def get_all_message_in_interaction(
per_page: Optional[int] = None,
db: AsyncSession = Depends(get_db),
) -> List[schemas.Message]:
interaction = crud.get_interaction(db=db, id=str(interaction_id))
interaction = await crud.get_interaction(db=db, id=str(interaction_id))

if not interaction:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Interaction not found"
)

return [
schemas.Message.model_validate(message)
for message in crud.get_messages(
db=db, interaction_id=str(interaction_id), page=page, per_page=per_page
)
]
messages = await crud.get_messages(
db=db, interaction_id=str(interaction_id), page=page, per_page=per_page
)

return [schemas.Message.model_validate(message) for message in messages]


@router.post(
Expand Down

0 comments on commit ca65ce5

Please sign in to comment.