public async Task <IActionResult> AddAppointment([FromBody] AppointmentForCreationDTO appointment) { try { if (appointment == null) { return(BadRequest("Appointment object is null")); } if (!ModelState.IsValid) { return(BadRequest("Invalid model object")); } var canAdd = await _businessLogic.CanAddAsync(appointment.PatientId, appointment.Date); if (!canAdd) { return(BadRequest("You cannot create another appointment for the same patient on the same day.")); } var appointmentEntity = _mapper.Map <Appointment>(appointment); appointmentEntity.Active = true; _repository.Appointment.AddAppointment(appointmentEntity); await _repository.SaveAsync(); var createdAppointment = _mapper.Map <AppointmentDTO>(appointmentEntity); return(Ok(createdAppointment)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Internal server error")); } }