Beispiel #1
0
        public ActionResult Create(AppointmentViewModel createAppointmentViewModel)
        {
            if (createAppointmentViewModel.IsCancelled && createAppointmentViewModel.Id != 0)
            {
                ModelState.AddModelError("", "Cannot edit an cancelled appointment");
            }

            if (appointmentService.PreValidate())
            {
                Appointment appointmentInput = new Appointment();
                bool        isEdit           = false;
                if (createAppointmentViewModel.Id != 0)
                {
                    appointmentInput.Id = createAppointmentViewModel.Id;
                    appointmentInput    = appointmentService.GetAll(x => x.Id == createAppointmentViewModel.Id).FirstOrDefault();
                    isEdit = true;
                }
                else
                {
                    appointmentInput.UserId = LoginUserSession.Current.UserId;
                }

                appointmentInput.StartDateTime = DateTime.ParseExact(createAppointmentViewModel.StartDateTime, "dd/MM/yyyy HH:mm", null);
                List <Activity> chosenActivities = new List <Activity>();
                foreach (int activityId in createAppointmentViewModel.chosenActivitiesIds)
                {
                    chosenActivities.Add(activityService.GetAll(x => x.Id == activityId).FirstOrDefault());
                }
                appointmentInput.EndDateTime = appointmentService.CalculateEndDateTime(appointmentInput.StartDateTime, chosenActivities);
                if (!appointmentService.TakenDateTimeCheck(appointmentInput.StartDateTime, appointmentInput.EndDateTime, isEdit, appointmentInput.UserId)) //if current time is taken
                {
                    return(View(createAppointmentViewModel));
                }
                appointmentInput.Activities.Clear();
                appointmentInput.Activities.AddRange(chosenActivities);
                if (appointmentService.Save(appointmentInput))
                {
                    TempData["Message"] = "Appointment saved successfully!";
                }
                else
                {
                    TempData["ErrorMessage"] = "Ooops something went wrong";
                }
                return(RedirectToAction("GetAll"));
            }
            return(View(createAppointmentViewModel));
        }