Skip to content

Commit

Permalink
feat: add route to delete many events-groups by its tag alias
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Jan 20, 2025
1 parent a0d3891 commit aa36737
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/modules/event_groups/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ViewEventGroup,
)
from src.modules.ownership import OwnershipEnum, setup_ownership_method
from src.modules.tags.repository import tag_repository
from src.storages.sql import SQLAlchemyStorage
from src.storages.sql.models import EventGroup

Expand Down Expand Up @@ -162,6 +163,17 @@ async def delete_by_alias(self, alias: str) -> None:
await session.execute(q)
await session.commit()

async def delete_by_tag_alias(self, tag_alias: str) -> int:
tag = await tag_repository.read_by_alias(tag_alias)
if tag is None:
return 0

async with self._create_session() as session:
q = delete(EventGroup.tags_association).where(EventGroup.tags_association.c.tag_id == tag.id)
deleted = await session.execute(q)
await session.commit()
return cast(deleted.rowcount, int)

# ^^^^^^^^^^^^^^^^^ CRUD ^^^^^^^^^^^^^^^^^ #

async def setup_ownership(self, group_id: int, user_id: int, role_alias: "OwnershipEnum") -> None:
Expand Down
26 changes: 25 additions & 1 deletion src/modules/event_groups/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ async def find_event_group_by_alias(alias: str) -> ViewEventGroup:
**EventGroupNotFoundException.responses,
},
)
async def delete_event_group_by_alias(alias: str) -> None:
async def delete_event_group_by_alias(
alias: str,
_: VERIFY_PARSER_DEPENDENCY,
) -> None:
"""
Delete event group by alias
"""
Expand All @@ -152,6 +155,27 @@ async def delete_event_group_by_alias(alias: str) -> None:
await event_group_repository.delete_by_alias(alias)


@router.delete(
"/by-tag-alias",
responses={200: {"description": "Event groups deleted successfully"}},
)
async def delete_event_group_by_tag_alias(
tag_alias: str,
_: VERIFY_PARSER_DEPENDENCY,
):
"""
Delete event groups by its tag alias
"""

tag_alias = unquote(tag_alias)
event_group = await event_group_repository.delete_by_tag_alias(tag_alias)

if event_group is None:
raise EventGroupNotFoundException()

await event_group_repository.delete_by_tag_alias(tag_alias)


@router.get(
"/{event_group_id}",
responses={
Expand Down
4 changes: 4 additions & 0 deletions src/modules/tags/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ async def delete(self, id: int) -> None:

# ^^^^^^^^^^^^^^^^^^^^ CRUD ^^^^^^^^^^^^^^^^^^^^ #

async def read_by_alias(self, alias: str) -> ViewTag | None:
async with self._create_session() as session:
return await CRUD.read_by(session, only_first=True, alias=alias)

async def setup_ownership(self, tag_id: int, user_id: int, role_alias: OwnershipEnum) -> None:
async with self._create_session() as session:
OwnershipClass = Tag.Ownership
Expand Down

0 comments on commit aa36737

Please sign in to comment.