feat: 增加菜地搜索功能

This commit is contained in:
2026-09-10 15:27:47 +08:00
parent 8b563e8249
commit fc58e8ee21
3 changed files with 25 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
from sqlalchemy import select, delete
from sqlalchemy import select, delete, or_
from sqlalchemy.orm import Session, selectinload
from models.patch import Patches, Crops, Records
@@ -14,9 +14,21 @@ def create_patch(db: Session, data: PatchCreate) -> bool:
return True
def get_patches(db: Session) -> list[PatchResponse]:
patches = db.execute(select(Patches)).scalars().all()
def get_patches(db: Session, keyword: str | None = None) -> list[PatchResponse]:
stmt = select(Patches)
if keyword:
like_pattern = f"%{keyword}%"
stmt = stmt.where(
or_(
Patches.name.ilike(like_pattern),
Patches.location.ilike(like_pattern),
)
)
patches = db.execute(stmt).scalars().all()
result = []
for p in patches:
crop = db.execute(
select(Crops).where(Crops.patch_id == p.id)
@@ -42,6 +54,7 @@ def get_patches(db: Session) -> list[PatchResponse]:
return result
def get_patch(db: Session, patch_id: int) -> PatchResponse | None:
patch = db.execute(
select(Patches).where(Patches.id == patch_id)