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 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 _productCategoryProductRepo; private readonly IRepository _productCategoryRepo; private readonly AjaxController _ajax; public IoTProductController(IRepository repo, IRepository productCategoryProductRepo, IRepository productCategoryRepo, AjaxController ajax) : base(repo) { this._productCategoryProductRepo = productCategoryProductRepo; this._productCategoryRepo = productCategoryRepo; this._ajax = ajax; } public override IQueryable Query(PagedListModel model, IQueryable query) { if(model.Query.CategoryId.HasValue) { query = this._productCategoryProductRepo.ReadOnlyTable().Where(o => o.CategoryId == model.Query.CategoryId.Value).Select(o => o.Product); } return query .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.Number), o => o.Number.Contains(model.Query.Number)) .WhereIf(!string.IsNullOrEmpty(model.Query.Path), o => o.Path.Contains(model.Query.Path)) .WhereIf(!string.IsNullOrEmpty(model.Query.Path), o => o.Path.Contains(model.Query.Path)) .WhereIf(!string.IsNullOrEmpty(model.Query.ApiJson), o => o.ApiJson.Contains(model.Query.ApiJson)) .OrderBy(o => o.Number); } public override void ToDisplayModel(IoTProduct entity, EditPlatformIoTProductModel model) { var category = this._productCategoryProductRepo.ReadOnlyTable() .Where(o => o.ProductId == model.Id) .Select(o=>o.Category) .FirstOrDefault(); model.CategoryId = category?.Id; if(model.CategoryId.HasValue) { var name = this._productCategoryRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= category.Left && o.Right >= category.Right).ToList() .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.CategoryId.Value)?.GetDisplayName(); ViewData.Add(model.CategoryId, name); } } public override void ToEditModel(IoTProduct entity, EditPlatformIoTProductModel model) { if(entity!=null) { model.CategoryId = this._productCategoryProductRepo .ReadOnlyTable() .FirstOrDefault(o => o.ProductId == model.Id) ?.CategoryId; } ViewData.SelectList(o => model.CategoryId, () => this._ajax.GetIoTProductCategory(model.CategoryId).SelectList()); } public override void ToEntity(EditPlatformIoTProductModel model, IoTProduct entity) { var productCategory = this._productCategoryProductRepo.Table().FirstOrDefault(o => o.ProductId == model.Id); if (model.CategoryId.HasValue) { if(productCategory==null) { productCategory = new IoTProductCategoryIoTProduct { CategoryId=model.CategoryId.Value,ProductId=model.Id }; this._productCategoryProductRepo.Add(productCategory); } else { if(productCategory.CategoryId!=model.CategoryId.Value) { productCategory.CategoryId = model.CategoryId.Value; } } } else { if(productCategory!=null) { this._productCategoryProductRepo.Delete(productCategory); } } } } }