refactor: define people service to CURD people
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# created by mmmy on 2025-09-30
|
# created by mmmy on 2025-09-30
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
|
from sqlalchemy import Column, Integer, String, Text, DateTime, func
|
||||||
|
|
||||||
|
from utils.rldb import RLDBBaseModel
|
||||||
|
|
||||||
class PeopleRLDBModel(RLDBBaseModel):
|
class PeopleRLDBModel(RLDBBaseModel):
|
||||||
__tablename__ = 'peoples'
|
__tablename__ = 'peoples'
|
||||||
id = Column(String(36), primary_key=True)
|
id = Column(String(36), primary_key=True)
|
||||||
@@ -21,21 +24,6 @@ class PeopleRLDBModel(RLDBBaseModel):
|
|||||||
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
|
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
|
||||||
deleted_at = Column(DateTime(timezone=True), nullable=True, index=True)
|
deleted_at = Column(DateTime(timezone=True), nullable=True, index=True)
|
||||||
|
|
||||||
def to_people(self) -> People:
|
|
||||||
# 将关系数据库模型转换为对象
|
|
||||||
return People(
|
|
||||||
id=self.id,
|
|
||||||
name=self.name,
|
|
||||||
contact=self.contact,
|
|
||||||
gender=self.gender,
|
|
||||||
age=self.age,
|
|
||||||
height=self.height,
|
|
||||||
marital_status=self.marital_status,
|
|
||||||
match_requirement=self.match_requirement,
|
|
||||||
introduction=self.introduction,
|
|
||||||
comments=self.comments,
|
|
||||||
cover=self.cover,
|
|
||||||
)
|
|
||||||
|
|
||||||
class People:
|
class People:
|
||||||
# 数据库 ID
|
# 数据库 ID
|
||||||
@@ -82,6 +70,52 @@ class People:
|
|||||||
f"match_requirement={self.match_requirement}, introduction={self.introduction}, "
|
f"match_requirement={self.match_requirement}, introduction={self.introduction}, "
|
||||||
f"comments={self.comments}, cover={self.cover})")
|
f"comments={self.comments}, cover={self.cover})")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_dict(cls, data: dict):
|
||||||
|
if 'created_at' in data:
|
||||||
|
# 移除 created_at 字段,避免类型错误
|
||||||
|
del data['created_at']
|
||||||
|
if 'updated_at' in data:
|
||||||
|
# 移除 updated_at 字段,避免类型错误
|
||||||
|
del data['updated_at']
|
||||||
|
if 'deleted_at' in data:
|
||||||
|
# 移除 deleted_at 字段,避免类型错误
|
||||||
|
del data['deleted_at']
|
||||||
|
return cls(**data)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_rldb_model(cls, data: PeopleRLDBModel):
|
||||||
|
# 将关系数据库模型转换为对象
|
||||||
|
return cls(
|
||||||
|
id=data.id,
|
||||||
|
name=data.name,
|
||||||
|
contact=data.contact,
|
||||||
|
gender=data.gender,
|
||||||
|
age=data.age,
|
||||||
|
height=data.height,
|
||||||
|
marital_status=data.marital_status,
|
||||||
|
match_requirement=data.match_requirement,
|
||||||
|
introduction=data.introduction,
|
||||||
|
comments=data.comments,
|
||||||
|
cover=data.cover,
|
||||||
|
)
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
# 将对象转换为字典格式
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'name': self.name,
|
||||||
|
'contact': self.contact,
|
||||||
|
'gender': self.gender,
|
||||||
|
'age': self.age,
|
||||||
|
'height': self.height,
|
||||||
|
'marital_status': self.marital_status,
|
||||||
|
'match_requirement': self.match_requirement,
|
||||||
|
'introduction': self.introduction,
|
||||||
|
'comments': self.comments,
|
||||||
|
'cover': self.cover,
|
||||||
|
}
|
||||||
|
|
||||||
def to_rldb_model(self) -> PeopleRLDBModel:
|
def to_rldb_model(self) -> PeopleRLDBModel:
|
||||||
# 将对象转换为关系数据库模型
|
# 将对象转换为关系数据库模型
|
||||||
return PeopleRLDBModel(
|
return PeopleRLDBModel(
|
||||||
|
|||||||
0
src/services/__init__.py
Normal file
0
src/services/__init__.py
Normal file
64
src/services/people.py
Normal file
64
src/services/people.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
from models.people import People, PeopleRLDBModel
|
||||||
|
from utils.error import error
|
||||||
|
from utils import rldb
|
||||||
|
|
||||||
|
|
||||||
|
class PeopleService:
|
||||||
|
def __init__(self):
|
||||||
|
self.rldb = rldb.get_instance()
|
||||||
|
|
||||||
|
def save(self, people: People) -> (str, error):
|
||||||
|
"""
|
||||||
|
保存人物到数据库和向量数据库
|
||||||
|
|
||||||
|
:param people: 人物对象
|
||||||
|
:return: 人物ID
|
||||||
|
"""
|
||||||
|
# 0. 生成 people id
|
||||||
|
people.id = people.id if people.id else uuid.uuid4().hex
|
||||||
|
|
||||||
|
# 1. 转换模型,并保存到 SQL 数据库
|
||||||
|
people_orm = people.to_rldb_model()
|
||||||
|
self.rldb.upsert(people_orm)
|
||||||
|
|
||||||
|
return people.id, error(0, "")
|
||||||
|
|
||||||
|
def delete(self, people_id: str) -> error:
|
||||||
|
"""
|
||||||
|
删除人物从数据库和向量数据库
|
||||||
|
|
||||||
|
:param people_id: 人物ID
|
||||||
|
:return: 错误对象
|
||||||
|
"""
|
||||||
|
people_orm = self.rldb.get(PeopleRLDBModel, people_id)
|
||||||
|
if not people_orm:
|
||||||
|
return error(1, f"people {people_id} not found")
|
||||||
|
self.rldb.delete(people_orm)
|
||||||
|
return error(0, "")
|
||||||
|
|
||||||
|
def list(self, conds: dict = {}, limit: int = 10, offset: int = 0) -> (list[People], error):
|
||||||
|
"""
|
||||||
|
从数据库列出人物
|
||||||
|
|
||||||
|
:param conds: 查询条件字典
|
||||||
|
:param limit: 分页大小
|
||||||
|
:param offset: 分页偏移量
|
||||||
|
:return: 人物对象列表
|
||||||
|
"""
|
||||||
|
people_orms = self.rldb.query(PeopleRLDBModel, **conds)
|
||||||
|
peoples = [People.from_rldb_model(people_orm) for people_orm in people_orms]
|
||||||
|
|
||||||
|
return peoples, error(0, "")
|
||||||
|
|
||||||
|
people_service = None
|
||||||
|
|
||||||
|
def init():
|
||||||
|
global people_service
|
||||||
|
people_service = PeopleService()
|
||||||
|
|
||||||
|
def get_instance() -> PeopleService:
|
||||||
|
return people_service
|
||||||
@@ -13,11 +13,11 @@ class error(Protocol):
|
|||||||
return f"{self.__class__.__name__}({self._error_code}, {self._error_info})"
|
return f"{self.__class__.__name__}({self._error_code}, {self._error_info})"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def error_code(self) -> int:
|
def code(self) -> int:
|
||||||
return self._error_code
|
return self._error_code
|
||||||
@property
|
@property
|
||||||
def error_info(self) -> str:
|
def info(self) -> str:
|
||||||
return self._error_info
|
return self._error_info
|
||||||
@property
|
@property
|
||||||
def is_success(self) -> bool:
|
def success(self) -> bool:
|
||||||
return self._error_code == 0
|
return self._error_code == 0
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ class RelationalDB(Protocol):
|
|||||||
def query(self,
|
def query(self,
|
||||||
model: type[RLDBBaseModel],
|
model: type[RLDBBaseModel],
|
||||||
include_deleted: bool = False,
|
include_deleted: bool = False,
|
||||||
limit: int = 10,
|
limit: int = None,
|
||||||
offset: int = 0,
|
offset: int = None,
|
||||||
**filters
|
**filters
|
||||||
) -> list[RLDBBaseModel]:
|
) -> list[RLDBBaseModel]:
|
||||||
...
|
...
|
||||||
@@ -102,8 +102,8 @@ class SqlAlchemyDB():
|
|||||||
|
|
||||||
def query(self,
|
def query(self,
|
||||||
model: type[RLDBBaseModel],
|
model: type[RLDBBaseModel],
|
||||||
limit: int = 10,
|
limit: int = None,
|
||||||
offset: int = 0,
|
offset: int = None,
|
||||||
**filters
|
**filters
|
||||||
) -> list[RLDBBaseModel]:
|
) -> list[RLDBBaseModel]:
|
||||||
results: list[RLDBBaseModel] = []
|
results: list[RLDBBaseModel] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user