using Application.Domain.Entities; using Application.Models; using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; 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] [Area(nameof(Admin))] public class PermissionController : CrudController, EditPermissionModel, EditPermissionModel> { private readonly IRepository _permissionCategoryRepo; public PermissionController(IRepository repo, IRepository ajax) : base(repo) { this._permissionCategoryRepo = ajax; } 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.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), model.CategoryId.HasValue); } public override void ToDisplayModel(Permission entity, EditPermissionModel model) { this.ViewData.Add(entity.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"); } } }