public IActionResult ById(SearchedInputModel input) { var viewModel = this.propertiesService.GetByIdBasedOnSearchRequirements(input); if (viewModel == null || !this.ModelState.IsValid) { return(this.RedirectToAction(nameof(this.Index), new IndexInputModel())); } return(this.View(viewModel)); }
public SearchedPropertyByIdViewModel GetByIdBasedOnSearchRequirements(SearchedInputModel input) { var property = this.propertiesRepository .All() .Where(p => p.Id == input.Id) .Select(p => new SearchedPropertyByIdViewModel { Id = p.Id, Name = p.Name, Country = p.Town.Country.Name, Town = p.Town.Name, Stars = p.Stars, Address = p.Address, CurrencyCode = p.Town.Country.Currency.CurrencyCode, Description = p.Description, Floors = p.Floors, Facilities = p.PropertyFacilities .Select(f => f.Facility.Name) .ToList(), Rules = p.PropertyRules .Select(r => new RuleNameIsAvailableViewModel { Name = r.Rule.Name, IsAllowed = r.IsAllowed, }) .ToList(), PropertyType = p.PropertyCategory.PropertyType.Name, PropertyCategory = p.PropertyCategory.Name, Images = p.PropertyImages.Select(oi => GlobalConstants.PropertyImagesPath + oi.Id + "." + oi.Extension).ToList(), Offers = p.Offers .Where(o => o.OfferBedTypes.Sum(b => b.BedType.Capacity) == input.Members && o.ValidFrom.Date <= input.CheckIn.Date && o.ValidTo.Date >= input.CheckOut.Date && (input.CheckOut - input.CheckIn).TotalDays >= 2) .Select(o => new SearchedOfferViewModel { Id = o.Id, Count = o.Count, Price = o.PricePerPerson, CheckIn = input.CheckIn.ToString(GlobalConstants.DateFormat), CheckOut = input.CheckOut.ToString(GlobalConstants.DateFormat), OfferFacilities = o.OfferFacilities .Select(f => new OfferFacilityViewModel { Name = f.Facility.Name, Category = f.Facility.FacilityCategory.Name, }) .ToList(), Rooms = o.OfferBedTypes .Select(b => new BedTypeViewModel { Type = b.BedType.Type, Capacity = b.BedType.Capacity, }) .ToList(), Guests = (byte)o.OfferBedTypes.Sum(b => b.BedType.Capacity), Images = o.OfferImages.Select(oi => GlobalConstants.OfferImagesPath + oi.Id + "." + oi.Extension).ToList(), }) .ToList(), }) .FirstOrDefault(); return(property); }