ocr-2 / ocr /api /report /db_requests.py
brestok's picture
made report and changes editable
127f2f2
from fastapi import HTTPException
from ocr.api.message.schemas import CreateMessageRequest
from ocr.api.report.dto import ReportModelShort
from ocr.api.report.model import ReportModel
from ocr.core.config import settings
async def get_all_reports_obj() -> list[ReportModelShort]:
reports = await settings.DB_CLIENT.reports.find({}).to_list(length=None)
return [ReportModelShort(**report) for report in reports]
async def delete_all_reports() -> None:
await settings.DB_CLIENT.reports.delete_many({})
async def get_report_obj_by_id(report_id: str) -> ReportModel:
report = await settings.DB_CLIENT.reports.find_one({"id": report_id})
if not report:
raise HTTPException(status_code=404, detail="Report not found")
return ReportModel.from_mongo(report)
async def save_report_obj(report: str, changes: str | None, original_text: str, filename: str) -> ReportModel:
report = ReportModel(report=report, changes=changes, filename=filename, originalText=original_text)
await settings.DB_CLIENT.reports.insert_one(report.to_mongo())
return report
async def get_last_report_obj() -> ReportModel | None:
report = await settings.DB_CLIENT.reports.find().sort("_id", -1).to_list(length=1)
return ReportModel.from_mongo(report[0]) if report else None
async def change_report_report_obj(report_id: str, data: CreateMessageRequest) -> ReportModel:
report = await settings.DB_CLIENT.reports.find_one({"id": report_id})
if not report:
raise HTTPException(status_code=404, detail="Report not found")
report = ReportModel.from_mongo(report)
report.report = data.text
await settings.DB_CLIENT.reports.update_one(
{"id": report.id},
{"$set": report.to_mongo()},
)
return report
async def change_changes_report_obj(report_id: str, data: CreateMessageRequest) -> ReportModel:
report = await settings.DB_CLIENT.reports.find_one({"id": report_id})
if not report:
raise HTTPException(status_code=404, detail="Report not found")
report = ReportModel.from_mongo(report)
report.changes = data.text
await settings.DB_CLIENT.reports.update_one(
{"id": report.id},
{"$set": report.to_mongo()},
)
return report