102 lines
4.0 KiB
Python
102 lines
4.0 KiB
Python
from typing import Optional
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from starlette import status
|
|
|
|
from app.application.appointment_types_repository import AppointmentTypesRepository
|
|
from app.application.patients_repository import PatientsRepository
|
|
from app.application.scheduled_appointments_repository import ScheduledAppointmentsRepository
|
|
from app.application.users_repository import UsersRepository
|
|
from app.domain.entities.scheduled_appointment import ScheduledAppointmentEntity
|
|
from app.domain.models import ScheduledAppointment
|
|
|
|
|
|
class ScheduledAppointmentsService:
|
|
def __init__(self, db: AsyncSession):
|
|
self.scheduled_appointment_repository = ScheduledAppointmentsRepository(db)
|
|
self.appointment_types_repository = AppointmentTypesRepository(db)
|
|
self.users_repository = UsersRepository(db)
|
|
self.patients_repository = PatientsRepository(db)
|
|
|
|
async def get_all_scheduled_appointments(self) -> list[ScheduledAppointmentEntity]:
|
|
scheduled_appointments = await self.scheduled_appointment_repository.get_all()
|
|
|
|
return [
|
|
self.model_to_entity(scheduled_appointment)
|
|
for scheduled_appointment in scheduled_appointments
|
|
]
|
|
|
|
async def get_scheduled_appointments_by_doctor_id(self, doctor_id: int) -> Optional[
|
|
list[ScheduledAppointmentEntity]
|
|
]:
|
|
doctor = self.users_repository.get_by_id(doctor_id)
|
|
|
|
if not doctor:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail='The doctor with this ID was not found',
|
|
)
|
|
|
|
scheduled_appointments = await self.scheduled_appointment_repository.get_by_doctor_id(doctor_id)
|
|
|
|
return [
|
|
self.model_to_entity(scheduled_appointment)
|
|
for scheduled_appointment in scheduled_appointments
|
|
]
|
|
|
|
async def get_scheduled_appointments_by_patient_id(self, patient_id: int) -> Optional[
|
|
list[ScheduledAppointmentEntity]
|
|
]:
|
|
patient = await self.patients_repository.get_by_id(patient_id)
|
|
|
|
if not patient:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail='The patient with this ID was not found',
|
|
)
|
|
|
|
scheduled_appointments = await self.scheduled_appointment_repository.get_by_patient_id(patient_id)
|
|
|
|
return [
|
|
self.model_to_entity(scheduled_appointment)
|
|
for scheduled_appointment in scheduled_appointments
|
|
]
|
|
|
|
|
|
|
|
@staticmethod
|
|
def entity_to_model(scheduled_appointment: ScheduledAppointmentEntity) -> ScheduledAppointment:
|
|
scheduled_appointment_model = ScheduledAppointment(
|
|
scheduled_datetime=scheduled_appointment.scheduled_datetime,
|
|
patient_id=scheduled_appointment.patient_id,
|
|
doctor_id=scheduled_appointment.doctor_id,
|
|
type_id=scheduled_appointment.type_id,
|
|
)
|
|
|
|
if scheduled_appointment.id:
|
|
scheduled_appointment_model.id = scheduled_appointment.id
|
|
|
|
return scheduled_appointment_model
|
|
|
|
@staticmethod
|
|
def model_to_entity(scheduled_appointment: ScheduledAppointment) -> ScheduledAppointmentEntity:
|
|
scheduled_appointment_entity = ScheduledAppointmentEntity(
|
|
id=scheduled_appointment.id,
|
|
scheduled_datetime=scheduled_appointment.scheduled_datetime,
|
|
patient_id=scheduled_appointment.patient_id,
|
|
doctor_id=scheduled_appointment.doctor_id,
|
|
type_id=scheduled_appointment.type_id,
|
|
)
|
|
|
|
if scheduled_appointment.patient is not None:
|
|
scheduled_appointment_entity.patient = scheduled_appointment.patient
|
|
|
|
if scheduled_appointment.doctor is not None:
|
|
scheduled_appointment_entity.doctor = scheduled_appointment.doctor
|
|
|
|
if scheduled_appointment.type is not None:
|
|
scheduled_appointment_entity.type = scheduled_appointment.type
|
|
|
|
return scheduled_appointment_entity
|