using System; using System.Linq; using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Jwt; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace IoTCenter.Controllers { public class HomeController : Controller { private readonly IJwtHelper _jwtHelper; private readonly IRepository _nodeRepo; private readonly IRepository _deviceRepo; public HomeController(IJwtHelper jwtHelper, IRepository nodeRepo, IRepository deviceRepo) { this._jwtHelper = jwtHelper; this._nodeRepo = nodeRepo; this._deviceRepo = deviceRepo; } [Authorize] public IActionResult Index() { return View(); } public IActionResult GetNodes(string token) { var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString(); var model = this._nodeRepo.ReadOnlyTable() .Select(o => new { o.Number, o.Name, o.DisplayOrder, Count = o.Devices.Count }) .ToList(); return Json(model); } [Authorize] public IActionResult Node(Guid id) { var model = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == id); return View(model); } public IActionResult GetNode(Guid id) { var model = this._nodeRepo.ReadOnlyTable() .Include(o => o.Sences) .Include(o => o.Devices).ThenInclude(o => o.Data) .Include(o => o.Devices).ThenInclude(o => o.Apis).ThenInclude(o => o.Parameters) .FirstOrDefault(o => o.Id == id); return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } } }