test-1 / api /endpoints.py
vmoras's picture
Initial commit
7083742
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}