using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using 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.IoTProduct).Include(o => o.IoTGateway); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.IoTGatewayId.HasValue, o => o.IoTGatewayId == model.Query.IoTGatewayId.Value) .WhereIf(model.Query.IoTProductId.HasValue, o => o.IoTProductId == model.Query.IoTProductId.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.Number), o => o.Number.Contains(model.Query.Number)) .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.IoTGatewayId, entity?.IoTGateway?.Name); ViewData.Add(model.IoTProductId, entity?.IoTProduct?.Name); } public override void ToEditModel(IoTDevice entity, EditIoTDeviceModel model) { ViewData.SelectList(o => model.IoTGatewayId, () => this._ajax.GetIoTGateway(model.IoTGatewayId).SelectList()); ViewData.SelectList(o => model.IoTProductId, () => this._ajax.GetIoTProduct(model.IoTProductId).SelectList()); } } }