Exemplo n.º 1
0
        public AddItemToCartResponse Handle(AddItemToCartRequest input)
        {
            var cart = _cartRepository.Get(input.CartId);

            if (cart is null)
            {
                throw new NotFoundException("Couldn't find the cart");
            }

            var item = _itemRepository.Get(input.ItemId);

            if (item is null)
            {
                throw new NotFoundException("Couldn't find the item");
            }

            var rules = _itemSaleRuleRepository.GetForItem(item.Id);

            rules.Validate(cart, item, input.Quantity);

            var cartItem = cart.AddItemToCart(item, input.Quantity);

            _cartRepository.Save(cart);

            _listener.OnAdded(cart, item, cartItem);

            // ...
            return(new AddItemToCartResponse());
        }
Exemplo n.º 2
0
        public Unit Handle(AddItemToCartRequest input)
        {
            var maybeCart = _cartRepository.Get(input.CartId);
            var cart      = maybeCart.ValueOr(
                () => throw new DomainException(new ErrorDetail.NotFound("Couldn't find the cart")));

            var item = _itemRepository.Get(input.ItemId);

            if (item is null)
            {
                throw new DomainException(new ErrorDetail.NotFound("Couldn't find the item"));
            }

            var rules = _itemSaleRuleRepository.GetForItem(item.Id);

            rules.Validate(cart, item, input.Quantity);

            var cartItem = cart.AddItemToCart(item, input.Quantity);

            _cartRepository.Save(cart);

            _listener.OnAdded(cart, item, cartItem);

            return(Unit.Instance);
        }
Exemplo n.º 3
0
        public Either <Error, Unit> Handle(AddItemToCartRequest input)
        {
            var maybeCart = _cartRepository.Get(input.CartId);

            if (!maybeCart.TryGetValue(out var cart))
            {
                return(Either.Left <Error, Unit>(new Error.NotFound("Couldn't find the cart")));
            }

            var item = _itemRepository.Get(input.ItemId);

            if (item is null)
            {
                return(Either.Left <Error, Unit>(new Error.NotFound("Couldn't find the item")));
            }

            var rules       = _itemSaleRuleRepository.GetForItem(item.Id);
            var rulesResult = rules.Validate(cart, item, input.Quantity);


            if (rulesResult is Either <Error, Unit> .Left)
            {
                return(rulesResult);
            }

            var cartItemResult = cart.AddItemToCart(item, input.Quantity);

            if (cartItemResult is Either <Error, CartItem> .Left cartItemErrorResult)
            {
                return(Either.Left <Error, Unit>(cartItemErrorResult.Value));
            }

            _cartRepository.Save(cart);

            _listener.OnAdded(cart, item, ((Either <Error, CartItem> .Right)cartItemResult).Value);

            return(Either.Right <Error, Unit>(Unit.Instance));
        }
        public Either <Error, Unit> Handle(AddItemToCartRequest input)
        {
            var maybeCart = _cartRepository.Get(input.CartId);

            if (!maybeCart.TryGetValue(out var cart))
            {
                return(Either.Left <Error, Unit>(new Error.NotFound("Couldn't find the cart")));
            }
            var item = _itemRepository.Get(input.ItemId);

            if (item is null)
            {
                return(Either.Left <Error, Unit>(new Error.NotFound("Couldn't find the item")));
            }

            var rules = _itemSaleRuleRepository.GetForItem(item.Id);

            return(rules
                   .Validate(cart, item, input.Quantity)
                   .FlatMap(_ => cart.AddItemToCart(item, input.Quantity))
                   .Do(_ => _cartRepository.Save(cart))
                   .Do(cartItem => _listener.OnAdded(cart, item, cartItem))
                   .Map(_ => Unit.Instance));
        }
        public Result <Unit> Handle(AddItemToCartRequest input)
        {
            var maybeCart = _cartRepository.Get(input.CartId);

            if (!maybeCart.TryGetValue(out var cart))
            {
                return(Result <Unit> .Error.NotFound.New("Couldn't find the cart"));
            }
            var maybeItem = _itemRepository.Get(input.ItemId);

            if (!maybeItem.TryGetValue(out var item))
            {
                return(Result <Unit> .Error.NotFound.New("Couldn't find the item"));
            }

            var rules       = _itemSaleRuleRepository.GetForItem(item.Id);
            var rulesResult = rules.Validate(cart, item, input.Quantity);

            if (rulesResult is Result <Unit> .Error)
            {
                return(rulesResult);
            }

            var cartItemResult = cart.AddItemToCart(item, input.Quantity);

            if (cartItemResult is Result <CartItem> .Error cartItemErrorResult)
            {
                return(cartItemErrorResult.AsErrorOf <Unit>());
            }

            _cartRepository.Save(cart);

            _listener.OnAdded(cart, item, cartItemResult.Value);

            return(Result <Unit> .Success.New(Unit.Instance));
        }