private async void Create(Appointment appointment) { if (appointment != null) { await appointmentRepository.AddAsync(appointment); BuscarTodos(); } }
public async Task <CreateAppointmentDTO> SaveAppointment(CreateAppointmentDTO appointmentDto, int userId) { var result = _validator.Validate(appointmentDto); if (result.Errors.Count() > 0) { throw new HttpResponseException(HttpStatusCode.BadRequest, result.Errors); } User isProvider = await _userRepository.GetByIdAsync(appointmentDto.ProviderId); if (isProvider == null || !isProvider.Provider) { throw new HttpResponseException(HttpStatusCode.Unauthorized, new { Message = "Você só pode criar agendamentos para provedores" }); } var date = appointmentDto.Date.Value; var hourStart = new DateTime(date.Year, date.Month, date.Day, date.Hour, 0, 0); if (hourStart < DateTime.Now) { throw new HttpResponseException(HttpStatusCode.BadRequest, new { Message = "Não é permitido horário anterior ao atual" }); } Expression <Func <Appointment, bool> > predicate = a => a.Provider.Id == appointmentDto.ProviderId && !a.CanceledAt.HasValue && a.Date.Value == hourStart; var checkAvailability = await _repository.GetAsync(predicate); if (checkAvailability.Count() > 0) { throw new HttpResponseException(HttpStatusCode.BadRequest, new { Message = "Horário não está disponivel" }); } Appointment appointment = _mapper.Map <Appointment>(appointmentDto); appointment.Provider = isProvider; User user = await _userRepository.GetByIdAsync(userId); appointment.User = user; Appointment appointmentSaved = await _repository.AddAsync(appointment); await _notificationRepository.Create(new Notification { Content = $"Novo agendamento de {user.Name} para o dia {appointmentDto.Date.Value.Day}/{appointmentDto.Date.Value.Month}/{appointmentDto.Date.Value.Year} {appointmentDto.Date.Value.Hour}:{appointmentDto.Date.Value.Minute}", UserId = appointmentDto.ProviderId }); return(_mapper.Map <CreateAppointmentDTO>(appointmentSaved)); }
public async Task <AppointmentResponse> SaveAsync(Appointment appointment) { try { await _appointmentRepository.AddAsync(appointment); await _unitOfWork.CompleteAsync(); return(new AppointmentResponse(appointment)); } catch (Exception ex) { return(new AppointmentResponse($"An error ocurred when while saving appointment: {ex.Message}")); } }
public async Task <ActionResult <int> > Post([FromBody] AppointmentDto appointment) { int recordId; try { recordId = await _appointmentRepository.AddAsync(appointment); } catch (ArgumentException) { return(BadRequest()); } return(recordId); }
public async Task <Unit> Handle(CreateAppointmentCommand request, CancellationToken cancellationToken) { var calendar = await _calendarRepository.GetByIdAsync(new CalendarId(request.CalendarId)); var appoitment = calendar .AddAppoitment(AppointmentTerm.CreateNewStartAndEnd( request.StartAppoitment, request.EndAppoitment), request.PatientId); await _appointmentRepository.AddAsync(appoitment); await _appointmentRepository.Save(); return(Unit.Value); }
/// <summary> /// Handle /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <Unit> Handle(BookAppointmentCommand request, CancellationToken cancellationToken) { var equipmentAvailable = await _equipmentService.GetEquipmentAvailableOnAppointmentDateAsync(request.AppointmentDate, request.StartTime, request.EndTime); var equipmentId = equipmentAvailable != null ? equipmentAvailable.EquipmentId : 0; var appointment = Appointment.BookAppointment(request.PatientId, equipmentId, request.ReferenceCode, request.AppointmentDate, request.StartTime, request.EndTime, _appointmentEquipmentIsAvailableValidator, _appointmentPatientMustExistRuleValidator); await _appointmentRepository.AddAsync(appointment); // raise events var patient = await _repository.GetByScalarValueAsync(new { Id = request.PatientId }); // add to outbox for processing await _mediator.Publish(AppointmentBookedEvent.Create(appointment.Id, request.ReferenceCode, $"{patient.FirstName} {patient.LastName}", patient.EmailAddress), cancellationToken); await _equipmentService.MarkAsUnAvailable(equipmentAvailable); // mark item as unavailable item from the cached list return(Unit.Value); }
public async Task <AppointmentResponse> SaveAsync(int ownerId, int veterinaryId, int vetId, int petId, Appointment appointment) { var existingOwner = await _ownerProfileRepository.FindById(ownerId); var existingVeterinary = await _veterinaryProfileRepository.FindById(veterinaryId); var existingVet = await _vetProfileRepository.FindById(vetId); var existingPet = await _petProfileRepository.FindById(petId); var existingScheduleVet = await _scheduleRepository.FindByProfileId(veterinaryId); var existingScheduleOwner = await _scheduleRepository.FindByProfileId(ownerId); if (existingOwner == null) { return(new AppointmentResponse("Owner not found")); } if (existingPet == null) { return(new AppointmentResponse("Pet not found")); } if (existingVet == null) { return(new AppointmentResponse("Vet not found")); } if (existingVeterinary == null) { return(new AppointmentResponse("Veterinary not found")); } try { bool differentDate = true; IEnumerable <Appointment> appointments = await _appointmentRepository.ListByScheduleId(existingScheduleVet.First().Id); appointments.ToList().ForEach(appoint => { if ((appoint.Date == appointment.Date) && appoint.Accepted == true) { differentDate = false; } }); if (differentDate == false) { return(new AppointmentResponse("The vet already have an appointment at the date, please try a different date")); } appointment.OwnerId = ownerId; appointment.VeterinaryId = veterinaryId; appointment.VetId = vetId; appointment.PetId = petId; appointment.ScheduleId = existingScheduleOwner.First().Id; await _appointmentRepository.AddAsync(appointment); await _unitOfWork.CompleteAsync(); appointment.ScheduleId = existingScheduleVet.First().Id; await _appointmentRepository.AddAsync(appointment); await _unitOfWork.CompleteAsync(); return(new AppointmentResponse(appointment)); } catch (Exception ex) { return(new AppointmentResponse($"An error ocurred while updating appointment: {ex.Message}")); } }
public async Task <IActionResult> CreateAppointmentAsync(AppointmentDto appointmentDto, HttpRequest request) { var doctor = await doctorRepository.GetAsync(appointmentDto.DoctorId); if (doctor == null) { return(new JsonResult(new ExceptionDto { Message = "No doctor with given permit number was found" }) { StatusCode = 422 }); } var patient = await patientRepository.GetAsync(appointmentDto.PatientId); if (patient == null) { return(new JsonResult(new ExceptionDto { Message = "No patient with given identity number was found" }) { StatusCode = 422 }); } var timeNow = DateTime.Now; if (appointmentDto.ScheduledDate < timeNow) { return(new JsonResult(new ExceptionDto { Message = "Given registration date is invalid" }) { StatusCode = 422 }); } if (await userService.GetCurrentUserAsync(request) is not Registrar registrar) { return(new JsonResult(new ExceptionDto { Message = "Could not find registrar" }) { StatusCode = 422 }); } var appointment = new Appointment { RegistrationDate = timeNow, Description = appointmentDto.Description, Patient = patient, Doctor = doctor, Status = AppointmentStatus.Scheduled, ScheduledDate = appointmentDto.ScheduledDate, Registrar = registrar }; return(new JsonResult(await appointmentRepository.AddAsync(appointment)) { StatusCode = 201 }); }