public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _repo.getUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.getUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> SelectUser(int id, int recipientId) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var select = await _repo.GetSelect(id, recipientId); if (select != null) { return(BadRequest("You've already selected this user")); } if (await _repo.getUser(recipientId) == null) { return(NotFound()); } var checkIfPatientFirstSelect = await _repo.GetSelect(recipientId, id); var SelectorUser = await _repo.getUser(id); var SelecteeUser = await _repo.getUser(recipientId); // Check if a patient is trying to select a patient if (SelectorUser.UserRole.RoleId.Equals(1) && SelecteeUser.UserRole.RoleId.Equals(1)) { return(BadRequest("You can't select another patient")); } // Check if a doctor is trying to select a patient if (SelectorUser.UserRole.RoleId.Equals(3) && SelecteeUser.UserRole.RoleId.Equals(1)) { // Check if patient has selected the doctor first // If patient has not selected the doctor, then the doctor cannot select the patient if (checkIfPatientFirstSelect == null) { return(BadRequest("You can't select a patient first")); } } select = new Select { SelectorId = id, SelecteeId = recipientId }; _repo.Add <Select>(select); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to select user")); }
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"); }