// GET: CalendarDate/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            M_CalendarDate editableDate = _repositoriyCalendar.FindById(id.Value);

            if (editableDate == null)
            {
                return(HttpNotFound());
            }

            CalendarDateViewModel editableDateVM = new CalendarDateViewModel(editableDate);
            var list = ViewModelFactory.GetSelectedListEmployersName(_employers);

            if (list.Count() != 0)
            {
                list.Where(li => li.Value == editableDateVM.NameEmployer)
                .FirstOrDefault()
                .Selected = true;
            }

            ViewBag.EmployerNames = list;
            return(View(editableDateVM));
        }
        public ActionResult Create(CalendarDateViewModel createdDate)
        {
            if (ModelState.IsValid)
            {
                M_CalendarDate m_CalendarDate = createdDate.GetModel() as M_CalendarDate;
                _repositoriyCalendar.Create(m_CalendarDate);
                return(RedirectToAction("Index"));
            }

            return(View(createdDate));
        }
        public ActionResult Edit(CalendarDateViewModel editableDateVM)
        {
            M_CalendarDate editableDate = editableDateVM.GetModel() as M_CalendarDate;

            if (ModelState.IsValid)
            {
                _repositoriyCalendar.Update(editableDate);
                return(RedirectToAction("Index"));
            }

            return(View(editableDateVM));
        }
        // GET: CalendarDate/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            M_CalendarDate deletedDate = _repositoriyCalendar.FindById(id.Value);

            if (deletedDate == null)
            {
                return(HttpNotFound());
            }

            CalendarDateViewModel deletedDateVM = new CalendarDateViewModel(deletedDate);

            return(View(deletedDateVM));
        }
        // GET: CalendarDate/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            M_CalendarDate m_CalendarDate = _repositoriyCalendar.FindById(id.Value);


            if (m_CalendarDate == null)
            {
                return(HttpNotFound());
            }

            CalendarDateViewModel calendarDetails = new CalendarDateViewModel(m_CalendarDate);

            return(View(calendarDetails));
        }
        public CalendarViewModel Get(int rentalId, DateTime start, int nights)
        {
            if (nights < 0)
            {
                throw new ApplicationException("Nights must be positive");
            }
            if (!_rentals.ContainsKey(rentalId))
            {
                throw new ApplicationException("Rental not found");
            }

            var result = new CalendarViewModel
            {
                RentalId = rentalId,
                Dates    = new List <CalendarDateViewModel>()
            };

            for (var i = 0; i < nights; i++)
            {
                var date = new CalendarDateViewModel
                {
                    Date     = start.Date.AddDays(i),
                    Bookings = new List <CalendarBookingViewModel>()
                };

                foreach (var booking in _bookings.Values)
                {
                    if (booking.RentalId == rentalId &&
                        booking.Start <= date.Date && booking.Start.AddDays(booking.Nights) > date.Date)
                    {
                        date.Bookings.Add(new CalendarBookingViewModel {
                            Id = booking.Id
                        });
                    }
                }

                result.Dates.Add(date);
            }

            return(result);
        }
        public CalendarViewModel Get(int rentalId, DateTime start, int nights)
        {
            var result = new CalendarViewModel
            {
                RentalId = rentalId,
                Dates    = new List <CalendarDateViewModel>()
            };
            int units = _rentals[rentalId].Units;

            for (var i = 0; i < nights; i++)
            {
                int occupiedUnits = 0;
                var date          = new CalendarDateViewModel
                {
                    Date     = start.Date.AddDays(i),
                    Bookings = new List <CalendarBookingViewModel>()
                };
                var rentalBooking = _bookings.Values.Where(x => x.RentalId == rentalId).ToList();

                foreach (var booking in rentalBooking)
                {
                    DateTime bookingEnd   = booking.Start.AddDays(booking.Nights);
                    DateTime bookingStart = booking.Start;

                    if (bookingStart <= date.Date && bookingEnd >= date.Date)
                    {
                        occupiedUnits += 1;
                        date.Bookings.Add(new CalendarBookingViewModel
                        {
                            Id     = booking.Id,
                            Nights = booking.Nights
                        });
                    }
                }
                date.Unit = units - occupiedUnits;
                result.Dates.Add(date);
            }
            result.PreparationTimeInDays = _rentals[rentalId].PreparationTimeInDays;
            return(result);
        }