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 IoTParameterController : CrudController { private readonly AjaxBaseController _ajax; public IoTParameterController(IRepository repo, AjaxBaseController ajax) : base(repo) { this._ajax = ajax; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.Api).ThenInclude(o => o.Product); } public override IQueryable Query(PagedListModel model, IQueryable query) { return base.Query(model, query) .WhereIf(model.Query.Required.HasValue, o => o.Required == model.Query.Required.Value) .WhereIf(model.Query.ProductId.HasValue, o => o.Api.ProductId == model.Query.ProductId.Value) .WhereIf(model.Query.ApiId.HasValue, o => o.ApiId == model.Query.ApiId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Type), o => o.Type.Contains(model.Query.Type)) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(!string.IsNullOrEmpty(model.Query.Description), o => o.Description.Contains(model.Query.Description)) .WhereIf(!string.IsNullOrEmpty(model.Query.Minimum), o => o.Minimum == model.Query.Minimum) .WhereIf(!string.IsNullOrEmpty(model.Query.Maxinum), o => o.Maxinum == model.Query.Maxinum); } public override void ToDisplayModel(IoTParameter entity, EditParameterModel model) { model.ProductId = entity?.Api.ProductId; ViewData.Add(model.ProductId, entity?.Api?.Product?.Name); ViewData.Add(model.ApiId, entity?.Api?.Name); } public override void ToEditModel(IoTParameter entity, EditParameterModel model) { model.ProductId = entity?.Api?.ProductId; ViewData.SelectList(o => model.ProductId, () => this._ajax.GetIoTProduct(model.ProductId).SelectList()); if(model.ProductId.HasValue) { ViewData.SelectList(o => model.ApiId, () => this._ajax.GetIoTApi(model.ProductId.Value, model.ApiId).SelectList()); } } } }