using Infrastructure.Data; using Application.Domain.Entities; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Linq; namespace IoT.Shared.Areas.IoTCenter.Controlls { public class AjaxBaseController { protected readonly IServiceProvider _services; protected readonly ILogger _logger; public AjaxBaseController(IServiceProvider services, ILogger logger) { this._services = services; this._logger = logger; } public JsonResult GetIoTProduct(Guid? selected) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Select(o => new { o.Id, o.Name }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTApi(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.IoTProductId == parentId) .Select(o => new { o.Id, o.Name }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTGateway(Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Select(o => new { o.Id, Name = $"[{o.Number}]{o.Name}" }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTDevice(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.IoTGatewayId == parentId) .Select(o => new { o.Id, o.Name }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } } }