// GET: Booking/Create public ActionResult Create(int? therapistId,DateTime? date) { CreateBookingViewModel model = new CreateBookingViewModel { TherapistsSelectListItems = GetTherapists(), TreatmentsSelectListItems = therapistId == null ? GetTreatments() : GetTreatments(therapistId.Value) }; if (date == null) { date = DateTime.Today; model.date = date.Value; } model.WeekFreeTimes = new List<DayTimeSlotViewModel>(); if (therapistId != null) { int week = CalendarService.GetWeekOfYear(date.Value); int year = date.Value.Year; DateTime firsDay = CalendarService.FirstDateOfWeekISO8601(year, week); IEnumerable<IEnumerable<ITimeSlot>> freeTimeSlotsForTherapist = factory.TimeSlotGateway.GetFreeTimeSlotsForTherapist( therapistId.Value, week, year); for (int i = 0; i < 7; i++) { DayTimeSlotViewModel dayTimeSlotViewModel = new DayTimeSlotViewModel(); dayTimeSlotViewModel.TimeSlots = new List<ITimeSlot>(); dayTimeSlotViewModel.Date = firsDay.AddDays(i); dayTimeSlotViewModel.TimeSlots = freeTimeSlotsForTherapist.ElementAt(i).ToList(); model.WeekFreeTimes.Add(dayTimeSlotViewModel); } } else { for (int i = 0; i < 7; i++) { DayTimeSlotViewModel dayTimeSlotViewModel = new DayTimeSlotViewModel(); dayTimeSlotViewModel.TimeSlots = new List<ITimeSlot>(); model.WeekFreeTimes.Add(dayTimeSlotViewModel); } } return View(model); }
public ActionResult Create(CreateBookingViewModel model) { try { Booking booking = new Booking(); booking.BookingDate = DateTime.Now; booking.DateTime = model.date .AddHours(model.Time.Hour) .AddMinutes(model.Time.Minute); booking.CustomerProfileId = User.Identity.GetUserId<int>(); booking.TherapistId = model.therapistId; booking.Treatments = new List<Treatment>(); foreach (int treatmendId in model.SelectedTreatmentsId) { booking.Treatments.Add( factory.TreatmentGateway.GetOne(treatmendId)); } factory.BookingGateway.CreateOne(booking); return RedirectToAction("Index",new { userId = User.Identity.GetUserId<int>()}); } catch { return View(); } }