using System; using System.Collections.Generic; using System.Linq; using Application.Domain.Entities; using Infrastructure.Data; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace IoTShared.Controllers { [Area("Admin")] public class AjaxController { private readonly ILogger _logger; private readonly IRepository _nodeRepo; private readonly IRepository _productRepo; private readonly IRepository _sceneRepo; private readonly IRepository _deviceRepo; private readonly IRepository _apiRepo; public AjaxController(ILogger logger, IRepository nodeRepo, IRepository productRepo, IRepository sceneRepo, IRepository deviceRepo, IRepository apiRepo) { this._logger = logger; this._nodeRepo = nodeRepo; this._productRepo = productRepo; this._sceneRepo = sceneRepo; this._deviceRepo = deviceRepo; this._apiRepo = apiRepo; } #region Node public SelectList GetNodeSelectList(Guid? selected = null) { var list = this._nodeRepo.ReadOnlyTable() .Select(o => new { o.Id, Name = $"{o.Name}/{o.Number}" }) .ToList(); return new SelectList(list, "Id", "Name", selected); } #endregion Node #region Scene public SelectList GetSceneSelectList(Guid parentId, Guid? selected = null) { var list = this._sceneRepo.ReadOnlyTable() .Where(o => o.NodeId == parentId) .Select(o => new { o.Id, o.Name }) .ToList(); return new SelectList(list, "Id", "Name", selected); } public JsonResult GetSceneJson(Guid parentId, Guid? selected) { return new JsonResult(this.GetSceneSelectList(parentId, selected)); } #endregion Scene #region Device public SelectList GetDeviceSelectList(Guid parentId, Guid? selected = null) { var list = this._deviceRepo.ReadOnlyTable() .Where(o => o.NodeId == parentId) .Select(o => new { o.Id, Name = $"{o.DisplayName ?? o.Name}/{o.Number}" }) .ToList(); return new SelectList(list, "Id", "Name", selected); } public JsonResult GetDeviceJson(Guid parentId, Guid? selected) { return new JsonResult(this.GetDeviceSelectList(parentId, selected)); } #endregion Device #region Api public SelectList GetProductSelectList(Guid? selected = null) { var list = this._productRepo.ReadOnlyTable() .Select(o => new { o.Id, o.Name }) .ToList(); return new SelectList(list, "Id", "Name", selected); } public SelectList GetApiSelectList(Guid parentId, Guid? selected = null) { var list = this._deviceRepo.ReadOnlyTable() .Where(o => o.Id == parentId).SelectMany(o => o.Product.Apis) .Select(o => new { o.Id, o.Name }) .ToList(); return new SelectList(list, "Id", "Name", selected); } public JsonResult GetApiJson(Guid parentId, Guid? selected) { return new JsonResult(this.GetApiSelectList(parentId, selected)); } #endregion Api } }