2022-01-09 23:59:53 +08:00
|
|
|
from typing import List, Optional
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
import strawberry # noqa
|
2023-04-10 20:23:31 +08:00
|
|
|
from api.gql.noticeboard.types import NoticeType
|
|
|
|
from apps.noticeboard import models
|
|
|
|
from core.uid_gen import FelicityID
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class NoticeQuery:
|
|
|
|
@strawberry.field
|
2023-03-19 23:21:32 +08:00
|
|
|
async def notice_by_uid(self, info, uid: FelicityID) -> Optional[NoticeType]:
|
2021-12-06 00:37:33 +08:00
|
|
|
return await models.Notice.get(uid=uid)
|
|
|
|
|
|
|
|
@strawberry.field
|
2023-03-19 23:25:50 +08:00
|
|
|
async def notices_by_creator(
|
|
|
|
self, info, uid: FelicityID
|
|
|
|
) -> Optional[List[NoticeType]]:
|
2021-12-06 00:37:33 +08:00
|
|
|
return await models.Notice.get_all(created_by_uid=uid)
|
|
|
|
|
|
|
|
@strawberry.field
|
2022-01-09 23:59:53 +08:00
|
|
|
async def notice_filter(
|
2023-03-19 23:25:50 +08:00
|
|
|
self,
|
|
|
|
info,
|
2023-04-25 22:42:39 +08:00
|
|
|
group_uid: FelicityID | None,
|
|
|
|
department_uid: FelicityID | None,
|
2022-01-09 23:59:53 +08:00
|
|
|
) -> List[NoticeType]:
|
2021-12-06 00:37:33 +08:00
|
|
|
filters = {}
|
|
|
|
|
|
|
|
if group_uid:
|
2022-01-09 23:59:53 +08:00
|
|
|
filters["groups__uid__in"] = [group_uid]
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
if department_uid:
|
2022-01-09 23:59:53 +08:00
|
|
|
filters["departments__uid__in"] = [department_uid]
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
notice_stmt = models.Notice.smart_query(
|
2022-01-09 23:59:53 +08:00
|
|
|
filters=filters, sort_attrs=["-created_at"]
|
2021-12-06 00:37:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
notices = (await models.Notice.session.execute(notice_stmt)).scalars().all()
|
|
|
|
return list(notices)
|