2018-10-30 13:07:29 +08:00
|
|
|
|
using Bootstrap.Security;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-31 12:08:57 +08:00
|
|
|
|
using System.Linq;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Menu : DataAccess.Menu
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName)
|
|
|
|
|
{
|
2018-10-31 12:08:57 +08:00
|
|
|
|
var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");
|
|
|
|
|
|
2019-01-15 12:52:09 +08:00
|
|
|
|
var menus = DbManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList();
|
2018-10-31 12:08:57 +08:00
|
|
|
|
menus.ForEach(m =>
|
|
|
|
|
{
|
|
|
|
|
m.CategoryName = dicts.FirstOrDefault(d => d.Code == m.Category)?.Name;
|
|
|
|
|
if (m.ParentId != "0") m.ParentName = menus.FirstOrDefault(p => p.Id == m.ParentId)?.Name;
|
|
|
|
|
});
|
|
|
|
|
return menus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Save(BootstrapMenu p)
|
2018-10-31 12:08:57 +08:00
|
|
|
|
{
|
|
|
|
|
if (p.Id == "0")
|
|
|
|
|
{
|
|
|
|
|
p.Id = null;
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Menus.InsertOne(p);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var update = Builders<BootstrapMenu>.Update.Set(md => md.ParentId, p.ParentId)
|
|
|
|
|
.Set(md => md.Name, p.Name)
|
|
|
|
|
.Set(md => md.Order, p.Order)
|
|
|
|
|
.Set(md => md.Icon, p.Icon)
|
|
|
|
|
.Set(md => md.Url, p.Url)
|
|
|
|
|
.Set(md => md.Category, p.Category)
|
|
|
|
|
.Set(md => md.Target, p.Target)
|
|
|
|
|
.Set(md => md.IsResource, p.IsResource)
|
2019-01-11 23:20:28 +08:00
|
|
|
|
.Set(md => md.Application, p.Application);
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Menus.UpdateOne(md => md.Id == p.Id, update);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Delete(IEnumerable<string> value)
|
2018-10-31 12:08:57 +08:00
|
|
|
|
{
|
|
|
|
|
var list = new List<WriteModel<BootstrapMenu>>();
|
|
|
|
|
foreach (var id in value)
|
|
|
|
|
{
|
|
|
|
|
list.Add(new DeleteOneModel<BootstrapMenu>(Builders<BootstrapMenu>.Filter.Eq(g => g.Id, id)));
|
|
|
|
|
}
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Menus.BulkWrite(list);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
return true;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
2018-10-31 16:50:55 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-15 12:52:09 +08:00
|
|
|
|
public override IEnumerable<object> RetrieveMenusByRoleId(string roleId) => DbManager.Roles.Find(md => md.Id == roleId).FirstOrDefault().Menus.Select(m => new { Id = m });
|
2018-10-31 16:50:55 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <param name="menuIds"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
|
|
|
|
|
{
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Roles.FindOneAndUpdate(md => md.Id == roleId, Builders<Role>.Update.Set(md => md.Menus, menuIds));
|
2018-10-31 16:50:55 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|