using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using SkyArtShop.Models; using SkyArtShop.Services; namespace SkyArtShop.Controllers; public class HomeController : Controller { private readonly PostgreSQLService _pgService; private readonly string _productsCollection = "Products"; private readonly string _sectionsCollection = "HomepageSections"; public HomeController(PostgreSQLService pgService) { _pgService = pgService; } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public async Task Index() { SiteSettings settings = await GetSiteSettings(); List topProducts = await GetTopSellerProducts(); List list = await GetHomepageSections(); base.ViewBag.Settings = settings; base.ViewBag.TopProducts = topProducts; base.ViewBag.Sections = list; return View(); } private async Task GetSiteSettings() { return (await _pgService.GetSiteSettingsAsync()) ?? new SiteSettings(); } private async Task> GetTopSellerProducts() { return (await _pgService.GetAllAsync(_productsCollection)).Where((Product p) => p.IsTopSeller && p.IsActive).Take(4).ToList(); } private async Task> GetHomepageSections() { List list = await _pgService.GetAllAsync(_sectionsCollection); Console.WriteLine($"Total sections from DB: {list.Count}"); List list2 = (from s in list where s.IsActive orderby s.DisplayOrder select s).ToList(); Console.WriteLine($"Active sections: {list2.Count}"); foreach (HomepageSection item in list2) { Console.WriteLine($"Section: {item.Title} | Type: {item.SectionType} | Order: {item.DisplayOrder} | Active: {item.IsActive}"); } return list2; } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(); } }