Example #1
0
        public async Task <ServiceResult> AddItemToCartAsync(Guid cartId, Guid shopItemDetailId, int count)
        {
            Check.Positive(count, nameof(count));

            var cartExist = await this.ExistAsync(x => x.Id == cartId);

            if (cartExist == false)
            {
                return(ServiceResultFactory.NotFound);
            }

            var shopItemDetail = await _context.ShopItemDetails.FirstOrDefaultAsync(x => x.Id == shopItemDetailId);

            if (shopItemDetail == null)
            {
                return(ServiceResultFactory.NotFound);
            }

            var oldCartShopItem = await _context.CartShopItems
                                  .FirstOrDefaultAsync(x => x.CartId == cartId && x.ShopItemDetailId == shopItemDetailId);

            if (oldCartShopItem == null)
            {
                // add new item
                var newCartShopItem = new CartShopItem
                {
                    CartId           = cartId,
                    ShopItemDetailId = shopItemDetailId,
                    Price            = shopItemDetail.Price,
                    Count            = count
                };
                _context.CartShopItems.Add(newCartShopItem);
            }
            else
            {
                // increase count and update price
                oldCartShopItem.Count += count;
                oldCartShopItem.Price  = shopItemDetail.Price;
                _context.CartShopItems.Update(oldCartShopItem);
            }

            var result = await _context.SaveAsync(nameof(AddItemToCartAsync));

            var taskModel = this.FindByAsync(x => x.Id == cartId);
            await result.SetModelIfSuccessAsync(taskModel);

            return(result);
        }
        public static CartShopItemViewModel Map(CartShopItem model)
        {
            if (model == null || model.ShopItemDetail == null || model.ShopItemDetail.ShopItem == null)
            {
                return(null);
            }

            return(new CartShopItemViewModel
            {
                Count = model.Count,
                Price = model.Price,
                ShopItemDetailId = model.ShopItemDetailId,
                Kind = model.ShopItemDetail.Kind,
                Title = model.ShopItemDetail.ShopItem.Title,
                ImagePath = Constants.GetFullPathToImage(model.ShopItemDetail.ShopItem.ImagePath)
            });
        }
Example #3
0
        public static List <Cart> LoadCarts(IEnumerable <ShopItemDetail> shopItemDetails)
        {
            var carts = new List <Cart>();

            foreach (var shopItemDetail in shopItemDetails)
            {
                var cartShopItem = new CartShopItem
                {
                    Count            = 1,
                    Price            = shopItemDetail.Price,
                    ShopItemDetailId = shopItemDetail.Id
                };
                carts.Add(new Cart
                {
                    CartShopItems = new List <CartShopItem>()
                    {
                        cartShopItem
                    }
                });
            }

            return(carts);
        }