using Application.Domain.Entities; 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.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; namespace IoT.Shared.Areas.Admin.Controllers { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] public class PermissionController : CrudController { private readonly IRepository _permissionCategoryRepo; public PermissionController(IRepository repo, IRepository ajax) : base(repo) { this._permissionCategoryRepo = ajax; } public override IActionResult Index(PagedListModel model) { this.ToEditModel(null, model.Query); return base.Index(model); } public override IQueryable Include(IQueryable query) { return query.Include(o => o.Category).ThenInclude(o => o.Parent); } public override IQueryable Query(PagedListModel model, IQueryable 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)) .OrderBy(o => o.CategoryId).ThenBy(o => o.Name); } public override void ToEditModel(Permission entity, EditPermissionModel model) { this.ViewData.SelectList(o => model.CategoryId, () => this.GetPermissionCategorySelectList(model.CategoryId)); } public override void ToDisplayModel(Permission entity, EditPermissionModel model) { this.ViewData.Add(model.CategoryId, entity?.Category?.Name); } [ApiExplorerSettings(IgnoreApi = true)] public SelectList GetPermissionCategorySelectList(Guid? selected) { var list = this._permissionCategoryRepo.ReadOnlyTable() .Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" }) .ToList(); return new SelectList(list, "Id", "Name", selected); } [ApiExplorerSettings(IgnoreApi = true)] public MultiSelectList GetPermissionMultiSelectList(IEnumerable selectedValues) { if (selectedValues == null) { selectedValues = new List(); } var list = this.Repo.ReadOnlyTable() .Include(o => o.Category) .OrderBy(o => o.CategoryId).ThenBy(o => o.Name) .Select(o => new { o.Id, o.Name, Group = o.Category.Name }) .ToList(); return new MultiSelectList(list, "Id", "Name", selectedValues, "Group"); } } }