from fastapi import Form, APIRouter, File, UploadFile, HTTPException, Request from db.repository import get_db_conn from db.get_data import GetDatabase from db.save_data import InsertDatabase from config import MYSQL_CONFIG from api.function import data_ingestion, get_data, delete_data, update_data from script.vector_db import IndexManager from service.dto import MetadataRequest router = APIRouter(tags=["Topics"]) db_conn = get_db_conn(MYSQL_CONFIG) get_database = GetDatabase(db_conn) index_manager = IndexManager() @router.post("/topic") async def upload_file( title: str = Form(...), author: str = Form(...), category: str = Form(...), year: int = Form(...), publisher: str = Form(...), file: UploadFile = File(...), # content_table: UploadFile = File(...) ): reference = { "title": title, "author": author, "category": category, "year": year, "publisher": publisher, } # response = await data_ingestion(db_conn, reference, file, content_table) response = await data_ingestion(db_conn, reference, file) return {"filename": file.filename, "response": response} @router.get("/topic") async def get_metadata(): results = await get_data(db_conn) return results @router.put("/topic/{id}") async def update_metadata(id: int, reference: MetadataRequest): try : old_reference = await get_database.get_data_by_id(id) index_manager.update_vector_database(old_reference, reference) return await update_data(id, reference, db_conn) except Exception as e: raise HTTPException(status_code=500, detail="An error occurred while updating metadata") @router.delete("/topic/{id}") async def delete_metadata(id: int): try: old_reference = await get_database.get_data_by_id(id) index_manager.delete_vector_database(old_reference) return await delete_data(id, db_conn) except Exception as e: print(e) raise HTTPException(status_code=500, detail="An error occurred while delete metadata")