Пример #1
0
        public JObject AlterGood(AlterGoodDto dto)
        {
            JObject jo      = new JObject();
            var     isExist = WebapiDbContext.Goods.Where(o => o.Id == dto.Id).FirstOrDefault();

            if (isExist == null)
            {
                jo["stateCode"] = 400;
                jo["message"]   = "商品不存在,无法修改!";
                return(jo);
            }
            else if (dto.Description.Length > 2000)
            {
                jo["stateCode"] = 400;
                jo["message"]   = "商品描述大于2000字符,无法修改!";
                return(jo);
            }
            else if (dto.Name == "" || dto.Name.Length > 100)
            {
                jo["stateCode"] = 400;
                jo["message"]   = "商品名称不合法,无法修改!";
                return(jo);
            }
            else if (dto.Price <= 0m)
            {
                jo["stateCode"] = 400;
                jo["message"]   = "商品价格不合法,无法修改!";
                return(jo);
            }
            else if (dto.TagsId.Count() > 5)
            {
                jo["stateCode"] = 400;
                jo["message"]   = "商品标签数超过五个,无法修改!";
                return(jo);
            }
            else
            {
                dto.Price = System.Decimal.Round(System.Decimal.Floor(dto.Price * 100) / 100, 2);
                var result = WebapiDbContext.GoodTags.Where(o => o.Good_id == dto.Id);
                foreach (Repository.Entity.Goodtags temp in result)
                {
                    WebapiDbContext.Remove(temp);
                }
                WebapiDbContext.SaveChanges();

                isExist.Name        = dto.Name;
                isExist.Price       = dto.Price;
                isExist.Description = dto.Description;
                var length = dto.TagsId.Length;
                for (var i = 0; i < length; i++)
                {
                    WebapiDbContext.GoodTags.Add(new Repository.Entity.Goodtags()
                    {
                        Tag_id  = dto.TagsId[i],
                        Good_id = dto.Id
                    });
                }
                WebapiDbContext.SaveChanges();
                jo["stateCode"] = 200;
                jo["message"]   = "success!";
                return(jo);
            }
        }
Пример #2
0
 public IActionResult AlterGood([FromServices] IWebapiService webapiService, [FromBody] AlterGoodDto dto)
 {
     return(Json(webapiService.AlterGood(dto)));
 }