コード例 #1
0
        public ActionResult Post([FromBody] ShoppingDTO value)
        {
            TryValidateModel(value);

            try
            {
                var            shopping    = this.mapper.Map <Shopping>(value);
                decimal        totalPrice  = 0;
                List <Product> productList = new List <Product>();
                foreach (Product product in shopping.Products)
                {
                    Product productUpdated = this.productService.Get(product.Id);
                    if (productUpdated.Stock >= product.Quantity)
                    {
                        productList.Add(product);
                        totalPrice = totalPrice + (product.Quantity * product.SalePrice);
                        this.productService.DescontarStock(product.Id, product.Quantity);
                    }
                }
                if (productList.Count == 0)
                {
                    return(Ok(new { Success = false, Message = "Ningún producto seleccionado tiene stock disponible" }));
                }
                shopping.TotalPrice = totalPrice;
                shopping.Products   = productList;
                this.shoppingService.Create(shopping);
                value = this.mapper.Map <ShoppingDTO>(shopping);
                return(Ok(new { Success = true, Message = "", data = value }));
            }
            catch
            {
                return(Ok(new { Success = false, Message = "Los productos no están disponibles" }));
            }
        }
コード例 #2
0
        public async Task <IActionResult> BuyDrinks(ShoppingInsertViewModel viewModel)
        {
            var configuration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <ShoppingInsertViewModel, ShoppingDTO>();
            });

            IMapper     mapper = configuration.CreateMapper();
            ShoppingDTO dto    = mapper.Map <ShoppingDTO>(viewModel);

            try
            {
                await _shoppingService.Create(dto);

                return(RedirectToAction("Home", "Index"));
            }
            catch (NecoException ex)
            {
                ViewBag.ValidationErrors = ex.Errors;
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
            }
            return(View());
        }
コード例 #3
0
        public async Task <IActionResult> Edit(int id, ShoppingDTO shoppingDTO)
        {
            if (id != shoppingDTO.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var shopping = _mapper.Map <Shopping>(shoppingDTO);

                    _shoppingAppService.Update(shopping);
                    _shoppingAppService.Commit();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ShoppingExists(shoppingDTO.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            return(View(shoppingDTO));
        }
コード例 #4
0
        public async Task Create(ShoppingDTO shopping)
        {
            // FALTA FAZER A VALIDAÇÃO



            await _shoppingRepository.Create(shopping);
        }
コード例 #5
0
        public async Task <IActionResult> Create(ShoppingDTO shoppingDTO)
        {
            if (ModelState.IsValid)
            {
                var shopping = _mapper.Map <Shopping>(shoppingDTO);

                _shoppingAppService.Insert(shopping);
                _shoppingAppService.Commit();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(shoppingDTO));
        }
コード例 #6
0
 public async Task Create(ShoppingDTO shopping)
 {
     this._contex.Shopping.Add(shopping);
     await this._contex.SaveChangesAsync();
 }