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; using Platform.Areas.IoTCenter.Controllers; using System; using System.Linq; namespace Platform.Areas.Admin.Controllers { [Authorize] [ApiController] [Route("Admin/[controller]/[action]")] [Area("Admin")] [ControllerScope(ControllerScopeType.Platform)] public class AreaController : TreeCrudController { private readonly AjaxController _ajax; public AreaController(IRepository repo, AjaxController ajax) : base(repo) { this._ajax = ajax; } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .Where(o => o.ParentId != null) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(!string.IsNullOrEmpty(model.Query.Number), o => o.Name.Contains(model.Query.Number)) .WhereIf(model.Query.Type.HasValue, o => o.Type == model.Query.Type.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.CustomType), o => o.CustomType.Contains(model.Query.CustomType)) .WhereIf(model.Query.ParentId.HasValue, o => o.ParentId == model.Query.ParentId.Value) .OrderBy(o => o.ParentId).ThenBy(o => o.DisplayOrder); } public override void ToDisplayModel(Area entity, EditAreaModel model) { if (model.ParentId.HasValue) { var name = this.Repo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left < entity.Left && o.Right > entity.Right).ToList() .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.ParentId.Value)?.GetDisplayName(); ViewData.Add(model.ParentId.Value, name); } } public override void ToEditModel(Area entity, EditAreaModel model) { ViewData.SelectList(o => model.ParentId, () => this._ajax.GetArea(model.ParentId).SelectList()); } } }