from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app.database import get_db from app.models import Document, DocumentFile, DocumentDispatch from app.schemas import DocumentCreate, DocumentUpdate, DocumentFileCreate, DocumentFileUpdate, DocumentDispatchCreate, DocumentDispatchUpdate router = APIRouter() # Document routes @router.get("/documents") async def get_documents(db: Session = Depends(get_db)): documents = db.query(Document).all() return documents @router.get("/documents/{document_id}") async def get_document(document_id: int, db: Session = Depends(get_db)): document = db.query(Document).filter(Document.id == document_id).first() if not document: raise HTTPException(status_code=404, detail="Document not found") return document @router.post("/documents") async def create_document(document: DocumentCreate, db: Session = Depends(get_db)): db_document = Document(**document.dict()) db.add(db_document) db.commit() db.refresh(db_document) return db_document @router.put("/documents/{document_id}") async def update_document(document_id: int, document: DocumentUpdate, db: Session = Depends(get_db)): db_document = db.query(Document).filter(Document.id == document_id).first() if not db_document: raise HTTPException(status_code=404, detail="Document not found") for key, value in document.dict(exclude_unset=True).items(): setattr(db_document, key, value) db.commit() db.refresh(db_document) return db_document @router.delete("/documents/{document_id}") async def delete_document(document_id: int, db: Session = Depends(get_db)): db_document = db.query(Document).filter(Document.id == document_id).first() if not db_document: raise HTTPException(status_code=404, detail="Document not found") db.delete(db_document) db.commit() return {"message": "Document deleted successfully"} # DocumentFile routes @router.get("/document_files") async def get_document_files(db: Session = Depends(get_db)): files = db.query(DocumentFile).all() return files @router.get("/document_files/{file_id}") async def get_document_file(file_id: int, db: Session = Depends(get_db)): file = db.query(DocumentFile).filter(DocumentFile.id == file_id).first() if not file: raise HTTPException(status_code=404, detail="Document file not found") return file @router.post("/document_files") async def create_document_file(file: DocumentFileCreate, db: Session = Depends(get_db)): db_file = DocumentFile(**file.dict()) db.add(db_file) db.commit() db.refresh(db_file) return db_file @router.put("/document_files/{file_id}") async def update_document_file(file_id: int, file: DocumentFileUpdate, db: Session = Depends(get_db)): db_file = db.query(DocumentFile).filter(DocumentFile.id == file_id).first() if not db_file: raise HTTPException(status_code=404, detail="Document file not found") for key, value in file.dict(exclude_unset=True).items(): setattr(db_file, key, value) db.commit() db.refresh(db_file) return db_file @router.delete("/document_files/{file_id}") async def delete_document_file(file_id: int, db: Session = Depends(get_db)): db_file = db.query(DocumentFile).filter(DocumentFile.id == file_id).first() if not db_file: raise HTTPException(status_code=404, detail="Document file not found") db.delete(db_file) db.commit() return {"message": "Document file deleted successfully"} # DocumentDispatch routes @router.get("/document_dispatches") async def get_document_dispatches(db: Session = Depends(get_db)): dispatches = db.query(DocumentDispatch).all() return dispatches @router.get("/document_dispatches/{dispatch_id}") async def get_document_dispatch(dispatch_id: int, db: Session = Depends(get_db)): dispatch = db.query(DocumentDispatch).filter(DocumentDispatch.id == dispatch_id).first() if not dispatch: raise HTTPException(status_code=404, detail="Document dispatch not found") return dispatch @router.post("/document_dispatches") async def create_document_dispatch(dispatch: DocumentDispatchCreate, db: Session = Depends(get_db)): db_dispatch = DocumentDispatch(**dispatch.dict()) db.add(db_dispatch) db.commit() db.refresh(db_dispatch) return db_dispatch @router.put("/document_dispatches/{dispatch_id}") async def update_document_dispatch(dispatch_id: int, dispatch: DocumentDispatchUpdate, db: Session = Depends(get_db)): db_dispatch = db.query(DocumentDispatch).filter(DocumentDispatch.id == dispatch_id).first() if not db_dispatch: raise HTTPException(status_code=404, detail="Document dispatch not found") for key, value in dispatch.dict(exclude_unset=True).items(): setattr(db_dispatch, key, value) db.commit() db.refresh(db_dispatch) return db_dispatch @router.delete("/document_dispatches/{dispatch_id}") async def delete_document_dispatch(dispatch_id: int, db: Session = Depends(get_db)): db_dispatch = db.query(DocumentDispatch).filter(DocumentDispatch.id == dispatch_id).first() if not db_dispatch: raise HTTPException(status_code=404, detail="Document dispatch not found") db.delete(db_dispatch) db.commit() return {"message": "Document dispatch deleted successfully"}