protected override async Task OnInitializedAsync()
        {
            var principal = _httpContextAccessor.HttpContext.User;

            userId       = principal.FindFirstValue(ClaimTypes.NameIdentifier);
            shoppingCart = await _shoppingCartService.GetShoppingCart(userId);

            List <ShoppingCartItem> shoppingCartItemsData = await _shoppingCartItemService.GetShoppingCartItems(shoppingCart.ShoppingCartId);

            for (int i = 0; i < shoppingCartItemsData.Count; i++)
            {
                ProductImage productImage = await _productImageService.GetProductImageOfProductId(shoppingCartItemsData[i].ProductId);

                Product product = await _productService.GetProduct(shoppingCartItemsData[i].ProductId);

                ShoppingCartItemDTO shoppingCartItemDTO = new ShoppingCartItemDTO
                {
                    DTOId                = i,
                    IfDelete             = false,
                    DeleteConfirmed      = false,
                    ShoppingCartItemData = shoppingCartItemsData[i],
                    ThumbImg             = productImage.ImageUrl,
                    Product              = product,
                    PreviousQuantity     = shoppingCartItemsData[i].Quantity
                };

                totalPrice += shoppingCartItemDTO.ShoppingCartItemData.Quantity * shoppingCartItemDTO.Product.Price;

                shoppingCartItems.Add(shoppingCartItemDTO);
            }
        }
        public async Task AddToCart(int productId, int quantity = 1)
        {
            ProductDTO product = await _productService.GetProductById(productId);

            if (product != null)
            {
                ShoppingCartItemDTO cartItem = new ShoppingCartItemDTO()
                {
                    Quantity       = quantity,
                    ShoppingCartId = GetCartId(),
                    ProductId      = productId
                };
                await _shoppingCartService.AddToCart(cartItem);
            }
        }
Exemplo n.º 3
0
        // *******************************************************************************************************************************
        #region -  Item  -

        public Task <string> InsertItemAsync(ShoppingCartItemDTO dto)
        {
            return(base.TryExecuteAsync(@"INSERT INTO ShoppingCartItem
(
  Cart_ID
, ItemNo
, Status
, Qty
, AddedOnUtc
)VALUES(
  @Cart_ID
, @ItemNo
, @Status
, @Qty
, @AddedOnUtc
)", dto));
        }
Exemplo n.º 4
0
        public async Task AddToCart(ShoppingCartItemDTO itemDto)
        {
            ShoppingCartItem item = _mapper.Map <ShoppingCartItem>(itemDto);

            ShoppingCartItem shoppingCartItem = await _unitOfWork
                                                .ShoppingCartRepository.GetByCartAndProductIds(itemDto.ShoppingCartId, itemDto.ProductId);

            if (shoppingCartItem == null)
            {
                await _unitOfWork.ShoppingCartRepository.AddAsync(item);
            }
            else
            {
                shoppingCartItem.Quantity += 1;
                _unitOfWork.ShoppingCartRepository.Update(shoppingCartItem);
            }

            await _unitOfWork.CommitAsync();
        }
        public string AddItem(int id)
        {
            var shoppingCartItem = GetShoppingCartItem(id);

            if (shoppingCartItem != null)
            {
                shoppingCartItem.Quantity++;
            }
            else
            {
                shoppingCartItem = ShoppingCartItemDTO.Create(
                    _productsRepo.GetById(id),
                    1
                    );
                _shoppingCart.ShoppingCartItems.Add(shoppingCartItem);
            }
            CalculatePrice();
            SaveShoppingCartToSession();
            return(shoppingCartItem.Product.Name);
        }
Exemplo n.º 6
0
        private ShoppingCartDTO GetShoppingCart()
        {
            // turn the shopping cart file into a shopping cart

            var lines = File.ReadAllLines("ShoppingCart.txt");

            ShoppingCartDTO shoppingCart = new ShoppingCartDTO();

            shoppingCart.ShoppingCartItems = new List <ShoppingCartItemDTO>();

            foreach (string line in lines)
            {
                if (!String.IsNullOrWhiteSpace(line.Trim()))
                {
                    ShoppingCartItemDTO cartItem = new ShoppingCartItemDTO {
                        ProductCode = line
                    };
                    shoppingCart.ShoppingCartItems.Add(cartItem);
                }
            }

            return(shoppingCart);
        }
Exemplo n.º 7
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));
            }
        }
Exemplo n.º 8
0
 public Task <string> DeleteItemAsync(ShoppingCartItemDTO dto)
 {
     return(base.TryExecuteAsync(@"DELETE FROM ShoppingCartItem WHERE Cart_ID = @Cart_ID AND ItemNo = @ItemNo", dto));
 }
Exemplo n.º 9
0
 public Task <string> UpdateItemQtyAsync(ShoppingCartItemDTO dto)
 {
     return(base.TryExecuteAsync(@"UPDATE ShoppingCartItem SET Qty = @Qty WHERE Cart_ID = @Cart_ID AND ItemNo = @ItemNo", dto));
 }
Exemplo n.º 10
0
 public static ShoppingCartItem ToEntity(this ShoppingCartItemDTO dto, ShoppingCartItem entity)
 {
     return(Mapper.Map(dto, entity));
 }
Exemplo n.º 11
0
 public static ShoppingCartItem ToEntity(this ShoppingCartItemDTO dto)
 {
     return(Mapper.Map <ShoppingCartItemDTO, ShoppingCartItem>(dto));
 }