public JsonResult Add(PluginEditModel model) { if (string.IsNullOrEmpty(model.Name)) { return(Json(new { Code = 300, Msg = "Name is not allowed to be empty." })); } var mongo = new MongoHelper(); var filter = Builders <BsonDocument> .Filter.Eq("Name", model.Name); var count = mongo.Count(Constant.PluginCollectionName, filter); if (count > 0) { return(Json(new { Code = 300, Msg = "The name is already existed.", })); } if (string.IsNullOrEmpty(model.Source)) { model.Source = ""; } if (string.IsNullOrEmpty(model.Description)) { model.Description = ""; } var now = DateTime.Now; var doc = new BsonDocument { ["ID"] = ObjectId.GenerateNewId(), ["Name"] = model.Name, ["Source"] = model.Source, ["CreateTime"] = now, ["UpdateTime"] = now, ["Description"] = model.Description, ["Status"] = 0, }; mongo.InsertOne(Constant.PluginCollectionName, doc); return(Json(new { Code = 200, Msg = "Saved successfully!" })); }
public JsonResult Edit(PluginEditModel 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." })); } var mongo = new MongoHelper(); var filter = Builders <BsonDocument> .Filter.Eq("ID", objectId); var doc = mongo.FindOne(Constant.PluginCollectionName, filter); if (doc == null) { return(Json(new { Code = 300, Msg = "The plugin is not existed." })); } var update1 = Builders <BsonDocument> .Update.Set("Name", model.Name); var update2 = Builders <BsonDocument> .Update.Set("Source", model.Source); var update3 = Builders <BsonDocument> .Update.Set("UpdateTime", DateTime.Now); var update4 = Builders <BsonDocument> .Update.Set("Description", model.Description); var update = Builders <BsonDocument> .Update.Combine(update1, update2, update3, update4); mongo.UpdateOne(Constant.PluginCollectionName, filter, update); return(Json(new { Code = 200, Msg = "Saved successfully!" })); }