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.
71 lines
2.6 KiB
71 lines
2.6 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using IoT.Shared.Areas.Admin.Controlls;
|
|
using IoTCenter.Application.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
|
|
namespace IoTCenter.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class NodeCategoryController : CrudController<NodeCategory, EditNodeCategoryModel>
|
|
{
|
|
private readonly AjaxController _ajax;
|
|
|
|
public NodeCategoryController(IRepository<NodeCategory> repo, AjaxController ajax) : base(repo)
|
|
{
|
|
this._ajax = ajax;
|
|
}
|
|
|
|
public override IQueryable<NodeCategory> Include(IQueryable<NodeCategory> query)
|
|
{
|
|
return query.Include(o => o.CategoryNodes).ThenInclude(o => o.Node);
|
|
}
|
|
|
|
public override IQueryable<NodeCategory> Query(PagedListModel<EditNodeCategoryModel> model, IQueryable<NodeCategory> query)
|
|
{
|
|
return base.Query(model, query)
|
|
.WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name))
|
|
.WhereIf(!string.IsNullOrEmpty(model.Query.Template), o => o.Template.Contains(model.Query.Template));
|
|
}
|
|
|
|
public override void ToDisplayModel(NodeCategory entity, EditNodeCategoryModel model)
|
|
{
|
|
model.Nodes = entity.CategoryNodes.Select(o => o.NodeId).ToList();
|
|
entity.CategoryNodes.ForEach(o => ViewData.Add(o.NodeId, o.Node.Name));
|
|
}
|
|
|
|
public override void ToEditModel(NodeCategory entity, EditNodeCategoryModel model)
|
|
{
|
|
if (entity != null)
|
|
{
|
|
model.Nodes = entity.CategoryNodes.Select(o => o.NodeId).ToList();
|
|
}
|
|
this.ViewData.MultiSelectList(o => model.Nodes, () => this._ajax.GetNodeMultiSelectList(model.Nodes));
|
|
}
|
|
|
|
public override void ToEntity(EditNodeCategoryModel model, NodeCategory entity)
|
|
{
|
|
foreach (var id in entity.CategoryNodes.Select(o => o.NodeId).ToList())
|
|
{
|
|
if (!model.Nodes.Any(o => o == id))
|
|
{
|
|
entity.CategoryNodes.RemoveAll(o => o.NodeId == id);
|
|
}
|
|
}
|
|
foreach (var id in model.Nodes)
|
|
{
|
|
if (!entity.CategoryNodes.Any(o => o.NodeId == id))
|
|
{
|
|
entity.CategoryNodes.Add(new NodeCategoryNode { NodeId = id });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |