Beispiel #1
0
        public ActionResult Create(ReservationViewModel model)
        {
            if (ModelState.IsValid)
            {
                ValidateReservation(model);
            }

            if (ModelState.IsValid)
            {
                _dataService.SaveReservation(model.ConvertToReservation());

                if (model.InstructorId != 0)
                {
                    try
                    {
                        Member   pilot      = _dataService.GetMember(model.MemberId);
                        Member   instructor = _dataService.GetMember((int)model.InstructorId);
                        Aircraft aircraft   = _dataService.GetAircraftById(model.AircraftId);

                        MailMessage message = new MailMessage();
                        message.From = new MailAddress("*****@*****.**");
                        message.To.Add(new MailAddress(instructor.Login.Email));

                        message.Subject = "Flight lesson notification";
                        message.Body    = "NTXFC club member " + pilot.FullName
                                          + " has scheduled flight lesson in " + aircraft.RegistrationNumber
                                          + " starting at " + model.StartTime + " on " + model.StartDate.ToString("yyyy-MM-dd") + "\t"
                                          + "If you are not able please contact pilot at " + pilot.Login.Email + " or " + pilot.Phone + "\t";

                        SendEmail(message);
                    }
                    catch (Exception ex)
                    {
                        string msg = "Error while trying to send Reservation notification:\t" + ex.ToString();
                        LogError(msg);
                    }
                }

                return(RedirectToAction("AircraftByWeek", new { aircraftId = model.AircraftId, startDate = model.StartDate }));
            }

            model.AircraftList   = _dataService.GetAllAirplanes();
            model.InstructorList = _dataService.GetMembersByRoleAndStatus(UserRoles.Instructor, MemberStatus.Active);
            model.TimeList       = new List <DateTime>().GetListFromRange(DateTime.MinValue.AddHours(6), DateTime.MinValue.AddHours(22),
                                                                          new TimeSpan(0, 30, 0)).ConvertAll(x => new SelectListItem()
            {
                Value = x.ToString("HH:mm"), Text = x.ToString("HH:mm")
            }).ToList();

            model.AircraftList.Insert(0, new Aircraft());
            model.InstructorList.Insert(0, new Member());

            return(View(model));
        }