Esempio n. 1
0
        private async Task <PropertyCountServiceModel> GetByCategoryAsync(HomeStatus managed, HomeCategory category)
        {
            var exampleImage = string.Empty;

            var count = await this.context.Homes
                        .Where(h => h.Status != managed && h.Category == category)
                        .CountAsync();

            if (category == HomeCategory.Apartment)
            {
                exampleImage = ApartmentImage;
            }
            else if (category == HomeCategory.House)
            {
                exampleImage = HouseImage;
            }
            else if (category == HomeCategory.Room)
            {
                exampleImage = RoomImage;
            }

            var categoryUpper = category.ToString().FirstCharToUpper();

            var model = new PropertyCountServiceModel
            {
                CategoryName = category.ToString().FirstCharToUpper() + "s",
                Count        = count,
                ExampleImage = exampleImage,
            };

            return(model);
        }
        public async Task <IActionResult> Index()
        {
            if (this.User.Identity.IsAuthenticated && this.User.IsInRole(AdministratorRoleName))
            {
                return(this.RedirectToAction("Index", "Dashboard", new { area = AdminArea }));
            }

            if (this.User.Identity.IsAuthenticated && this.User.IsInRole(OwnerRoleName))
            {
                return(this.RedirectToAction("Index", "Dashboard", new { area = ManagementArea }));
            }

            var housesCountModel = await this.listingService.GetPropertyCountByCategoryAsync(House);

            var apartmentsCountModel = await this.listingService.GetPropertyCountByCategoryAsync(Apartment);

            var roomsCountModel = await this.listingService.GetPropertyCountByCategoryAsync(Room);

            if (housesCountModel == null)
            {
                housesCountModel = new PropertyCountServiceModel
                {
                    CategoryName = "No Houses",
                    Count        = 0,
                    ExampleImage = DefaultPropertyImage,
                };
            }

            if (apartmentsCountModel == null)
            {
                apartmentsCountModel = new PropertyCountServiceModel
                {
                    CategoryName = "No Apartments",
                    Count        = 0,
                    ExampleImage = DefaultPropertyImage,
                };
            }

            if (roomsCountModel == null)
            {
                roomsCountModel = new PropertyCountServiceModel
                {
                    CategoryName = "No Rooms",
                    Count        = 0,
                    ExampleImage = DefaultPropertyImage,
                };
            }

            var model = new HomeIndexViewModel
            {
                PropertiesToRent   = await this.listingService.GetAllByStatusAsync(ToRent),
                PropertiesToManage = await this.listingService.GetAllByStatusAsync(ToManage),
            };

            model.PropertiesByCategory.Add(housesCountModel);
            model.PropertiesByCategory.Add(apartmentsCountModel);
            model.PropertiesByCategory.Add(roomsCountModel);

            // Redundant Check ???
            if (model == null)
            {
                return(this.RedirectToAction(
                           nameof(HomeController.Index),
                           "Home",
                           new { area = string.Empty })
                       .WithWarning(string.Empty, CouldNotFind));
            }

            return(this.View(model));
        }