新增小程序后端代码,包括数据库、路由、服务层等。
小程序前后端打通,包括登录、注册、绑定设备、查询绑定信息,修改小朋友名称等功能。
This commit is contained in:
42
mini-program/app/service/child.py
Normal file
42
mini-program/app/service/child.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from collections.abc import Mapping
|
||||
from datetime import date
|
||||
from typing import Optional
|
||||
|
||||
from app.dao.child import ChildDAO
|
||||
|
||||
|
||||
class ChildService:
|
||||
def __init__(self, db):
|
||||
self.dao = ChildDAO(db)
|
||||
|
||||
def create(
|
||||
self,
|
||||
user_id: int,
|
||||
child_name: str,
|
||||
child_gender: int = 2,
|
||||
child_birthday: Optional[date] = None,
|
||||
) -> Mapping:
|
||||
child_id = self.dao.create(user_id, child_name, child_gender, child_birthday)
|
||||
return self.dao.get_by_id(child_id)
|
||||
|
||||
def list_children(self, user_id: int, limit: int = 20, cursor: int = None) -> tuple[list, bool]:
|
||||
rows = self.dao.list_by_parent(user_id, limit, cursor)
|
||||
has_more = len(rows) > limit
|
||||
rows = rows[:limit]
|
||||
return rows, has_more
|
||||
|
||||
def get(self, child_id: int) -> Optional[Mapping]:
|
||||
return self.dao.get_by_id(child_id)
|
||||
|
||||
def update(
|
||||
self,
|
||||
child_id: int,
|
||||
user_id: int,
|
||||
child_name: Optional[str] = None,
|
||||
child_gender: Optional[int] = None,
|
||||
child_birthday: Optional[date] = None,
|
||||
) -> Mapping:
|
||||
if not self.dao.has_access(child_id, user_id):
|
||||
raise PermissionError("No access to this child")
|
||||
self.dao.update(child_id, child_name, child_gender, child_birthday)
|
||||
return self.dao.get_by_id(child_id)
|
||||
Reference in New Issue
Block a user