|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # -*- coding: utf-8 -*- |
3 | | -from backend.app.admin.conf import admin_settings |
4 | 3 | from backend.app.admin.crud.crud_config import config_dao |
5 | 4 | from backend.app.admin.model import Config |
6 | 5 | from backend.app.admin.schema.config import CreateConfigParam, UpdateConfigParam |
7 | 6 | from backend.common.exception import errors |
8 | 7 | from backend.database.db_mysql import async_db_session |
9 | | -from backend.database.db_redis import redis_client |
10 | | -from backend.utils.serializers import select_as_dict |
11 | 8 |
|
12 | 9 |
|
13 | 10 | class ConfigService: |
14 | 11 | @staticmethod |
15 | 12 | async def get() -> Config | dict: |
16 | 13 | async with async_db_session() as db: |
17 | | - cache_config = await redis_client.hgetall(admin_settings.CONFIG_REDIS_KEY) |
18 | | - if not cache_config: |
19 | | - config = await config_dao.get_one(db) |
20 | | - if not config: |
21 | | - raise errors.NotFoundError(msg='系统配置不存在') |
22 | | - data_map = select_as_dict(config) |
23 | | - del data_map['created_time'] |
24 | | - del data_map['updated_time'] |
25 | | - await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=data_map) |
26 | | - return config |
27 | | - else: |
28 | | - return cache_config |
| 14 | + config = await config_dao.get(db) |
| 15 | + if not config: |
| 16 | + raise errors.NotFoundError(msg='配置不存在') |
| 17 | + return config |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + async def get_select(): ... |
29 | 21 |
|
30 | 22 | @staticmethod |
31 | 23 | async def create(*, obj: CreateConfigParam) -> None: |
32 | | - async with async_db_session.begin() as db: |
33 | | - config = await config_dao.get_one(db) |
34 | | - if config: |
35 | | - raise errors.ForbiddenError(msg='系统配置已存在') |
36 | | - await config_dao.create(db, obj) |
37 | | - await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=obj.model_dump()) |
| 24 | + async with async_db_session.begin(): |
| 25 | + ... |
38 | 26 |
|
39 | 27 | @staticmethod |
40 | 28 | async def update(*, pk: int, obj: UpdateConfigParam) -> int: |
41 | | - async with async_db_session.begin() as db: |
42 | | - count = await config_dao.update(db, pk, obj) |
43 | | - await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=obj.model_dump()) |
44 | | - return count |
| 29 | + async with async_db_session.begin(): |
| 30 | + ... |
45 | 31 |
|
46 | 32 | @staticmethod |
47 | 33 | async def delete(*, pk: list[int]) -> int: |
48 | | - async with async_db_session.begin() as db: |
49 | | - configs = await config_dao.get_all(db) |
50 | | - if len(configs) == 1: |
51 | | - raise errors.ForbiddenError(msg='系统配置无法彻底删除') |
52 | | - count = await config_dao.delete(db, pk) |
53 | | - await redis_client.delete(admin_settings.CONFIG_REDIS_KEY) |
54 | | - return count |
| 34 | + async with async_db_session.begin(): |
| 35 | + ... |
55 | 36 |
|
56 | 37 |
|
57 | 38 | config_service: ConfigService = ConfigService() |
0 commit comments