using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; using IoT.Shared.Application.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System; using System.Linq; namespace IoT.Shared.Areas.IoTCenter.Controlls { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] [ControllerScope(ControllerScopeType.Platform)] public class IoTDeviceController : CrudController { private readonly AjaxBaseController _ajax; public IoTDeviceController(IRepository repo, AjaxBaseController ajax) : base(repo) { this._ajax = ajax; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.Product).Include(o => o.Node); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.NodeId.HasValue, o => o.NodeId == model.Query.NodeId.Value) .WhereIf(model.Query.ProductId.HasValue, o => o.ProductId == model.Query.ProductId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(!string.IsNullOrEmpty(model.Query.DisplayName), o => o.DisplayName.Contains(model.Query.DisplayName)) .WhereIf(!string.IsNullOrEmpty(model.Query.Gateway), o => o.Gateway.Contains(model.Query.Gateway)) .WhereIf(!string.IsNullOrEmpty(model.Query.Tag), o => o.Tag.Contains(model.Query.Tag)) .WhereIf(!string.IsNullOrEmpty(model.Query.Ip), o => o.Ip.Contains(model.Query.Ip)) .WhereIf(!string.IsNullOrEmpty(model.Query.UserName), o => o.UserName.Contains(model.Query.UserName)) .WhereIf(!string.IsNullOrEmpty(model.Query.Password), o => o.Password.Contains(model.Query.Password)) .WhereIf(!string.IsNullOrEmpty(model.Query.Number), o => o.Number.Contains(model.Query.Number)) .WhereIf(!string.IsNullOrEmpty(model.Query.Icon), o => o.Icon.Contains(model.Query.Icon)) .WhereIf(!string.IsNullOrEmpty(model.Query.ConnectId), o => o.ConnectId.Contains(model.Query.ConnectId)) .WhereIf(model.Query.IsOnline.HasValue, o => o.IsOnline == model.Query.IsOnline.Value) .WhereIf(model.Query.Disabled.HasValue, o => o.Disabled == model.Query.Disabled.Value); } public override void ToDisplayModel(IoTDevice entity, EditIoTDeviceModel model) { ViewData.Add(model.NodeId, entity?.Node?.Name); ViewData.Add(model.ProductId, entity?.Product?.Name); } public override void ToEditModel(IoTDevice entity, EditIoTDeviceModel model) { ViewData.SelectList(o => model.NodeId, () => this._ajax.GetIoTGateway(model.NodeId).SelectList()); ViewData.SelectList(o => model.ProductId, () => this._ajax.GetIoTProduct(model.ProductId).SelectList()); } } }