Files
SkyArtShop/Controllers/HomeController.cs

68 lines
2.0 KiB
C#
Raw Normal View History

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<IActionResult> Index()
{
SiteSettings settings = await GetSiteSettings();
List<Product> topProducts = await GetTopSellerProducts();
List<HomepageSection> list = await GetHomepageSections();
base.ViewBag.Settings = settings;
base.ViewBag.TopProducts = topProducts;
base.ViewBag.Sections = list;
return View();
}
private async Task<SiteSettings> GetSiteSettings()
{
return (await _pgService.GetSiteSettingsAsync()) ?? new SiteSettings();
}
private async Task<List<Product>> GetTopSellerProducts()
{
return (await _pgService.GetAllAsync<Product>(_productsCollection)).Where((Product p) => p.IsTopSeller && p.IsActive).Take(4).ToList();
}
private async Task<List<HomepageSection>> GetHomepageSections()
{
List<HomepageSection> list = await _pgService.GetAllAsync<HomepageSection>(_sectionsCollection);
Console.WriteLine($"Total sections from DB: {list.Count}");
List<HomepageSection> 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();
}
}