You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoTCenter/Areas/Admin/Controllers/HomeController.cs

64 lines
2.3 KiB

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<Node> _nodeRepo;
private readonly IRepository<Product> _productRepo;
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<Category> _categoryRepo;
public HomeController(IRepository<Node> nodeRepo, IRepository<Category> categoryRepo, IRepository<Product> productRepo, IRepository<Device> 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);
}
}
}