Beispiel #1
0
        public ActionResult EditVenue(VenueViewModel model)
        {
            //Populate states dropdown
            ViewBag.States = PopulateStatesDropdown();

            //Editing the venue details
            Venue existingVenue = _venueService.GetVenueById(model.VenueId);

            existingVenue.Address1    = model.Address1;
            existingVenue.Address2    = model.Address2;
            existingVenue.City        = model.City;
            existingVenue.DateUpdated = DateTime.Now;
            existingVenue.Name        = model.Name;
            existingVenue.Phone       = model.Phone;
            existingVenue.State       = model.State;
            existingVenue.ZipCode     = model.ZipCode;

            //find the current hours of the venue using the venueId
            List <BusinessHours> existingHours = _venueService.GetVenueBusinessHours(model.VenueId);

            TimeSpan beginTimeSpan = TimeSpan.Zero;
            TimeSpan endTimeSpan   = TimeSpan.Zero;

            //for each hours in the view modeling hours - runs 7 times since the days of weeks are not null
            foreach (var hours in model.BusinessHours)
            {
                //convert the string day of week to numbers to be able to pass it back into the dB
                DayOfWeek convertDayOfWeek = (DayOfWeek)(Enum.Parse(typeof(DayOfWeek), hours.DayOfWeek));

                //Match the day of week with the existing
                BusinessHours checkHours = existingHours.Find(x => x.DayOfWeek == (int)convertDayOfWeek);

                //Check if only one field is filled and the other isn't
                if (hours.OpenTime != null && hours.CloseTime == null)
                {
                    ViewBag.States = PopulateStatesDropdown();
                    ViewData.ModelState.AddModelError("BothTimeFilled", "Please fill out both Opening and Ending Time for the certain day");
                    return(View(model));
                }
                if (hours.OpenTime == null && hours.CloseTime != null)
                {
                    ViewBag.States = PopulateStatesDropdown();
                    ViewData.ModelState.AddModelError("BothTimeFilled", "Please fill out both Opening and Ending Time for the certain day");
                    return(View(model));
                }

                //If the Open Time and the Closing Time are not null, there was time entered into it
                if (hours.OpenTime != null && hours.CloseTime != null)
                {
                    //Checking if it is a valid format, if so assign it to the TimeSpan variable initialized above
                    bool checkOpenTime  = TimeSpan.TryParse(hours.OpenTime, out beginTimeSpan);
                    bool checkCloseTime = TimeSpan.TryParse(hours.CloseTime, out endTimeSpan);

                    if (!checkCloseTime && !checkOpenTime)
                    {
                        ViewBag.States = PopulateStatesDropdown();
                        ViewData.ModelState.AddModelError("ValidTime", "The time must be between 00:00 - 23:00");
                        return(View(model));
                    }


                    //checking if opening time and closing time is valid
                    if (beginTimeSpan >= endTimeSpan)
                    {
                        ViewBag.States = PopulateStatesDropdown();
                        ViewData.ModelState.AddModelError("TimeRange", "The Opening Time cannot be after or equal to the Closing Time");
                        return(View(model));
                    }

                    //Case: 1: Adding new Business Hours - If checkHours equals null, then there was no match so time to add in a new business hour
                    if (checkHours == null)
                    {
                        _venueService.AddBusinessHour(new BusinessHours()
                        {
                            VenueId   = model.VenueId,
                            CloseTime = TimeSpan.Parse(hours.CloseTime),
                            OpenTime  = TimeSpan.Parse(hours.OpenTime),
                            DayOfWeek = (int)convertDayOfWeek
                        });
                    }
                    //Case: 2 Modify Business Hours - if checkHours is not null there was a match
                    else
                    {
                        checkHours.CloseTime = TimeSpan.Parse(hours.CloseTime);
                        checkHours.OpenTime  = TimeSpan.Parse(hours.OpenTime);

                        _venueService.UpdateBusinessHours(checkHours);
                    }
                }

                //Case:3 Deleting Business Hours - user removes the business hours from the venue
                if (checkHours != null && (hours.OpenTime == null && hours.CloseTime == null))
                {
                    _venueService.DeleteBusinessHours(checkHours);
                }
            }

            //Edit the generic information of the venue excluding the business hours
            _venueService.EditVenue(existingVenue);



            return(RedirectToAction("Details", new { id = model.VenueId }));
        }