Пример #1
0
        public IActionResult Update([FromBody] dynamic userRequest)
        {
            var id   = JwtService.GetUserId(HttpContext);
            var user = _dbContext.Users.FirstOrDefault(u => u.Id == id);

            if (user == null)
            {
                return(NotFound("用户不存在"));
            }
            user.Description = userRequest.description;
            try
            {
                _dbContext.Update(user);
                _dbContext.SaveChanges();
                return(Success("用户信息更新成功"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Error("用户信息更新失败"));
            }
        }
Пример #2
0
        public IActionResult Publish([FromBody] dynamic goodsRequest)
        {
            var userId = JwtService.GetUserId(HttpContext);
            var good   = new Good
            {
                UserId        = userId,
                BrandId       = goodsRequest.brand_id,
                CategoryId    = goodsRequest.category_id,
                Name          = goodsRequest.name,
                Enabled       = 1,
                OnSale        = 1, // TODO 管理员审核功能
                Price         = goodsRequest.price,
                Pic           = goodsRequest.pic,
                Description   = goodsRequest.description,
                OriginalPrice = goodsRequest.original_price,
                Stock         = goodsRequest.stock,
                DetailHtml    = goodsRequest.detail_html
            };
            var count = _dbContext.Categories.Count(c => c.Id == good.CategoryId);

            if (count == 0)
            {
                return(BadRequest("分类不存在")); // TODO 分类已被禁用
            }
            if (_dbContext.Goods.Count(g => g.Name == good.Name && g.UserId == good.UserId && g.OnSale == 1) == 1)
            {
                return(BadRequest("你已有一个同名商品在销售中"));
            }
            try
            {
                _dbContext.Goods.Add(good);
                _dbContext.SaveChanges();
                return(Success("发布成功", good.Id));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Error("发布失败"));
            }
        }