using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using Application.Domain.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Platform.Application.Models.IoTCenter; using Platform.Areas.IoTCenter.Controllers; using System.Linq; namespace IoT.Shared.Areas.IoTCenter.Controlls { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] [ControllerScope(ControllerScopeType.Platform)] public class IoTProductController : CrudController { private readonly IRepository _productCategoryRepo; private readonly AjaxController _ajax; public IoTProductController(IRepository repo, IRepository productCategoryRepo, AjaxController ajax) : base(repo) { this._productCategoryRepo = productCategoryRepo; this._ajax = ajax; } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.IoTProductCategoryId.HasValue, o => o.IoTProductCategoryId == model.Query.IoTProductCategoryId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(!string.IsNullOrEmpty(model.Query.Number), o => o.Number.Contains(model.Query.Number)) .WhereIf(!string.IsNullOrEmpty(model.Query.Path), o => o.Path.Contains(model.Query.Path)) .OrderBy(o => o.Number); } public override IQueryable Include(IQueryable query) { return query.Include(o => o.IoTProductCategory); } public override void ToDisplayModel(IoTProduct entity, EditPlatformIoTProductModel model) { if (model.IoTProductCategoryId.HasValue) { var name = this._productCategoryRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTProductCategory.Left && o.Right >= entity.IoTProductCategory.Right).ToList() .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.IoTProductCategoryId.Value)?.GetDisplayName(); ViewData.Add(model.IoTProductCategoryId, name); } } public override void ToEditModel(IoTProduct entity, EditPlatformIoTProductModel model) { ViewData.SelectList(o => model.IoTProductCategoryId, () => this._ajax.GetIoTProductCategory(model.IoTProductCategoryId).SelectList()); } } }