Exemplo n.º 1
0
        public async Task <IActionResult> ChooseItem(AddItemViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                if (!User.Identity.IsAuthenticated)
                {
                    return(RedirectToAction("Login", "Account"));
                }

                else
                {
                    try
                    {
                        await _orderRepository.AddItemToOrderAsync(model, this.User.Identity.Name);

                        return(this.RedirectToAction("InStock"));
                    }

                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
            }
            return(this.View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddProduct(AddItemViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                await _orderRepository.AddItemToOrderAsync(model, this.User.Identity.Name);

                return(this.RedirectToAction("Create"));
            }

            return(this.View(model));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddProduct(AddItemViewModel model, string productId)
        {
            if (!string.IsNullOrEmpty(productId))
            {
                model.ProductId = Convert.ToInt32(productId);
            }

            if (ModelState.IsValid)
            {
                await _orderRepository.AddItemToOrderAsync(model, User.Identity.Name, User.IsInRole("ReSeller"));

                return(RedirectToAction("Create"));
            }

            return(View(model));
        }
        public async Task <ActionResult <OrderModel> > AddItemToOrder(int orderId, AddItemModel addItemModel)
        {
            try
            {
                _logger.LogInformation("{Username} has started adding {Amount} items with id {ItemId} to order with id {OrderId}",
                                       addItemModel.Username, addItemModel.Amount, addItemModel.ItemId, orderId);

                var order = await _orderRepository.AddItemToOrderAsync(
                    orderId,
                    addItemModel.ItemId,
                    addItemModel.Amount,
                    addItemModel.Username);

                var location = _linkGenerator.GetPathByAction(
                    "GetOrderById",
                    "Orders",
                    new { orderId = order.OrderId });

                var reserveItemsDto = new ReserveItemsDto(order, addItemModel);
                _rabbitManager.Publish(reserveItemsDto, "OrderService_ReserveItemsExchange", ExchangeType.Direct, "ReserveItems");

                if (orderId == 0)
                {
                    _logger.LogInformation("{Username} has successfuly added {Amount} items with id {ItemId} to a newly created order with id {OrderId}",
                                           addItemModel.Username, addItemModel.Amount, addItemModel.ItemId, order.OrderId);

                    return(Created(location, _mapper.Map <OrderModel>(order)));
                }
                else
                {
                    _logger.LogInformation("{Username} has successfuly added {Amount} items with id {ItemId} to an existing order with id {OrderId}",
                                           addItemModel.Username, addItemModel.Amount, addItemModel.ItemId, order.OrderId);

                    return(_mapper.Map <OrderModel>(order));
                }
            }
            catch (Exception e)
            {
                _logger.LogInformation("{Username} has failed to add {Amount} items with id {ItemId} to an order with id {OrderId}",
                                       addItemModel.Username, addItemModel.Amount, addItemModel.ItemId, orderId);

                return(BadRequest(e.Message));
            }
        }