init
This commit is contained in:
2
app/crud/__init__.py
Normal file
2
app/crud/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
82
app/crud/base.py
Normal file
82
app/crud/base.py
Normal file
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
||||
|
||||
from loguru import logger
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.sql import Select
|
||||
|
||||
from app.db.base import Base
|
||||
|
||||
ModelType = TypeVar("ModelType", bound=Base)
|
||||
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
|
||||
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
|
||||
|
||||
|
||||
class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
||||
def __init__(self, model: Type[ModelType]):
|
||||
"""
|
||||
CRUD object with default methods to Create, Read, Update, Delete (CRUD).
|
||||
|
||||
**Parameters**
|
||||
|
||||
* `model`: A SQLAlchemy model class
|
||||
* `schema`: A Pydantic model (schema) class
|
||||
"""
|
||||
self.model = model
|
||||
|
||||
def get(self, db: Session, id: Any) -> Optional[ModelType]:
|
||||
return db.query(self.model).filter(self.model.id == id).first()
|
||||
|
||||
def page_query(self) -> Select:
|
||||
return select(self.model)
|
||||
|
||||
def get_multi(
|
||||
self, db: Session, *, skip: int = 0, limit: int = 100
|
||||
) -> List[ModelType]:
|
||||
return db.query(self.model).offset(skip).limit(limit).all()
|
||||
|
||||
def get_multi_query(self) -> Select:
|
||||
return select(self.model)
|
||||
|
||||
def create(self, db: Session, *, obj_in: CreateSchemaType) -> ModelType:
|
||||
obj_in_data = obj_in.model_dump()
|
||||
logger.debug(f"{obj_in_data=}")
|
||||
db_obj = self.model(**obj_in_data) # type: ignore
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
logger.debug(f"created {self.model.__name__}: {db_obj.id=}")
|
||||
return db_obj
|
||||
|
||||
def update(
|
||||
self,
|
||||
db: Session,
|
||||
*,
|
||||
db_obj: ModelType,
|
||||
obj_in: Union[UpdateSchemaType, Dict[str, Any]]
|
||||
) -> ModelType:
|
||||
obj_data = obj_in.model_dump()
|
||||
if isinstance(obj_in, dict):
|
||||
update_data = obj_in
|
||||
else:
|
||||
update_data = obj_in.model_dump(exclude_unset=True)
|
||||
for field in obj_data:
|
||||
if field in update_data:
|
||||
setattr(db_obj, field, update_data[field])
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def remove(self, db: Session, *, id: int) -> ModelType:
|
||||
obj = db.query(self.model).get(id)
|
||||
db.delete(obj)
|
||||
db.commit()
|
||||
return obj
|
||||
|
||||
def get_by_ids(self, db: Session, ids: list[int]):
|
||||
return db.query(self.model).filter(self.model.id.in_(ids)).all()
|
||||
30
app/crud/form_crud.py
Normal file
30
app/crud/form_crud.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
from sqlalchemy import and_, func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.form import Form
|
||||
from app.schemas import FormCreate, FormUpdate
|
||||
from app.crud.base import CRUDBase
|
||||
|
||||
|
||||
class CRUDForm(CRUDBase[Form, FormUpdate, FormCreate]):
|
||||
def get_last_bench(self, db: Session, num: int = 10):
|
||||
# 创建子查询,获取每个不同的name的最大id
|
||||
subquery = (
|
||||
db.query(self.model.name, func.max(self.model.id).label('max_id'))
|
||||
.group_by(self.model.name)
|
||||
.subquery()
|
||||
)
|
||||
|
||||
# 主查询,与子查询连接,获取每个name的最后一条数据
|
||||
results = (
|
||||
db.query(self.model)
|
||||
.join(subquery, and_(self.model.id == subquery.c.max_id))
|
||||
.all()
|
||||
)
|
||||
return results
|
||||
|
||||
|
||||
form = CRUDForm(Form)
|
||||
15
app/crud/result_crud.py
Normal file
15
app/crud/result_crud.py
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.result import Result
|
||||
from app.schemas import ResultCreate, ResultUpdate
|
||||
from app.crud.base import CRUDBase
|
||||
|
||||
|
||||
class CRUDResult(CRUDBase[Result, ResultUpdate, ResultCreate]):
|
||||
def get_by_uuid(self, db: Session, uuid: str) -> Result:
|
||||
return db.query(self.model).filter(self.model.uuid == uuid).all()
|
||||
|
||||
|
||||
result = CRUDResult(Result)
|
||||
Reference in New Issue
Block a user