You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.8 KiB
109 lines
4.8 KiB
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<IoTProduct, EditPlatformIoTProductModel>
|
|
{
|
|
private readonly IRepository<IoTProductCategoryIoTProduct> _productCategoryProductRepo;
|
|
private readonly IRepository<IoTProductCategory> _productCategoryRepo;
|
|
private readonly AjaxController _ajax;
|
|
|
|
public IoTProductController(IRepository<IoTProduct> repo, IRepository<IoTProductCategoryIoTProduct> productCategoryProductRepo, IRepository<IoTProductCategory> productCategoryRepo, AjaxController ajax) : base(repo)
|
|
{
|
|
this._productCategoryProductRepo = productCategoryProductRepo;
|
|
this._productCategoryRepo = productCategoryRepo;
|
|
this._ajax = ajax;
|
|
}
|
|
|
|
public override IQueryable<IoTProduct> Query(PagedListModel<EditPlatformIoTProductModel> model, IQueryable<IoTProduct> 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|