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

12
.idea/workspace.xml generated
View File

@@ -6,7 +6,7 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="92592c29-2276-4c2c-bd45-9ab94e25491d" name="Changes" comment=""> <list default="true" id="92592c29-2276-4c2c-bd45-9ab94e25491d" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/schemas/patch.py" beforeDir="false" afterPath="$PROJECT_DIR$/schemas/patch.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/routers/patch.py" beforeDir="false" afterPath="$PROJECT_DIR$/routers/patch.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/service/patch_service.py" beforeDir="false" afterPath="$PROJECT_DIR$/service/patch_service.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/service/patch_service.py" beforeDir="false" afterPath="$PROJECT_DIR$/service/patch_service.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@@ -26,9 +26,9 @@
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component> </component>
<component name="ProjectColorInfo"><![CDATA[{ <component name="ProjectColorInfo">{
"associatedIndex": 7 &quot;associatedIndex&quot;: 7
}]]></component> }</component>
<component name="ProjectId" id="3J4P6AjqhDBRxWCujlbOFJb7BqN" /> <component name="ProjectId" id="3J4P6AjqhDBRxWCujlbOFJb7BqN" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true"> <component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" /> <ConfirmationsSetting value="2" id="Add" />
@@ -41,6 +41,7 @@
"keyToString": { "keyToString": {
"FastAPI.patch-service.executor": "Run", "FastAPI.patch-service.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
"git-widget-placeholder": "master", "git-widget-placeholder": "master",
"last_opened_file_path": "D:/Cxx/PythonProjects/patch-service/routers", "last_opened_file_path": "D:/Cxx/PythonProjects/patch-service/routers",
"node.js.detected.package.eslint": "true", "node.js.detected.package.eslint": "true",
@@ -91,6 +92,7 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1788917189828</updated> <updated>1788917189828</updated>
<workItem from="1788917190899" duration="18264000" /> <workItem from="1788917190899" duration="18264000" />
<workItem from="1789023303581" duration="1297000" />
</task> </task>
<servers /> <servers />
</component> </component>
@@ -98,6 +100,6 @@
<option name="version" value="3" /> <option name="version" value="3" />
</component> </component>
<component name="com.intellij.coverage.CoverageDataManagerImpl"> <component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/patch_service$patch_service.coverage" NAME="patch-service Coverage Results" MODIFIED="1788951341519" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" /> <SUITE FILE_PATH="coverage/patch_service$patch_service.coverage" NAME="patch-service Coverage Results" MODIFIED="1789023884564" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
</component> </component>
</project> </project>

View File

@@ -15,8 +15,8 @@ def create(data: PatchCreate, db: Session = Depends(get_db)):
@router.get("", response_model=list[PatchResponse]) @router.get("", response_model=list[PatchResponse])
def list_all(db: Session = Depends(get_db)): def list_all(keyword: str | None, db: Session = Depends(get_db)):
return patch_service.get_patches(db) return patch_service.get_patches(db, keyword)
@router.get("/{patch_id}", response_model=PatchResponse) @router.get("/{patch_id}", response_model=PatchResponse)

View File

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