visus-plus/api/app/controllers/appointments_router.py

124 lines
4.5 KiB
Python

from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from datetime import date
from app.database.session import get_db
from app.domain.entities.appointment import AppointmentEntity
from app.infrastructure.appointments_service import AppointmentsService
from app.infrastructure.dependencies import get_current_user
router = APIRouter()
@router.get(
"/",
response_model=list[AppointmentEntity],
summary="Get all appointments",
description="Returns a list of all appointments",
)
async def get_all_appointments(
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
start_date: date | None = Query(None, description="Start date for filtering (YYYY-MM-DD)"),
end_date: date | None = Query(None, description="End date for filtering (YYYY-MM-DD)"),
):
appointments_service = AppointmentsService(db)
return await appointments_service.get_all_appointments(start_date=start_date, end_date=end_date)
@router.get(
"/doctor/{doctor_id}/",
response_model=list[AppointmentEntity],
summary="Get all appointments for doctor",
description="Returns a list of appointments for doctor",
)
async def get_all_appointments_by_doctor_id(
doctor_id: int,
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
start_date: date | None = Query(None, description="Start date for filtering (YYYY-MM-DD)"),
end_date: date | None = Query(None, description="End date for filtering (YYYY-MM-DD)"),
):
appointments_service = AppointmentsService(db)
return await appointments_service.get_appointments_by_doctor_id(doctor_id, start_date=start_date, end_date=end_date)
@router.get(
"/doctor/{doctor_id}/upcoming/",
response_model=list[AppointmentEntity],
summary="Get upcoming appointments for doctor",
description="Returns the next 5 upcoming appointments for doctor",
)
async def get_upcoming_appointments_by_doctor_id(
doctor_id: int,
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
):
appointments_service = AppointmentsService(db)
return await appointments_service.get_upcoming_appointments_by_doctor_id(doctor_id)
@router.get(
"/reminders/",
response_model=list[AppointmentEntity],
summary="Get appointment reminders",
description="Returns a list of appointments with upcoming follow-up reminders based on days_until_the_next_appointment",
)
async def get_appointment_reminders(
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
current_date: date = Query(default_factory=date.today,
description="Current date for reminder calculation (YYYY-MM-DD)"),
):
appointments_service = AppointmentsService(db)
return await appointments_service.get_appointment_reminders(current_date)
@router.get(
"/patient/{patient_id}/",
response_model=list[AppointmentEntity],
summary="Get all appointments for patient",
description="Returns a list of appointments for patient",
)
async def get_all_appointments_by_patient_id(
patient_id: int,
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
start_date: date | None = Query(None, description="Start date for filtering (YYYY-MM-DD)"),
end_date: date | None = Query(None, description="End date for filtering (YYYY-MM-DD)"),
):
appointments_service = AppointmentsService(db)
return await appointments_service.get_appointments_by_patient_id(patient_id, start_date=start_date,
end_date=end_date)
@router.post(
"/",
response_model=AppointmentEntity,
summary="Create appointment",
description="Creates a new appointment",
)
async def create_appointment(
appointment: AppointmentEntity,
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
):
appointment_service = AppointmentsService(db)
return await appointment_service.create_appointment(appointment, user.id)
@router.put(
"/{appointment_id}/",
response_model=AppointmentEntity,
summary="Update appointment",
description="Updates an existing appointment",
)
async def update_appointment(
appointment_id: int,
appointment: AppointmentEntity,
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
):
appointment_service = AppointmentsService(db)
return await appointment_service.update_appointment(appointment_id, appointment)