from fastapi import APIRouter, Depends, status from sqlalchemy.orm import Session from app.db.session import get_db from app.services.notes_service import NotesService from app.schemas.notes import CustomerNoteCreate, CustomerNoteOut, BidderNoteCreate, CustomerNoteUpdate, BidderNoteUpdate from app.schemas.project import ProjectNoteCreate, ProjectNoteOut, ProjectNoteUpdate from typing import List router = APIRouter(prefix="/api/v1/notes", tags=["notes"]) @router.get("/projects/{project_no}", response_model=List[ProjectNoteOut]) def list_project_notes(project_no: int, db: Session = Depends(get_db)): service = NotesService(db) notes = service.list_project_notes(project_no) # Map DB rows to ProjectNoteOut expected fields mapped = [] for r in notes: mapped.append({ 'note_id': r.get('ID') or r.get('NoteID'), 'project_no': r.get('ProjectNo'), 'date': r.get('Time') or r.get('Date'), 'employee_id': r.get('EmployeeID'), 'employee_name': None, 'customer_id': r.get('CustomerID'), 'customer_name': None, 'notes': r.get('Notes') }) return mapped @router.post("/projects/{project_no}", status_code=status.HTTP_201_CREATED) def create_project_note(project_no: int, note_in: ProjectNoteCreate, db: Session = Depends(get_db)): service = NotesService(db) note_id = service.create_project_note(project_no, note_in.model_dump()) return {"id": note_id} @router.put("/projects/{project_no}/{note_id}", response_model=ProjectNoteOut) def update_project_note(project_no: int, note_id: int, note_in: ProjectNoteUpdate, db: Session = Depends(get_db)): service = NotesService(db) updated = service.update_project_note(project_no, note_id, note_in.model_dump(exclude_unset=True)) # Map to response model fields return { 'note_id': updated.get('ID') or updated.get('NoteID'), 'project_no': updated.get('ProjectNo'), 'date': updated.get('Time') or updated.get('Date'), 'employee_id': updated.get('EmployeeID'), 'employee_name': None, 'customer_id': updated.get('CustomerID'), 'customer_name': None, 'notes': updated.get('Notes') } @router.delete("/projects/{project_no}/{note_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_project_note(project_no: int, note_id: int, db: Session = Depends(get_db)): service = NotesService(db) service.delete_project_note(project_no, note_id) return None @router.get("/customers/{customer_id}", response_model=List[CustomerNoteOut]) def list_customer_notes(customer_id: int, db: Session = Depends(get_db)): service = NotesService(db) notes = service.list_customer_notes(customer_id) # Map DB rows to CustomerNoteOut expected fields mapped = [] for r in notes: mapped.append({ 'note_id2': r.get('NoteID2'), 'note_id': r.get('NoteID'), 'customer_id': r.get('CustomerID'), 'date': r.get('Date'), 'employee_id': r.get('EmployeeID'), 'customer_type_id': r.get('CustomerTypeID'), 'notes': r.get('Notes') }) return mapped @router.post("/customers/{customer_id}", status_code=status.HTTP_201_CREATED) def create_customer_note(customer_id: int, note_in: CustomerNoteCreate, db: Session = Depends(get_db)): service = NotesService(db) note_id = service.create_customer_note(customer_id, note_in.model_dump()) return {"id": note_id} @router.put("/customers/{customer_id}/{note_id2}", response_model=CustomerNoteOut) def update_customer_note(customer_id: int, note_id2: int, note_in: CustomerNoteUpdate, db: Session = Depends(get_db)): service = NotesService(db) updated = service.update_customer_note(customer_id, note_id2, note_in.model_dump(exclude_unset=True)) return { 'note_id2': updated.get('NoteID2'), 'note_id': updated.get('NoteID'), 'customer_id': updated.get('CustomerID'), 'date': updated.get('Date'), 'employee_id': updated.get('EmployeeID'), 'customer_type_id': updated.get('CustomerTypeID'), 'notes': updated.get('Notes') } @router.delete("/customers/{customer_id}/{note_id2}", status_code=status.HTTP_204_NO_CONTENT) def delete_customer_note(customer_id: int, note_id2: int, db: Session = Depends(get_db)): service = NotesService(db) service.delete_customer_note(customer_id, note_id2) return None @router.get("/bidders/{bidder_id}", response_model=List[dict]) def list_bidder_notes(bidder_id: int, db: Session = Depends(get_db)): service = NotesService(db) notes = service.list_bidder_notes(bidder_id) return notes @router.post("/bidders/{bidder_id}", status_code=status.HTTP_201_CREATED) def create_bidder_note(bidder_id: int, note_in: BidderNoteCreate, db: Session = Depends(get_db)): service = NotesService(db) note_id = service.create_bidder_note(bidder_id, note_in.model_dump()) return {"id": note_id} @router.put("/bidders/{bidder_id}/{note_id}", response_model=dict) def update_bidder_note(bidder_id: int, note_id: int, note_in: BidderNoteUpdate, db: Session = Depends(get_db)): service = NotesService(db) updated = service.update_bidder_note(bidder_id, note_id, note_in.model_dump(exclude_unset=True)) return updated @router.delete("/bidders/{bidder_id}/{note_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_bidder_note(bidder_id: int, note_id: int, db: Session = Depends(get_db)): service = NotesService(db) service.delete_bidder_note(bidder_id, note_id) return None