using Application.Domain.Entities; using Infrastructure.Data; using IoTCenter.Areas.Admin.ViewModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Linq; namespace IoTCenter.Areas.Admin.Controllers { [Authorize] [Area(nameof(Admin))] public class HomeController : Controller { private readonly IRepository _nodeRepo; private readonly IRepository _productRepo; private readonly IRepository _deviceRepo; private readonly IRepository _categoryRepo; public HomeController(IRepository nodeRepo, IRepository categoryRepo, IRepository productRepo, IRepository deviceRepo) { this._nodeRepo = nodeRepo; this._categoryRepo = categoryRepo; this._productRepo = productRepo; this._deviceRepo = deviceRepo; } public IActionResult Index() { var model = new HomeIndexViewModel { Nodes = this._nodeRepo.ReadOnlyTable() .ToList() .Select(o => new NodeViewModel { Id = o.Id, Number = o.Number, Name = o.Name, Image = o.Image, DeviceCount = _deviceRepo.ReadOnlyTable().Count(d => d.NodeId == o.Id) }).ToList(), Categories = this._categoryRepo.ReadOnlyTable() .ToList() .Select(o => new CategoryViewModel { Id = o.Id, Name = o.Name, Image = o.Image, Number = o.Number, DeviceCount = _deviceRepo.ReadOnlyTable().Count(d => d.Product.CategoryId == o.Id) }).ToList(), Products = this._productRepo.ReadOnlyTable() .ToList() .Select(o => new ProductViewModel { Id = o.Id, Name = o.Name, Image = o.Image, DeviceCount = _deviceRepo.ReadOnlyTable().Count(d => d.ProductId == o.Id) }).ToList() }; return View(model); } } }