Exemplo n.º 1
0
        public ActionResult Store([FromBody] OrderStoreViewModel viewModel)
        {
            var maybeOrder = _orderRepository.FindByNumber(viewModel.Number);

            if (maybeOrder.HasValue)
            {
                return(BadRequest());
            }

            var order = _mapper.Map <Order>(viewModel);

            _orderRepository.Add(order);

            _uow.Commit();

            return(CreatedAtAction(nameof(Show), new { number = order.Number }, _mapper.Map <OrderShowViewModel>(order)));
        }
Exemplo n.º 2
0
        public async Task <Result> Add(OrderStoreViewModel order)
        {
            try
            {
                var client = _clientFactory.CreateClient("Api");

                var json = JsonConvert.SerializeObject(order);

                var content = new StringContent(json, Encoding.UTF8, "application/json");

                var response = await client.PostAsync("Order", content);

                response.EnsureSuccessStatusCode();

                return(Result.Ok());
            }
            catch (HttpRequestException ex)
            {
                _logger.LogError(ex, string.Empty);

                return(Result.Fail(ex.Message));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Store([FromRoute] int productId)
        {
            if (HttpContext.Session.TryGetValue("@order-number", out var value))
            {
                var orderNumber = new Guid(value);

                var item = new OrderItemStoreViewModel
                {
                    ProductId = productId
                };

                var itemResult = await _orderService.AddItem(orderNumber, item);

                if (itemResult.IsFailure)
                {
                    TempData["Failure"] = "Não foi possível adicionar o produto no carrinho";

                    return(RedirectToAction("Index", "Product"));
                }

                TempData["Success"] = "Produto adicionado ao carrinho";

                var countResult = await _orderService.CountNumberOfItems(orderNumber);

                if (countResult.IsFailure)
                {
                    TempData["Failure"] = "Não foi possível atualizar o carrinho";

                    return(RedirectToAction("Index", "Product"));
                }

                HttpContext.Session.SetInt32("@order-items-count", countResult.Value);

                return(RedirectToAction("Index", "Product"));
            }

            var order = new OrderStoreViewModel
            {
                Number = Guid.NewGuid()
            };

            var orderCreateResult = await _orderService.Add(order);

            if (orderCreateResult.IsFailure)
            {
                TempData["Failure"] = "Não foi possível adicionar o produto no carrinho";

                return(RedirectToAction("Index", "Product"));
            }

            HttpContext.Session.Set("@order-number", order.Number.ToByteArray());

            var orderItem = new OrderItemStoreViewModel
            {
                ProductId = productId
            };

            var orderItemCreateResult = await _orderService.AddItem(order.Number, orderItem);

            if (orderItemCreateResult.IsFailure)
            {
                TempData["Failure"] = "Não foi possível adicionar o produto no carrinho";

                return(RedirectToAction("Index", "Product"));
            }

            TempData["Success"] = "Produto adicionado ao carrinho";

            var orderItemCountResult = await _orderService.CountNumberOfItems(order.Number);

            if (orderItemCountResult.IsFailure)
            {
                TempData["Failure"] = "Não foi possível atualizar o carrinho";

                return(RedirectToAction("Index", "Product"));
            }

            HttpContext.Session.SetInt32("@order-items-count", orderItemCountResult.Value);

            return(RedirectToAction("Index", "Product"));
        }