add config

This commit is contained in:
chakcy 2026-01-26 11:45:09 +08:00
parent 01e96b714a
commit 583a22fe00
4 changed files with 50 additions and 52 deletions

View File

@ -0,0 +1,47 @@
# app/services/item_service.py
from typing import Optional, List
from app.models.item import Item
from app.schemas.item import ItemCreate, ItemUpdate
class ItemService:
@staticmethod
async def get_all_items(
skip: int = 0,
limit: int = 100,
owner_id: Optional[int] = None
) -> List[Item]:
query = Item.all()
if owner_id:
query = query.filter(owner_id=owner_id)
return await query.offset(skip).limit(limit)
@staticmethod
async def get_item_by_id(item_id: int) -> Optional[Item]:
return await Item.filter(id=item_id).first().prefetch_related("owner")
@staticmethod
async def create_item(item_data: ItemCreate, owner_id: Optional[int] = None) -> Item:
item = await Item.create(
**item_data.model_dump(),
owner_id=owner_id
)
return item
@staticmethod
async def update_item(item_id: int, item_data: ItemUpdate) -> Optional[Item]:
item = await ItemService.get_item_by_id(item_id)
if not item:
return None
update_data = item_data.model_dump(exclude_unset=True)
await item.update_from_dict(update_data)
await item.save()
return item
@staticmethod
async def delete_item(item_id: int) -> bool:
item = await ItemService.get_item_by_id(item_id)
if item:
await item.delete()
return True
return False

View File

@ -65,52 +65,3 @@ class UserService:
await user.delete() await user.delete()
return True return True
return False return False
# app/services/item_service.py
from typing import Optional, List
from app.models.item import Item
from app.schemas.item import ItemCreate, ItemUpdate
class ItemService:
@staticmethod
async def get_all_items(
skip: int = 0,
limit: int = 100,
owner_id: Optional[int] = None
) -> List[Item]:
query = Item.all()
if owner_id:
query = query.filter(owner_id=owner_id)
return await query.offset(skip).limit(limit)
@staticmethod
async def get_item_by_id(item_id: int) -> Optional[Item]:
return await Item.filter(id=item_id).first().prefetch_related("owner")
@staticmethod
async def create_item(item_data: ItemCreate, owner_id: Optional[int] = None) -> Item:
item = await Item.create(
**item_data.model_dump(),
owner_id=owner_id
)
return item
@staticmethod
async def update_item(item_id: int, item_data: ItemUpdate) -> Optional[Item]:
item = await ItemService.get_item_by_id(item_id)
if not item:
return None
update_data = item_data.model_dump(exclude_unset=True)
await item.update_from_dict(update_data)
await item.save()
return item
@staticmethod
async def delete_item(item_id: int) -> bool:
item = await ItemService.get_item_by_id(item_id)
if item:
await item.delete()
return True
return False

View File

@ -51,7 +51,7 @@ def app_run(app_config: dict, default_config: dict):
host=host, host=host,
port=port, port=port,
reload=reload, reload=reload,
# workers=workers, workers=workers,
log_level="debug" if app_config.get("debug", False) else "info", log_level="debug" if app_config.get("debug", False) else "info",
access_log=True, access_log=True,
) )

View File

@ -32,7 +32,7 @@ def launch(run_fun: Callable[[dict, dict], None], run_mode: str):
default_config = config.get("default", {}) default_config = config.get("default", {})
run_mode = default_config.get("env", "development") run_mode = default_config.get("env", "development")
app_config = config.get(run_mode, {}) app_config = config.get(run_mode, {})
if run_mode == "production":
init_module_bank(config) init_module_bank(config)
run_fun(app_config, default_config) run_fun(app_config, default_config)