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 Microsoft.Extensions.Configuration; using System.Linq; namespace IoT.Shared.Areas.IoTCenter.Controlls { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] [ControllerScope(ControllerScopeType.Platform)] public class IoTDataController : CrudController { private readonly AjaxBaseController _ajax; public IoTDataController(IRepository repo, AjaxBaseController ajax) : base(repo) { this._ajax = ajax; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.IoTDevice).ThenInclude(o => o.IoTGateway) .Include(o=>o.IoTDevice).ThenInclude(o=>o.IoTProduct); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.IoTProductId.HasValue, o => o.IoTDevice.IoTProductId == model.Query.IoTProductId.Value) .WhereIf(model.Query.IoTGatewayId.HasValue, o => o.IoTDevice.IoTGatewayId == model.Query.IoTGatewayId.Value) .WhereIf(model.Query.IoTDeviceId.HasValue, o => o.IoTDeviceId == model.Query.IoTDeviceId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(!string.IsNullOrEmpty(model.Query.Key), o => o.Key.Contains(model.Query.Key)) .WhereIf(!string.IsNullOrEmpty(model.Query.Value), o => o.Value.Contains(model.Query.Value)) .WhereIf(model.Query.ValueType.HasValue, o => o.ValueType == model.Query.ValueType.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Unit), o => o.Unit.Contains(model.Query.Unit)) .WhereIf(!string.IsNullOrEmpty(model.Query.Description), o => o.Description.Contains(model.Query.Description)) .WhereIf(model.Query.Timestamp.HasValue, o => o.Timestamp == model.Query.Timestamp.Value) .WhereIf(model.Query.Hidden.HasValue, o => o.Hidden == model.Query.Hidden.Value) .OrderBy(o => o.IoTDevice.IoTProductId).ThenBy(o => o.IoTDevice.IoTGatewayId).ThenBy(o => o.IoTDeviceId); } public override void ToDisplayModel(IoTData entity, EditIoTDataModel model) { if(entity!=null) { model.IoTProductId = entity.IoTDevice.IoTProductId; model.IoTGatewayId = entity.IoTDevice.IoTGatewayId; } if(model.IoTProductId.HasValue) { ViewData.Add(model.IoTProductId, entity.IoTDevice.IoTProduct.Name); } if(model.IoTDeviceId.HasValue) { ViewData.Add(model.IoTDeviceId, entity.IoTDevice.Name); } if(model.IoTGatewayId.HasValue) { ViewData.Add(model.IoTGatewayId, entity.IoTDevice.IoTGateway.Name); } } public override void ToEditModel(IoTData entity, EditIoTDataModel model) { if(entity!=null) { model.IoTGatewayId = entity?.IoTDevice?.IoTGatewayId; } ViewData.SelectList(o => model.IoTProductId, () => this._ajax.GetIoTProduct(model.IoTProductId).SelectList()); ViewData.SelectList(o => model.IoTGatewayId, () => this._ajax.GetIoTGateway(model.IoTGatewayId).SelectList()); if (model.IoTGatewayId.HasValue) { ViewData.SelectList(o => model.IoTDeviceId, () => this._ajax.GetIoTDevice(model.IoTGatewayId.Value, model.IoTDeviceId).SelectList()); } } } }