Skip to content

Commit 64ee74b

Browse files
authored
Add system notice interface (#487)
1 parent 1e855db commit 64ee74b

File tree

7 files changed

+263
-0
lines changed

7 files changed

+263
-0
lines changed

backend/app/admin/api/v1/sys/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from backend.app.admin.api.v1.sys.dict_data import router as dict_data_router
1111
from backend.app.admin.api.v1.sys.dict_type import router as dict_type_router
1212
from backend.app.admin.api.v1.sys.menu import router as menu_router
13+
from backend.app.admin.api.v1.sys.notice import router as notice_router
1314
from backend.app.admin.api.v1.sys.role import router as role_router
1415
from backend.app.admin.api.v1.sys.user import router as user_router
1516

@@ -25,3 +26,4 @@
2526
router.include_router(role_router, prefix='/roles', tags=['系统角色'])
2627
router.include_router(user_router, prefix='/users', tags=['系统用户'])
2728
router.include_router(data_rule_router, prefix='/data-rules', tags=['系统数据权限规则'])
29+
router.include_router(notice_router, prefix='/notices', tags=['系统通知公告'])
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from typing import Annotated
4+
5+
from fastapi import APIRouter, Depends, Path, Query
6+
7+
from backend.app.admin.schema.notice import CreateNoticeParam, GetNoticeListDetails, UpdateNoticeParam
8+
from backend.app.admin.service.notice_service import notice_service
9+
from backend.common.pagination import DependsPagination, paging_data
10+
from backend.common.response.response_schema import ResponseModel, response_base
11+
from backend.common.security.jwt import DependsJwtAuth
12+
from backend.common.security.permission import RequestPermission
13+
from backend.common.security.rbac import DependsRBAC
14+
from backend.database.db import CurrentSession
15+
16+
router = APIRouter()
17+
18+
19+
@router.get('/{pk}', summary='获取通知公告详情', dependencies=[DependsJwtAuth])
20+
async def get_notice(pk: Annotated[int, Path(...)]) -> ResponseModel:
21+
notice = await notice_service.get(pk=pk)
22+
return response_base.success(data=notice)
23+
24+
25+
@router.get(
26+
'',
27+
summary='(模糊条件)分页获取所有通知公告',
28+
dependencies=[
29+
DependsJwtAuth,
30+
DependsPagination,
31+
],
32+
)
33+
async def get_pagination_notice(db: CurrentSession) -> ResponseModel:
34+
notice_select = await notice_service.get_select()
35+
page_data = await paging_data(db, notice_select, GetNoticeListDetails)
36+
return response_base.success(data=page_data)
37+
38+
39+
@router.post(
40+
'',
41+
summary='创建通知公告',
42+
dependencies=[
43+
Depends(RequestPermission('sys:notice:add')),
44+
DependsRBAC,
45+
],
46+
)
47+
async def create_notice(obj: CreateNoticeParam) -> ResponseModel:
48+
await notice_service.create(obj=obj)
49+
return response_base.success()
50+
51+
52+
@router.put(
53+
'/{pk}',
54+
summary='更新通知公告',
55+
dependencies=[
56+
Depends(RequestPermission('sys:notice:edit')),
57+
DependsRBAC,
58+
],
59+
)
60+
async def update_notice(pk: Annotated[int, Path(...)], obj: UpdateNoticeParam) -> ResponseModel:
61+
count = await notice_service.update(pk=pk, obj=obj)
62+
if count > 0:
63+
return response_base.success()
64+
return response_base.fail()
65+
66+
67+
@router.delete(
68+
'',
69+
summary='(批量)删除通知公告',
70+
dependencies=[
71+
Depends(RequestPermission('sys:notice:del')),
72+
DependsRBAC,
73+
],
74+
)
75+
async def delete_notice(pk: Annotated[list[int], Query(...)]) -> ResponseModel:
76+
count = await notice_service.delete(pk=pk)
77+
if count > 0:
78+
return response_base.success()
79+
return response_base.fail()

backend/app/admin/crud/crud_notice.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from typing import Sequence
4+
5+
from sqlalchemy import Select
6+
from sqlalchemy.ext.asyncio import AsyncSession
7+
from sqlalchemy_crud_plus import CRUDPlus
8+
9+
from backend.app.admin.model import Notice
10+
from backend.app.admin.schema.notice import CreateNoticeParam, UpdateNoticeParam
11+
12+
13+
class CRUDNotice(CRUDPlus[Notice]):
14+
async def get(self, db: AsyncSession, pk: int) -> Notice | None:
15+
"""
16+
获取系统通知公告
17+
18+
:param db:
19+
:param pk:
20+
:return:
21+
"""
22+
return await self.select_model(db, pk)
23+
24+
async def get_list(self) -> Select:
25+
"""
26+
获取系统通知公告列表
27+
28+
:return:
29+
"""
30+
return await self.select_order('created_time', 'desc')
31+
32+
async def get_all(self, db: AsyncSession) -> Sequence[Notice]:
33+
"""
34+
获取所有系统通知公告
35+
36+
:param db:
37+
:return:
38+
"""
39+
return await self.select_models(db)
40+
41+
async def create(self, db: AsyncSession, obj_in: CreateNoticeParam) -> None:
42+
"""
43+
创建系统通知公告
44+
45+
:param db:
46+
:param obj_in:
47+
:return:
48+
"""
49+
await self.create_model(db, obj_in)
50+
51+
async def update(self, db: AsyncSession, pk: int, obj_in: UpdateNoticeParam) -> int:
52+
"""
53+
更新系统通知公告
54+
55+
:param db:
56+
:param pk:
57+
:param obj_in:
58+
:return:
59+
"""
60+
return await self.update_model(db, pk, obj_in)
61+
62+
async def delete(self, db: AsyncSession, pk: list[int]) -> int:
63+
"""
64+
删除系统通知公告
65+
66+
:param db:
67+
:param pk:
68+
:return:
69+
"""
70+
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pk)
71+
72+
73+
notice_dao: CRUDNotice = CRUDNotice(Notice)

backend/app/admin/model/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from backend.app.admin.model.dict_type import DictType
1010
from backend.app.admin.model.login_log import LoginLog
1111
from backend.app.admin.model.menu import Menu
12+
from backend.app.admin.model.notice import Notice
1213
from backend.app.admin.model.opera_log import OperaLog
1314
from backend.app.admin.model.role import Role
1415
from backend.app.admin.model.user import User

backend/app/admin/model/notice.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from sqlalchemy import TEXT, String
4+
from sqlalchemy.dialects.mysql import LONGTEXT
5+
from sqlalchemy.orm import Mapped, mapped_column
6+
7+
from backend.common.model import Base, id_key
8+
9+
10+
class Notice(Base):
11+
"""系统通知公告"""
12+
13+
__tablename__ = 'sys_notice'
14+
15+
id: Mapped[id_key] = mapped_column(init=False)
16+
title: Mapped[str] = mapped_column(String(50), comment='标题')
17+
type: Mapped[int] = mapped_column(comment='类型(0:通知、1:公告)')
18+
author: Mapped[str] = mapped_column(String(16), comment='作者')
19+
source: Mapped[str] = mapped_column(String(50), comment='信息来源')
20+
status: Mapped[int] = mapped_column(comment='状态(0:隐藏、1:显示)')
21+
content: Mapped[str] = mapped_column(LONGTEXT().with_variant(TEXT, 'postgresql'), comment='内容')

backend/app/admin/schema/notice.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from datetime import datetime
4+
5+
from pydantic import ConfigDict, Field
6+
7+
from backend.common.enums import StatusType
8+
from backend.common.schema import SchemaBase
9+
10+
11+
class NoticeSchemaBase(SchemaBase):
12+
title: str
13+
type: int
14+
author: str
15+
source: str
16+
status: StatusType = Field(StatusType.enable)
17+
content: str
18+
19+
20+
class CreateNoticeParam(NoticeSchemaBase):
21+
pass
22+
23+
24+
class UpdateNoticeParam(NoticeSchemaBase):
25+
pass
26+
27+
28+
class GetNoticeListDetails(NoticeSchemaBase):
29+
model_config = ConfigDict(from_attributes=True)
30+
31+
id: int
32+
created_time: datetime
33+
updated_time: datetime | None = None
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from typing import Sequence
4+
5+
from sqlalchemy import Select
6+
7+
from backend.app.admin.crud.crud_notice import notice_dao
8+
from backend.app.admin.model import Notice
9+
from backend.app.admin.schema.notice import CreateNoticeParam, UpdateNoticeParam
10+
from backend.common.exception import errors
11+
from backend.database.db import async_db_session
12+
13+
14+
class NoticeService:
15+
@staticmethod
16+
async def get(*, pk: int) -> Notice:
17+
async with async_db_session() as db:
18+
notice = await notice_dao.get(db, pk)
19+
if not notice:
20+
raise errors.NotFoundError(msg='通知公告不存在')
21+
return notice
22+
23+
@staticmethod
24+
async def get_select() -> Select:
25+
return await notice_dao.get_list()
26+
27+
@staticmethod
28+
async def get_all() -> Sequence[Notice]:
29+
async with async_db_session() as db:
30+
notices = await notice_dao.get_all(db)
31+
return notices
32+
33+
@staticmethod
34+
async def create(*, obj: CreateNoticeParam) -> None:
35+
async with async_db_session.begin() as db:
36+
await notice_dao.create(db, obj)
37+
38+
@staticmethod
39+
async def update(*, pk: int, obj: UpdateNoticeParam) -> int:
40+
async with async_db_session.begin() as db:
41+
notice = await notice_dao.get(db, pk)
42+
if not notice:
43+
raise errors.NotFoundError(msg='通知公告不存在')
44+
count = await notice_dao.update(db, pk, obj)
45+
return count
46+
47+
@staticmethod
48+
async def delete(*, pk: list[int]) -> int:
49+
async with async_db_session.begin() as db:
50+
count = await notice_dao.delete(db, pk)
51+
return count
52+
53+
54+
notice_service: NoticeService = NoticeService()

0 commit comments

Comments
 (0)