Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ from pydantic import BaseModel
|
|
| 3 |
from motor.motor_asyncio import AsyncIOMotorClient
|
| 4 |
from typing import List
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 6 |
import os, re
|
| 7 |
|
| 8 |
# βββ MongoDB connection ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -25,6 +26,7 @@ app.add_middleware(
|
|
| 25 |
client = AsyncIOMotorClient(MONGO_URI)
|
| 26 |
db = client.disease
|
| 27 |
disease_collection = db.diseases
|
|
|
|
| 28 |
|
| 29 |
# βββ Pydantic schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
|
|
@@ -192,6 +194,32 @@ def welcome():
|
|
| 192 |
return {"message": "Sarva AI Remediation API. Visit /docs for documentation."}
|
| 193 |
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
@app.get("/check/{plant_name}")
|
| 196 |
async def check(plant_name: str):
|
| 197 |
return {"plantName": plant_name}
|
|
|
|
| 3 |
from motor.motor_asyncio import AsyncIOMotorClient
|
| 4 |
from typing import List
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from datetime import datetime, timezone
|
| 7 |
import os, re
|
| 8 |
|
| 9 |
# βββ MongoDB connection ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 26 |
client = AsyncIOMotorClient(MONGO_URI)
|
| 27 |
db = client.disease
|
| 28 |
disease_collection = db.diseases
|
| 29 |
+
meta_collection = db.meta
|
| 30 |
|
| 31 |
# βββ Pydantic schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 32 |
|
|
|
|
| 194 |
return {"message": "Sarva AI Remediation API. Visit /docs for documentation."}
|
| 195 |
|
| 196 |
|
| 197 |
+
@app.get("/kotlinback/meta/last-edited")
|
| 198 |
+
async def get_last_edited():
|
| 199 |
+
"""
|
| 200 |
+
Returns the global lastEdited timestamp for all remediation data.
|
| 201 |
+
The Android app uses this to decide whether to fetch fresh data.
|
| 202 |
+
If no meta document exists yet, one is created with the current UTC time.
|
| 203 |
+
"""
|
| 204 |
+
doc = await meta_collection.find_one({"_id": "global"})
|
| 205 |
+
if doc is None:
|
| 206 |
+
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 207 |
+
await meta_collection.insert_one({"_id": "global", "lastEdited": now})
|
| 208 |
+
return {"lastEdited": now}
|
| 209 |
+
return {"lastEdited": doc["lastEdited"]}
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
@app.get("/kotlinback/all")
|
| 213 |
+
async def get_all_diseases(lang: str = "en"):
|
| 214 |
+
"""
|
| 215 |
+
Returns all disease remediation documents in a single request.
|
| 216 |
+
The Android app calls this when a lastEdited version mismatch is detected
|
| 217 |
+
and bulk-caches all results to avoid one-by-one fetches.
|
| 218 |
+
"""
|
| 219 |
+
diseases = await disease_collection.find({}).to_list(length=None)
|
| 220 |
+
return [build_disease_response(d, lang).dict() for d in diseases]
|
| 221 |
+
|
| 222 |
+
|
| 223 |
@app.get("/check/{plant_name}")
|
| 224 |
async def check(plant_name: str):
|
| 225 |
return {"plantName": plant_name}
|