Exemplo n.º 1
0
        public async Task <CartExtendedResponse> Handle(CreateCartCommand request, CancellationToken cancellationToken)
        {
            var entity = new CartSession
            {
                Id    = Guid.NewGuid().ToString(),
                Items = request.ItemsIds.Select(item => new CartItem
                {
                    CartItemId = new Guid(item),
                    Quantity   = 1,
                }).ToList(),
                User = new CartUser
                {
                    Email = request.UserEmail,
                },
                ValidityDate = DateTimeOffset.Now.AddMonths(2),
            };

            var result = await _repository.AddOrUpdateAsync(entity);

            var response = _mapper.Map <CartExtendedResponse>(result);

            var tasks = response.Items
                        .Select(async x => await _catalogService.EnrichCartItem(x, cancellationToken));

            response.Items = await Task.WhenAll(tasks);

            return(response);
        }
Exemplo n.º 2
0
        public async Task <CartExtendedResponse> Handle(UpdateCartItemQuantityCommand command,
                                                        CancellationToken cancellationToken)
        {
            var cartDetail = await _repository.GetAsync(command.CartId);

            if (command.IsAddOperation)
            {
                cartDetail.Items.FirstOrDefault(_ => _.CartItemId == command.CartItemId)?.IncreaseQuantity();
            }
            else
            {
                cartDetail.Items.FirstOrDefault(_ => _.CartItemId == command.CartItemId)?.DecreaseQuantity();
            }

            var cartItemsList = cartDetail.Items.ToList();

            cartItemsList.RemoveAll(x => x.Quantity <= 0);

            cartDetail.Items = cartItemsList;

            await _repository.AddOrUpdateAsync(cartDetail);

            var response = _mapper.Map <CartExtendedResponse>(cartDetail);

            var tasks = response.Items
                        .Select(async x => await _catalogService.EnrichCartItem(x, cancellationToken));

            response.Items = await Task.WhenAll(tasks);

            return(response);
        }
        public async Task <CartExtendedResponse> Handle(GetCartCommand command, CancellationToken cancellationToken)
        {
            var result = await _repository.GetAsync(command.Id);

            var extendedResponse = _mapper.Map <CartExtendedResponse>(result);

            var tasks = extendedResponse.Items
                        .Select(async x => await _catalogService.EnrichCartItem(x, cancellationToken));

            extendedResponse.Items = await Task.WhenAll(tasks);

            return(extendedResponse);
        }