示例#1
0
        public async Task <IActionResult> CreateAppointment(int patientId,
                                                            [FromBody] AppointmentForCreationDto appointmentForCreation)
        {
            if (appointmentForCreation == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new appointment");
                return(BadRequest(ModelState));
            }

            var command = new AddAppointmentCommand(patientId, appointmentForCreation.AppointmentDate, appointmentForCreation.Reason);

            _logger.LogInformation(
                $"----- Sending command: AddAppointmentCommand - {command.PatientId} - {command.AppointmentDate}");

            var commandResult = await _mediator.Send(command);

            if (commandResult == null)
            {
                return(BadRequest("Command not created"));
            }

            return(CreatedAtAction("GetAppointmentByIdentifier",
                                   new
            {
                patientId,
                id = commandResult.Id
            }, commandResult));
        }
        public async Task <IActionResult> CreateAppointment(int userId, AppointmentForCreationDto apptForCreation)
        {
            var apptCreator = await _repo.getUser(userId);

            if (apptCreator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var apptRecipient = (User)null;

            if (apptCreator.UserRole.RoleId.Equals(1))
            {
                apptForCreation.PatientId = userId;

                apptRecipient = await _repo.getUser(apptForCreation.DoctorId);

                if (apptRecipient == null)
                {
                    return(BadRequest("Could not find Doctor"));
                }
            }
            else if (apptCreator.UserRole.RoleId.Equals(3))
            {
                apptForCreation.DoctorId = userId;

                apptRecipient = await _repo.getUser(apptForCreation.PatientId);

                if (apptRecipient == null)
                {
                    return(BadRequest("Could not find Patient"));
                }
            }
            //messageForCreationDto.SenderId = userId;
            // var apptRecipient = await _repo.getUser(apptForCreation.RecipientId);
            // if (recipient == null) {
            //     return BadRequest("Could not find user");
            // }
            var appointment = _mapper.Map <Appointment>(apptForCreation);

            appointment.Patient = await _repo.getUser(appointment.PatientId);

            appointment.Doctor = await _repo.getUser(appointment.DoctorId);


            // CHECK IF APPOINTMENT ALREADY EXISTS ON THIS DATE
            UserParams userParams = new UserParams();

            userParams.UserId = appointment.PatientId;
            var patientApptsGet = await _repo.GetAppointmentsForUser(userParams);

            for (int i = 0; i < patientApptsGet.Count; i++)
            {
                var appt = _mapper.Map <AppointmentToReturnDto>(appointment);
                if (patientApptsGet[i].StartDate.CompareTo(appt.StartDate) == 0)
                {
                    return(BadRequest("This date is unavailable."));
                }
            }

            userParams.UserId = appointment.DoctorId;
            var doctorApptsGet = await _repo.GetAppointmentsForUser(userParams);

            for (int i = 0; i < doctorApptsGet.Count; i++)
            {
                var appt = _mapper.Map <AppointmentToReturnDto>(appointment);
                if (doctorApptsGet[i].StartDate.CompareTo(appt.StartDate) == 0)
                {
                    return(BadRequest("This date is unavailable."));
                }
            }



            _repo.Add(appointment);

            if (await _repo.SaveAll())
            {
                var apptToReturn = _mapper.Map <AppointmentToReturnDto>(appointment);
                return(CreatedAtRoute("GetAppointment", new { id = appointment.Id }, apptToReturn)); //, messageToReturn);
            }
            throw new Exception("Creating the appointment failed on save");
        }