public IActionResult Edit(int id)
        {
            var dicount = _discountService.GetById(id);

            if (dicount == null)
            {
                return(NotFound());
            }
            DiscountCreate model = new DiscountCreate
            {
                DiscountId     = dicount.DiscountId,
                ProductId      = dicount.ProductId,
                Name           = dicount.Name,
                Value          = dicount.Value,
                BeginDate      = dicount.BeginDate,
                FinishDate     = dicount.FinishDate,
                Type           = dicount.Type,
                TypeOfDiscount = new List <SelectListItem>
                {
                    new SelectListItem {
                        Value = "0", Text = "Wartościowa"
                    },
                    new SelectListItem {
                        Value = "1", Text = "Procentowa"
                    }
                }
            };

            ViewData["ProductId"] = new SelectList(_productService.GetAllProducts(), "ProductId", "Name");

            return(View(model));
        }
 public IActionResult Create(DiscountCreate discountAdd)
 {
     discountAdd.TypeOfDiscount = new List <SelectListItem>
     {
         new SelectListItem {
             Value = "0", Text = "Wartościowa"
         },
         new SelectListItem {
             Value = "1", Text = "Procentowa"
         },
     };
     ViewData["ProductId"] = new SelectList(_productService.GetAllProducts(), "ProductId", "Name");
     if (ModelState.IsValid)
     {
         Discount modelToAdd = new Discount
         {
             Name       = discountAdd.Name,
             Type       = discountAdd.Type,
             Value      = discountAdd.Value,
             BeginDate  = discountAdd.BeginDate,
             FinishDate = discountAdd.FinishDate,
             ProductId  = discountAdd.ProductId
         };
         Product product = _productService.GetById(discountAdd.ProductId);
         if (modelToAdd.Type == 0)//wartosciowa
         {
             if (product.Price - (decimal)modelToAdd.Value <= 0)
             {
                 TempData["Warning"] = "Nie można dodać promocji, cena produktu nie moze być zerowa!!!";
                 return(View(discountAdd));
             }
         }
         else if (modelToAdd.Type == 1)//procentowa
         {
             if (modelToAdd.Value <= 0 || modelToAdd.Value > 99)
             {
                 TempData["Warning"] = "Wartosć promocji powinna być pomiędzy 1 a 99";
                 return(View(discountAdd));
             }
             if (product.Price - (product.Price * ((decimal)modelToAdd.Value / 100)) <= 0)
             {
                 TempData["Warning"] = "Nie można dodać promocji, cena produktu nie moze być zerowa!!!";
                 return(View(discountAdd));
             }
         }
         _discountService.Create(modelToAdd);
         TempData["Info"] = "Pomocja została dodana";
         return(RedirectToAction(nameof(Index)));
     }
     return(View(discountAdd));
 }
        public IActionResult Create()
        {
            DiscountCreate model = new DiscountCreate
            {
                TypeOfDiscount = new List <SelectListItem>
                {
                    new SelectListItem {
                        Value = "0", Text = "Wartościowa"
                    },
                    new SelectListItem {
                        Value = "1", Text = "Procentowa"
                    },
                }
            };

            ViewData["ProductId"] = new SelectList(_productService.GetAllProducts(), "ProductId", "Name");

            model.BeginDate  = DateTime.UtcNow;
            model.FinishDate = DateTime.UtcNow;

            return(View(model));
        }