[Authorize]// only the users with a token can access this method public async Task <ActionResult <Appointments> > createAppointment(CreateAppointmentDTO app) { if (await AppointmentDateExist(app.date)) { if (await AppointmentHourExist(app.hour)) { return(BadRequest("This date is already used!")); } } var useremail = User.FindFirst(ClaimTypes.Email)?.Value; var user = await _context.USERS.Where(x => x.email == useremail).FirstOrDefaultAsync(); var Appointment = new Appointments { date = app.date, hour = app.hour, UserId = user.Id }; _context.APPOINTMENTS.Add(Appointment); await _context.SaveChangesAsync(); return(Ok(_mapper.Map <ReturnAppointmentsDTO>(Appointment))); }
public async Task <ActionResult <CreateAppointmentDTO> > SaveAppointment([FromBody] CreateAppointmentDTO appointmentDto) { var identity = HttpContext.User.Identity as ClaimsIdentity; IEnumerable <Claim> claims = identity.Claims; var userId = claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; return(await _appointmentService.SaveAppointment(appointmentDto, Int32.Parse(userId))); }
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 void CallCreateAppointmentInRepository() { CreateAppointmentDTO createAppointmentDTO = new CreateAppointmentDTO() { StaffId = "a test", ListingId = 1, Time = It.IsAny <DateTime>() }; var result = await sut.Create(createAppointmentDTO); var vr = Assert.IsType <RedirectToActionResult>(result); fixture.repositoryWrapper.Verify(x => x.Appointment.CreateAppointment(It.IsAny <Appointment>()), Times.Once); fixture.repositoryWrapper.Verify(y => y.SaveAsync(), Times.Once); Assert.Equal("Index", vr.ActionName); }
public async Task <IActionResult> Create() { List <SelectListItem> list = await ParseListingForSelectList(); CreateAppointmentDTO app = new CreateAppointmentDTO() { StaffId = await _repositoryWrapper.Employee.GetUserId(User) }; ViewBag.listing = list; ViewData["UserId"] = await _repositoryWrapper.Employee.GetUserId(User); return(View(nameof(Create), app)); }
public async Task <IActionResult> Create(CreateAppointmentDTO appointment) { if (ModelState.IsValid) { Appointment app = _mapper.Map <Appointment>(appointment); _repositoryWrapper.Appointment.CreateAppointment(app); await _repositoryWrapper.SaveAsync(); return(RedirectToAction(nameof(Index))); } return(View(nameof(Create), appointment)); }
public async Task <IActionResult> CreateAppointment(int id) { var userId = await GetCurrentUserId(); CreateAppointmentDTO createAppointmentDTO = new CreateAppointmentDTO(); createAppointmentDTO.CurrentPsychologist = _unitOfWork.Psychologists.Get(id); createAppointmentDTO.CurrentAuthorizedUser = _unitOfWork.AuthorizedUsers.Get(userId); createAppointmentDTO.Psychologists = new List <Psychologist>(); //_unitOfWork.Psychologists.GetAll().ToList(); createAppointmentDTO.AuthorizedUsers = new List <AuthorizedUser>(); //_unitOfWork.AuthorizedUsers.GetAll().ToList(); return(PartialView(createAppointmentDTO)); }
public async void ReturnViewIfModelStateInvalid() { sut.ModelState.AddModelError(string.Empty, "Invalid AppointmentDTO"); CreateAppointmentDTO createAppointmentDTO = new CreateAppointmentDTO() { }; var result = await sut.Create(createAppointmentDTO); var vr = Assert.IsType <ViewResult>(result); fixture.repositoryWrapper.Verify(x => x.Appointment.CreateAppointment(It.IsAny <Appointment>()), Times.Never); fixture.repositoryWrapper.Verify(y => y.SaveAsync(), Times.Never); Assert.Equal("Create", vr.ViewName); Assert.IsType <CreateAppointmentDTO>(vr.Model); }
public IActionResult CreateAppointment(CreateAppointmentDTO model) { string _status = String.Empty; string _description = String.Empty; List <Appointment> appointments = _unitOfWork.Psychologists.Get(model.PsychologistId).Appointments.ToList(); foreach (var elem in appointments) { if ((model.StartDateTime >= elem.StartDataTime && model.StartDateTime <= elem.EndDataTime) || (model.EndDateTime >= elem.StartDataTime && model.EndDateTime <= elem.EndDataTime) || (elem.StartDataTime >= model.StartDateTime && elem.EndDataTime <= model.EndDateTime)) { _status = "error"; _description = "Psychologist is busy at that time."; break; } } if (_status != "error") { Appointment appointment = new Appointment { PsychologistId = model.PsychologistId, AuthorizedUserId = model.AuthorizedUserId, StartDataTime = model.StartDateTime, EndDataTime = model.EndDateTime, AdditionalInfo = model.Info }; _unitOfWork.Appointments.Add(appointment); int result = _unitOfWork.Complete(); _status = "success"; _description = "Appointment created."; } //if (User.IsInRole("Doctor")) { //return RedirectToAction("DashboardDoctor", "User", new { showAll = false }); } //else { //return RedirectToAction("DashboardPatient", "User", new { showAll = false }); } return(Json(new { status = _status, description = _description })); }
public async Task <IActionResult> CreateAppointment() { var userId = await GetCurrentUserId(); CreateAppointmentDTO createAppointmentDTO = new CreateAppointmentDTO(); if (User.IsInRole("Psychologist")) { createAppointmentDTO.CurrentPsychologist = _unitOfWork.Psychologists.Get(userId); } else if (User.IsInRole("AuthorizedUser")) { createAppointmentDTO.CurrentAuthorizedUser = _unitOfWork.AuthorizedUsers.Get(userId); } createAppointmentDTO.Psychologists = _unitOfWork.Psychologists.GetAll().ToList(); createAppointmentDTO.AuthorizedUsers = _unitOfWork.AuthorizedUsers.GetAll().ToList(); return(PartialView(createAppointmentDTO)); }
public IActionResult CreateAppointment(CreateAppointmentDTO model) { string _status = String.Empty; string _description = String.Empty; List <Appointment> appointments = _unitOfWork.Psychologists.Get(model.PsychologistId).Appointments.ToList(); string currDayOfWeek = DateTime.Now.DayOfWeek.ToString(); Day day = (Day)Enum.Parse(typeof(Day), currDayOfWeek); //var kek = model.CurrentPsychologist.WorkSchedules.Where(s => s.Day == day).FirstOrDefault(); var kek = _unitOfWork.Psychologists.Get(model.PsychologistId).WorkSchedules.Where(s => s.Day == day).FirstOrDefault(); foreach (var elem in appointments) { if ((model.StartDateTime >= elem.StartDataTime && model.StartDateTime <= elem.EndDataTime) || (model.EndDateTime >= elem.StartDataTime && model.EndDateTime <= elem.EndDataTime) || (elem.StartDataTime >= model.StartDateTime && elem.EndDataTime <= model.EndDateTime)) { _status = "error"; _description = "Psychologist is busy at that time."; break; } } if (_status != "error") { if (!(model.StartDateTime.Value.Hour >= kek.StartTime.Hours && model.StartDateTime.Value.Minute >= kek.StartTime.Minutes && model.EndDateTime.Value.Hour < kek.EndTime.Hours)) { _status = "error"; _description = "Psychologist does not work at this time."; } } if (_status != "error") { Appointment appointment = new Appointment { PsychologistId = model.PsychologistId, AuthorizedUserId = model.AuthorizedUserId, StartDataTime = model.StartDateTime, EndDataTime = model.EndDateTime, AdditionalInfo = model.Info }; _unitOfWork.Appointments.Add(appointment); int result = _unitOfWork.Complete(); _status = "success"; _description = "Appointment created."; } //if (User.IsInRole("Doctor")) { //return RedirectToAction("DashboardDoctor", "User", new { showAll = false }); } //else { //return RedirectToAction("DashboardPatient", "User", new { showAll = false }); } return(Json(new { status = _status, description = _description })); }