public async Task <IActionResult> Post(string customerId, [FromBody] AddItemDto item)
        {
            if (item == null)
            {
                return(ResponseMessage.BadRequest(Status.UnexpectedError, UnexpectedError));
            }

            var validator = new AddItemValidator();
            var results   = validator.Validate(item);

            if (!results.IsValid)
            {
                var error = results.Errors.FirstOrDefault();
                return(ResponseMessage.BadRequest(error.ErrorCode, error.ErrorMessage));
            }

            var actor = (PID)shoppingListsActorProvider.ActorInstance;

            var @event = await actor.RequestAsync <ShoppingListEvent>(new AddItem(customerId, item.Name, item.Quantity));

            switch (@event.Status)
            {
            case Status.ItemAdded: return(ResponseMessage.CreatedAtAction("Get", "ShoppingLists", new { name = item.Name }, @event.Status, ItemAdded));

            case Status.QuantityUpdated: return(ResponseMessage.Ok(@event.Status, ItemUpdatedWithNewQuantity));

            default: return(ResponseMessage.BadRequest(@event.Status, UnexpectedError));
            }
        }
Example #2
0
        public async Task <ActionResult <CartResponse> > Post(Guid cartId, [FromBody] AddItemRequest request)
        {
            var validationResult = new AddItemValidator().Validate(request);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var cart = await service.GetCart(cartId);

            if (cart == null)
            {
                return(NotFound());
            }

            var response = await service.AddItemToCart(cartId, request);

            return(CreatedAtRoute("GetCart", new { cartId }, response));
        }
        public async Task <IActionResult> AddItem(string basketId, [FromBody] AddItemRequest request)
        {
            var validation = new AddItemValidator().Validate(request);

            if (!validation.IsValid)
            {
                return(BadRequest(validation.Errors));
            }

            var basketReadModel = await queryProcessor.ProcessAsync(new ReadModelByIdQuery <BasketReadModel>(basketId), CancellationToken.None);

            if (basketReadModel == null)
            {
                return(NotFound());
            }

            try
            {
                var task = await commandBus.PublishAsync(
                    new AddItem(new BasketId(basketId), request.ProductName, request.Price, request.Quantity), CancellationToken.None);

                if (task.IsSuccess)
                {
                    return(CreatedAtRoute("GetBasket", new { basketId = basketId.ToString() }, basketId.ToString()));
                }
            }
            catch (BasketException ex)
            {
                return(new JsonResult(ex.Message)
                {
                    StatusCode = (int)HttpStatusCode.BadRequest
                });
            }

            return(BadRequest());
        }