From fc58e8ee21b892430e4898980730cee19b75ca8f Mon Sep 17 00:00:00 2001
From: Cxx0822 <1556464090@qq.com>
Date: Thu, 10 Sep 2026 15:27:47 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=8F=9C=E5=9C=B0?=
=?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/workspace.xml | 12 +++++++-----
routers/patch.py | 4 ++--
service/patch_service.py | 19 ++++++++++++++++---
3 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 4e6e3c6..b793f09 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -6,7 +6,7 @@
-
+
@@ -26,9 +26,9 @@
-
+ {
+ "associatedIndex": 7
+}
@@ -41,6 +41,7 @@
"keyToString": {
"FastAPI.patch-service.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
+ "RunOnceActivity.git.unshallow": "true",
"git-widget-placeholder": "master",
"last_opened_file_path": "D:/Cxx/PythonProjects/patch-service/routers",
"node.js.detected.package.eslint": "true",
@@ -91,6 +92,7 @@
1788917189828
+
@@ -98,6 +100,6 @@
-
+
\ No newline at end of file
diff --git a/routers/patch.py b/routers/patch.py
index 3ce15bf..7b86ff7 100644
--- a/routers/patch.py
+++ b/routers/patch.py
@@ -15,8 +15,8 @@ def create(data: PatchCreate, db: Session = Depends(get_db)):
@router.get("", response_model=list[PatchResponse])
-def list_all(db: Session = Depends(get_db)):
- return patch_service.get_patches(db)
+def list_all(keyword: str | None, db: Session = Depends(get_db)):
+ return patch_service.get_patches(db, keyword)
@router.get("/{patch_id}", response_model=PatchResponse)
diff --git a/service/patch_service.py b/service/patch_service.py
index 5fe0fec..476a6cb 100644
--- a/service/patch_service.py
+++ b/service/patch_service.py
@@ -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)