예제 #1
0
        public async Task <CartViewModel> Change(CartItemChangeViewModel item)
        {
            try
            {
                var customer = await _currentContext.GetCurrentCustomer();

                CartViewModel model = new CartViewModel(await InitAjax());
                RCart         cart  = await _cartService.GetFromCache(_currentContext.ClientId, EnumDefine.CartStatusEnum.New);

                if (cart == null)
                {
                    //int shardId= _commonService.get

                    var  command        = item.ToCommand(1, _currentContext.LanguageId, customer.Id);
                    long systemIdentity = await _commonService.GetNextId(typeof(Cart));

                    CartAddCommand cartAddCommand = new CartAddCommand(SystemDefine.DefaultVersion)
                    {
                        LanguageId     = _currentContext.LanguageId,
                        Code           = Common.Common.GenerateCodeFromId(systemIdentity),
                        StoreId        = ConfigSettingEnum.StoreId.GetConfig(),
                        ClientId       = _currentContext.ClientId,
                        CreatedUid     = customer.Id ?? string.Empty,
                        CartItem       = command,
                        CartItemDetail = new CartItemDetailAddCommand(SystemDefine.DefaultVersion)
                        {
                            ProductId = command.ProductId,
                            Name      = "Name",
                        }
                    };
                    await _cartService.Add(cartAddCommand);
                }
                else
                {
                    //if (cart.Version != item.Version)
                    //{
                    //    model.AddMessage(ResourceKey.Cart_IsChanged);
                    //    return model;
                    //}
                    CartItemChangeCommand command = item.ToCommand(1, _currentContext.LanguageId, customer.Id, cart);
                    await _cartService.Change(command);
                }
                cart = await _cartService.GetFromCache(_currentContext.ClientId, EnumDefine.CartStatusEnum.New);

                model.CartItems = cart.CartItemFulls?.Select(p => p.ToModel()).ToArray();
                return(model);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                throw e;
            }
        }
예제 #2
0
 public CartItem(CartItemChangeCommand command)
 {
     Id             = Common.Common.GenerateGuid();
     ProductId      = command.ProductId;
     Price          = command.Price;
     CreatedDateUtc = command.CreatedDateUtc;
     UpdatedDateUtc = command.CreatedDateUtc;
     CreatedUid     = command.UpdatedUid;
     UpdatedUid     = command.UpdatedUid;
     LanguageId     = command.LanguageId;
     StoreId        = command.StoreId;
     Status         = EnumDefine.CartStatusEnum.New;
 }
예제 #3
0
        public async Task <CommandResult> Change(CartItemChangeCommand command)
        {
            CommandResult commandResult = await _commandService.SendAndReceiveResult <CommandResult>(command);

            return(commandResult);
        }
        public async Task <ICommandResult> Handle(CartItemChangeCommand mesage)
        {
            try
            {
                var shard = await _shardingService.GetShardById(ShardGroup, mesage.ShardId);

                RCart rCart = await _cartService.GetFromDb(shard.ConnectionString, mesage.CartId);

                if (rCart == null)
                {
                    throw new MessageException(ResourceKey.Cart_NotFound);
                }
                Cart cart = new Cart(rCart);
                switch (mesage.Action)
                {
                case EnumDefine.CartActionEnum.AddNewItem:
                {
                    CartItem       cartItem = new CartItem(mesage);
                    CartItemDetail shoppingCartItemDetail = null;
                    if (mesage.CartItemDetail != null)
                    {
                        shoppingCartItemDetail = new CartItemDetail(mesage.CartItemDetail, mesage.LanguageId, mesage.UpdatedUid);
                    }
                    var newCartItems = cart.Add(cartItem, shoppingCartItemDetail, mesage.Quantity, out var newCartItemDetail);
                    _eventSender.Add(cart.Events);
                    await _cartService.Save(shard.ConnectionString, shard.Id, mesage.CartId, mesage.CartCode, mesage.Version, newCartItems, newCartItemDetail);
                }
                break;

                case EnumDefine.CartActionEnum.RemoveItem:
                {
                    var cartItemRemoved = cart.Remove(mesage.ProductId, mesage.Quantity, out var shoppingCartItemDetailsRemoved);
                    await _cartService.Remove(shard.ConnectionString, mesage.CartId, mesage.Version,
                                              cartItemRemoved?.Select(p => p.Id).ToArray(),
                                              shoppingCartItemDetailsRemoved?.Select(p => p.Id).ToArray());
                };
                    break;

                case EnumDefine.CartActionEnum.ChangeQuantity:
                {
                    var changeQuantity = mesage.Quantity;
                    if (changeQuantity > 0)
                    {
                        CartItem       cartItem = new CartItem(mesage);
                        CartItemDetail shoppingCartItemDetail = null;
                        if (mesage.CartItemDetail != null)
                        {
                            shoppingCartItemDetail = new CartItemDetail(mesage.CartItemDetail, mesage.LanguageId, mesage.UpdatedUid);
                        }
                        var newCartItems = cart.Add(cartItem, shoppingCartItemDetail, changeQuantity, out var newCartItemDetail);
                        _eventSender.Add(cart.Events);
                        await _cartService.Save(shard.ConnectionString, shard.Id, mesage.CartId, mesage.CartCode, mesage.Version, newCartItems, newCartItemDetail);
                    }
                    else
                    {
                        var cartItemRemoved = cart.Remove(mesage.ProductId, mesage.Quantity * -1, out var shoppingCartItemDetailsRemoved);
                        await _cartService.Remove(shard.ConnectionString, mesage.CartId, mesage.Version,
                                                  cartItemRemoved?.Select(p => p.Id).ToArray(),
                                                  shoppingCartItemDetailsRemoved?.Select(p => p.Id).ToArray());
                    }
                }
                break;
                }
                await _eventSender.Notify();

                ICommandResult result = new CommandResult()
                {
                    Message  = "",
                    ObjectId = string.Empty,
                    Status   = CommandResult.StatusEnum.Sucess
                };
                return(result);
            }
            catch (MessageException e)
            {
                e.Data["Param"] = mesage;
                ICommandResult result = new CommandResult()
                {
                    Message      = e.Message,
                    Status       = CommandResult.StatusEnum.Fail,
                    ResourceName = e.ResourceName
                };
                return(result);
            }
            catch (Exception e)
            {
                e.Data["Param"] = mesage;
                ICommandResult result = new CommandResult()
                {
                    Message = e.Message,
                    Status  = CommandResult.StatusEnum.Fail
                };
                return(result);
            }
        }