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.
iot/projects/Platform/Areas/IoTCenter/Controllers/IoTProductController.cs

70 lines
3.0 KiB

using Infrastructure.Application;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Web.Mvc;
using IoT.Shared.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<IoTProduct, EditPlatformIoTProductModel>
{
private readonly IRepository<IoTProductCategory> _productCategoryRepo;
private readonly AjaxController _ajax;
public IoTProductController(IRepository<IoTProduct> repo, IRepository<IoTProductCategory> productCategoryRepo, AjaxController ajax) : base(repo)
{
this._productCategoryRepo = productCategoryRepo;
this._ajax = ajax;
}
public override IQueryable<IoTProduct> Query(PagedListModel<EditPlatformIoTProductModel> model, IQueryable<IoTProduct> query)
{
return query
.WhereIf(model.Query.CategoryId.HasValue, o => o.CategoryId == model.Query.CategoryId.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.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 IQueryable<IoTProduct> Include(IQueryable<IoTProduct> query)
{
return query.Include(o => o.Category);
}
public override void ToDisplayModel(IoTProduct entity, EditPlatformIoTProductModel model)
{
if (model.CategoryId.HasValue)
{
var name = this._productCategoryRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.Where(o => o.Left <= entity.Category.Left && o.Right >= entity.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)
{
ViewData.SelectList(o => model.CategoryId, () => this._ajax.GetIoTProductCategory(model.CategoryId).SelectList());
}
}
}