public IHttpActionResult Post(CategorySaveModel model)
        {
            if (!this.User.IsInRole("Admin"))
            {
                return(this.StatusCode(HttpStatusCode.Unauthorized));
            }

            var category = this.data.Categories.All().FirstOrDefault(x => x.Name == model.Name);

            if (category != null)
            {
                var id  = category.Id;
                var uri = new Uri($"http://localhost:58368/api/categories/{id}");
                return(this.Redirect(uri));
            }

            var user          = this.data.Users.All().FirstOrDefault();
            var categoryToAdd = new Category
            {
                Name   = model.Name,
                Author = user
            };

            this.data.Categories.Add(categoryToAdd);
            this.data.Savechanges();
            return(this.Created("api/categories", new
            {
                CategoryId = categoryToAdd
            }));
        }
        public JsonResult Save(CategorySaveModel model)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                return(Json(new
                {
                    Code = 300,
                    Msg = "Name is not allowed to be empty."
                }));
            }

            if (model.Type == null)
            {
                return(Json(new
                {
                    Code = 300,
                    Msg = "Type is not allowed to be empty!"
                }));
            }

            var mongo = new MongoHelper();

            if (string.IsNullOrEmpty(model.ID))
            {
                var doc = new BsonDocument();
                doc["Name"] = model.Name;
                doc["Type"] = model.Type.Value.ToString();

                if (ConfigHelper.EnableAuthority)
                {
                    var user = UserHelper.GetCurrentUser();

                    if (user != null)
                    {
                        doc["UserID"] = user.ID;
                    }
                }

                mongo.InsertOne(Constant.CategoryCollectionName, doc);
            }
            else
            {
                var filter = Builders <BsonDocument> .Filter.Eq("_id", ObjectId.Parse(model.ID));

                var update1 = Builders <BsonDocument> .Update.Set("Name", BsonString.Create(model.Name));

                var update2 = Builders <BsonDocument> .Update.Set("Type", BsonString.Create(model.Type.Value.ToString()));

                var update = Builders <BsonDocument> .Update.Combine(update1, update2);

                mongo.UpdateOne(Constant.CategoryCollectionName, filter, update);
            }

            return(Json(new
            {
                Code = 200,
                Msg = "Saved successfully!"
            }));
        }
示例#3
0
        public JsonResult Save(CategorySaveModel model)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                return(Json(new
                {
                    Code = 300,
                    Msg = "名称不允许为空!"
                }));
            }

            if (model.Type == null)
            {
                return(Json(new
                {
                    Code = 300,
                    Msg = "类型不允许为空!"
                }));
            }

            var mongo = new MongoHelper();

            if (string.IsNullOrEmpty(model.ID))
            {
                var doc = new BsonDocument();
                doc["Name"] = model.Name;
                doc["Type"] = model.Type.Value.ToString();
                mongo.InsertOne(Constant.CategoryCollectionName, doc);
            }
            else
            {
                var filter = Builders <BsonDocument> .Filter.Eq("_id", ObjectId.Parse(model.ID));

                var update1 = Builders <BsonDocument> .Update.Set("Name", BsonString.Create(model.Name));

                var update2 = Builders <BsonDocument> .Update.Set("Type", BsonString.Create(model.Type.Value.ToString()));

                var update = Builders <BsonDocument> .Update.Combine(update1, update2);

                mongo.UpdateOne(Constant.CategoryCollectionName, filter, update);
            }

            return(Json(new
            {
                Code = 200,
                Msg = "保存成功!"
            }));
        }