Coverage for src/auth/api/permission_controller.py: 100%

1 from typing import Annotated

2

3 from advanced_alchemy.extensions.litestar.dto import SQLAlchemyDTO, SQLAlchemyDTOConfig

4 from advanced_alchemy.extensions.litestar.providers import create_service_dependencies

5 from advanced_alchemy.filters import FilterTypes

6 from advanced_alchemy.service import OffsetPagination

7 from litestar import delete, get, post

8 from litestar.controller import Controller

9 from litestar.params import Dependency

10 import msgspec

11

12 from ..models import PermissionModel

13 from ..services import PermissionService

14

15

16 class PermissionCreateRequest(msgspec.Struct):

17 name: str

18

19

20 class PermissionDTO(SQLAlchemyDTO[PermissionModel]):

21 config = SQLAlchemyDTOConfig()

22

23

24 class PermissionController(Controller):

25 dependencies = create_service_dependencies(

26 PermissionService,

27 key="service",

28 filters={

29 "created_at": True,

30 "updated_at": True,

31 },

32 )

33 return_dto = PermissionDTO

34

35 @get(operation_id="ListPermissions", path="/permissions")

36 async def list_permissions(

37 self,

38 service: PermissionService,

39 filters: Annotated[list[FilterTypes], Dependency(skip_validation=True)],

40 ) -> OffsetPagination[PermissionModel]:

41 results, total = await service.list_and_count(*filters)

42 return service.to_schema(data=results, total=total, filters=filters)

43

44 @post(operation_id="CreatePermission", path="/permissions")

45 async def create_permission(self, service: PermissionService, data: PermissionCreateRequest) -> PermissionModel:

46 return await service.create(PermissionModel(**msgspec.to_builtins(data)), auto_commit=True)

47

48 @delete(operation_id="DeletePermission", path="/permissions/{item_id:str}")

49 async def delete_permission(self, service: PermissionService, item_id: str) -> None:

50 await service.delete(item_id, auto_commit=True)