Esempio n. 1
0
        public async Task <IHttpActionResult> Create([FromBody] ShoppingCartItemDTO ShoppingCartItemDto)
        {
            var result = new ApiResult <string>();
            var userId = User.Identity.GetUserId();

            if (ShoppingCartItemService
                .GetAll()
                .Count(x => x.CustomerId == userId &&
                       x.Status == (int)ShoppingCartStatus.ShoppingCart &&
                       !x.Deleted) > 20)
            {
                result.Info = "最多添加20条购物车信息!";
                result.Code = 2;
                return(Ok(result));
            }

            if (!ModelState.IsValid)
            {
                return(Ok(new ApiResult <System.Web.Http.ModelBinding.ModelStateDictionary>()
                {
                    Code = 3,
                    Data = ModelState,
                    Info = "请仔细填写表单!"
                }));
            }
            try
            {
                var hasExsit = ShoppingCartItemService
                               .GetAll()
                               .Any(x => x.CustomerId == userId &&
                                    x.Status == (int)ShoppingCartStatus.ShoppingCart &&
                                    !x.Deleted &&
                                    x.ProductId == ShoppingCartItemDto.ProductId &&
                                    x.AttributesIds == ShoppingCartItemDto.AttributesIds
                                    );

                if (hasExsit)
                {
                    var entity = await ShoppingCartItemService
                                 .GetAll()
                                 .SingleOrDefaultAsync(x => x.CustomerId == userId &&
                                                       x.Status == (int)ShoppingCartStatus.ShoppingCart &&
                                                       !x.Deleted &&
                                                       x.ProductId == ShoppingCartItemDto.ProductId &&
                                                       x.AttributesIds == ShoppingCartItemDto.AttributesIds
                                                       );

                    entity.Quantity += ShoppingCartItemDto.Quantity;
                    entity.Price    += ShoppingCartItemDto.Price;
                    entity.LastTime  = DateTime.Now;
                    await ShoppingCartItemService.UpdateAsync(entity);

                    return(Ok(new ApiResult <ShoppingCartItemDTO>()
                    {
                        Data = entity.ToModel()
                    }));
                }
                else
                {
                    var entity = ShoppingCartItemDto.ToEntity();
                    entity.ShoppingCartStatus = ShoppingCartStatus.ShoppingCart;
                    entity.CustomerId         = userId;
                    entity.CreateTime         = DateTime.Now;
                    await ShoppingCartItemService.InsertAsync(entity);

                    return(Ok(new ApiResult <ShoppingCartItemDTO>()
                    {
                        Data = entity.ToModel()
                    }));
                }
            }
            catch (Exception ex)
            {
                result.Info = "添加购物车异常!";
                result.Code = 1;
                result.Data = ex.Message;
                return(Ok(result));
            }
        }