File size: 5,653 Bytes
f8eac62
 
 
 
e0f68a3
 
f8eac62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f68a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8eac62
 
 
 
5cb6441
 
 
 
 
 
 
 
 
 
 
 
 
f8eac62
 
 
 
 
 
 
 
 
e0f68a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8eac62
 
 
 
 
 
 
 
 
 
 
 
e0f68a3
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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