File size: 2,170 Bytes
7083742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, Request, HTTPException

from api.utils import check_missing_fields
from services.appointment_manager import AppointmentManager


router = APIRouter()
appointment_manager = AppointmentManager()


@router.post("/appointments/", response_model=dict)
async def create_appointment(request: Request):
    """
    Create a new appointment and save it on the json files.
    """
    try:
        data = await request.json()
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}")

    # Make sure the request has the user and appointment details
    missing_fields = check_missing_fields(['user', 'full_date'], data)
    if missing_fields:
        raise HTTPException(status_code=400, detail=f"Missing fields: {', '.join(missing_fields)}")

    # Validate subfields for user and appointment date
    user_data, date_data = data["user"], data["full_date"]
    missing_user_fields = check_missing_fields(['identification', 'name', 'last_name', 'age'], user_data)
    if missing_user_fields:
        raise HTTPException(status_code=400, detail=f"Missing fields: {', '.join(missing_user_fields)}")
    missing_date_fields = check_missing_fields(['date', 'hour'], date_data)
    if missing_date_fields:
        raise HTTPException(status_code=400, detail=f"Missing fields: {', '.join(missing_date_fields)}")

    # Create appointment
    appointment_manager.create_appointment(user_data, date_data)

    return {'Status': 'Success'}


@router.get("/appointments/available", response_model=dict)
async def get_available_appointments():
    """
    Get all the dates and hours available for appointments.
    """
    return {'available_dates': appointment_manager.get_available_appointments()}

@router.get("/appointments/reserved", response_model=dict)
async def get_reserved_days():
    """
    Get all the dates and hours reserved for appointments.
    """
    return {'reserved_days': appointment_manager.get_reserved_days()}

@router.get("/appointments/{date}")
async def get_appointment(date: str):
    appointments = appointment_manager.get_appointments(date)
    return {'appointments': appointments}