public async Task<IActionResult> Create([FromBody]AppointmentModel model) { var bookingModel = new AppointmentBookingModel { StartTime = model.Start, EndTime = model.End, SelectedServices = model.Services, SelectedEsthetician = new EstheticianViewModel { Id = model.EstheticianId }, NumberOfGuests = 1, PhoneNumber = model.Description, FirstName = model.FirstName, Gender = model.Gender, SelectedLocationId = model.LocationId }; var result = await _appointmentService.BookAppointmentAsync(bookingModel); if (result != null && _env.IsProduction()) { await _notificationService.SendNewAppointmentInfoToEsthetician(result.Id); } return result != null ? (IActionResult)Ok(GenerateApptModel(result)) : BadRequest("Username already taken."); }
public async Task<Appointment> BookAppointmentAsync(AppointmentBookingModel model) { var esth = _estheticians.GetByIdAsync(model.SelectedEsthetician.Id, x => x.User); var services = await _spaServices.FindAsync(x => model.SelectedServices.Select(o => o.Id).Contains(x.Id)); Client client = null; //Create the user if they added a password if (model.Password != null) { var clientUser = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber }; await Task.FromResult(_users.CreateAsync(clientUser, model.Password).Result); client = new Client { ApplicationUserId = _users.FindByEmailAsync(model.Email).Result.Id, AppointmentRemindersViaText = model.ReminderViaText, AppointmentRemindersViaEmail = model.ReminderViaEmail, Appointments = new List<Appointment>() }; } var appt = new Appointment { Services = services.ToList(), Esthetician = await esth, StartTime = model.StartTime.ToUniversalTime(), EndTime = model.EndTime.ToUniversalTime(), RemindViaEmail = model.ReminderViaEmail, RemindViaText = model.ReminderViaText, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, PhoneNumber = model.PhoneNumber, LocationId = model.SelectedLocationId, Gender = model.Gender }; //appt.Cost = appt.Services.Sum(x => x.Cost); //link the created user, if any if (client != null) { appt.Client = client; } try { return await _appointments.AddAsync(appt); } catch (Exception ex) { throw ex; } }