using Masuit.MyBlogs.Core.Common; using Masuit.MyBlogs.Core.Infrastructure.Services.Interface; using Masuit.MyBlogs.Core.Models.DTO; using Masuit.MyBlogs.Core.Models.Entity; using Masuit.MyBlogs.Core.Models.Enum; using Masuit.Tools.Systems; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; namespace Masuit.MyBlogs.Core.Controllers { /// /// 菜单管理 /// public class MenuController : AdminController { /// /// 菜单数据服务 /// public IMenuService MenuService { get; set; } /// /// 获取菜单 /// /// public ActionResult GetMenus() { var menus = MenuService.GetAll(m => m.ParentId).ThenBy(m => m.Sort).ToList(); return ResultData(menus); } /// /// 获取菜单类型 /// /// public ActionResult GetMenuType() { var array = Enum.GetValues(typeof(MenuType)); var list = new List(); foreach (Enum e in array) { list.Add(new { e, name = e.GetDisplay() }); } return ResultData(list); } /// /// 删除菜单 /// /// /// public ActionResult Delete(int id) { var menus = MenuService.GetChildrenMenusByParentId(id); bool b = MenuService.DeleteEntitiesSaved(menus); return ResultData(null, b, b ? "删除成功" : "删除失败"); } /// /// 保持菜单 /// /// /// public ActionResult Save(MenuInputDto model) { if (string.IsNullOrEmpty(model.Icon) || !model.Icon.Contains("/")) { model.Icon = null; } var m = MenuService.GetById(model.Id); if (m == null) { var menu = MenuService.AddEntitySaved(model.Mapper()); if (menu != null) { return ResultData(menu, true, "添加成功"); } return ResultData(null, false, "添加失败"); } Mapper.Map(model, m); bool b = MenuService.UpdateEntitySaved(m); return ResultData(null, b, b ? "修改成功" : "修改失败"); } } }