using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Results; using System.Web; using System.IO; using MongoDB.Bson; using MongoDB.Driver; using Newtonsoft.Json.Linq; using ShadowEditor.Server.Base; using ShadowEditor.Server.Helpers; using ShadowEditor.Model.System; using ShadowEditor.Server.CustomAttribute; namespace ShadowEditor.Server.Controllers.System { /// /// 角色控制器 /// public class RoleController : ApiBase { /// /// 获取列表 /// /// /// /// /// [HttpGet] [Authority(OperatingAuthority.LIST_ROLE)] public JsonResult List(int pageSize = 20, int pageNum = 1, string keyword = "") { var mongo = new MongoHelper(); var filter = Builders.Filter.Ne("Status", -1); if (!string.IsNullOrEmpty(keyword)) { var filter1 = Builders.Filter.Regex("Name", keyword); filter = Builders.Filter.And(filter, filter1); } var sort = Builders.Sort.Descending("ID"); var total = mongo.Count(Constant.RoleCollectionName, filter); var docs = mongo.FindMany(Constant.RoleCollectionName, filter) .Sort(sort) .Skip(pageSize * (pageNum - 1)) .Limit(pageSize) .ToList(); var rows = new List(); foreach (var doc in docs) { rows.Add(new RoleModel { ID = doc["ID"].ToString(), Name = doc["Name"].ToString(), CreateTime = doc["CreateTime"].ToLocalTime(), UpdateTime = doc["UpdateTime"].ToLocalTime(), Status = doc["Status"].ToInt32(), }); } return Json(new { Code = 200, Msg = "Get Successfully!", Data = new { total, rows, }, }); } /// /// 添加 /// /// /// [HttpPost] [Authority(OperatingAuthority.ADD_ROLE)] public JsonResult Add(RoleEditModel model) { if (string.IsNullOrEmpty(model.Name)) { return Json(new { Code = 300, Msg = "Name is not allowed to be empty." }); } if (model.Name.StartsWith("_")) { return Json(new { Code = 300, Msg = "Name is not allowed to start with _." }); } var mongo = new MongoHelper(); var filter = Builders.Filter.Eq("Name", model.Name); var count = mongo.Count(Constant.RoleCollectionName, filter); if (count > 0) { return Json(new { Code = 300, Msg = "The name is already existed.", }); } var now = DateTime.Now; var doc = new BsonDocument { ["ID"] = ObjectId.GenerateNewId(), ["Name"] = model.Name, ["CreateTime"] = now, ["UpdateTime"] = now, ["Status"] = 0, }; mongo.InsertOne(Constant.RoleCollectionName, doc); return Json(new { Code = 200, Msg = "Saved successfully!" }); } /// /// 编辑 /// /// /// [HttpPost] [Authority(OperatingAuthority.EDIT_ROLE)] public JsonResult Edit(RoleEditModel model) { var objectId = ObjectId.GenerateNewId(); if (!string.IsNullOrEmpty(model.ID) && !ObjectId.TryParse(model.ID, out objectId)) { return Json(new { Code = 300, Msg = "ID is not allowed." }); } if (string.IsNullOrEmpty(model.Name)) { return Json(new { Code = 300, Msg = "Name is not allowed to be empty." }); } if (model.Name.StartsWith("_")) { return Json(new { Code = 300, Msg = "Name is not allowed to start with _." }); } var mongo = new MongoHelper(); var filter = Builders.Filter.Eq("ID", objectId); var update1 = Builders.Update.Set("Name", model.Name); var update2 = Builders.Update.Set("UpdateTime", DateTime.Now); var update = Builders.Update.Combine(update1, update2); mongo.UpdateOne(Constant.RoleCollectionName, filter, update); return Json(new { Code = 200, Msg = "Saved successfully!" }); } /// /// 删除 /// /// /// [HttpPost] [Authority(OperatingAuthority.DELETE_ROLE)] public JsonResult Delete(string ID) { var objectId = ObjectId.GenerateNewId(); if (!string.IsNullOrEmpty(ID) && !ObjectId.TryParse(ID, out objectId)) { return Json(new { Code = 300, Msg = "ID is not allowed." }); } var mongo = new MongoHelper(); var filter = Builders.Filter.Eq("ID", objectId); var doc = mongo.FindOne(Constant.RoleCollectionName, filter); if (doc == null) { return Json(new { Code = 300, Msg = "The asset is not existed!" }); } var update = Builders.Update.Set("Status", -1); mongo.UpdateOne(Constant.RoleCollectionName, filter, update); return Json(new { Code = 200, Msg = "Delete successfully!" }); } } }