public IActionResult Edit(long Id, ProductInsert productInsert)
        {
            productInsert.Id = Id;

            if (ModelState.IsValid)
            {
                try
                {
                    _editProduct.Execute(productInsert);
                    TempData["Msg"] = "Succesfully edited product";
                    return(RedirectToAction("Index"));
                }
                catch (EntityNotFoundException e)
                {
                    ModelState.AddModelError(string.Empty, e.Message);
                    ViewBag.ProductInsertData = _getProductInsertData.Execute();
                    return(View(productInsert));
                }
                catch (EntityAlreadyExistException e)
                {
                    ModelState.AddModelError(string.Empty, e.Message);
                    ViewBag.ProductInsertData = _getProductInsertData.Execute();
                    return(View(productInsert));
                }
                catch (Exception e)
                {
                    TempData["Msg"] = e.Message;
                    return(RedirectToAction("Index"));
                }
            }
            return(View(productInsert));
        }
Exemple #2
0
 public ActionResult Edit(int id, ProductDto product)
 {
     try
     {
         _edit.Execute(product);
         return(RedirectToAction(nameof(Index)));
     }
     catch (Exception e)
     {
         return(View());
     }
 }
 public ActionResult Edit(InsertUpdateProductDto product)
 {
     try
     {
         _editProduct.Execute(product);
         return(RedirectToAction("Index"));
     }
     catch (EntityAlreadyExistsException e)
     {
         ViewBag.Manufacturers = new SelectList(_getManufacturers.Execute(), "ManufacturerId", "Name");
         ViewBag.Categories    = new SelectList(_getCategories.Execute(), "CategoryId", "Name");
         ViewBag.Suppliers     = _getSuppliers.Execute();
         TempData["error"]     = e.Message;
         return(View(product));
     }
     catch
     {
         ViewBag.Manufacturers = new SelectList(_getManufacturers.Execute(), "ManufacturerId", "Name");
         ViewBag.Categories    = new SelectList(_getCategories.Execute(), "CategoryId", "Name");
         ViewBag.Suppliers     = _getSuppliers.Execute();
         TempData["error"]     = "An error has occured";
         return(View(product));
     }
 }
Exemple #4
0
        public IActionResult Update(int id, [FromForm] ProductEdit p)
        {
            try
            {
                List <string> productPaths = new List <string>();
                List <string> productAlts  = new List <string>();

                if (p.Images != null && p.Images.Count != 0)
                {
                    foreach (var image in p.Images)
                    {
                        var extension = Path.GetExtension(image.FileName);

                        if (!ImageUploadHelper.AllowedExtensions.Contains(extension))
                        {
                            TempData["message"] = "One of the files is not an image";
                            return(Redirect($"~/Products/Edit/{id}"));
                        }

                        if (image.Length > ImageUploadHelper.MaxSize)
                        {
                            TempData["message"] = "One of the images is too large for uploading. Max = 8MB";
                            return(Redirect($"~/Products/Edit/{id}"));
                        }

                        var newFileName = DateTime.Now.ToBinary().ToString() + "_" + image.FileName;
                        var filePath    = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName);
                        image.CopyTo(new FileStream(filePath, FileMode.Create));

                        productPaths.Add(@"uploads\" + newFileName);
                        productAlts.Add(newFileName);
                    }
                }

                var dto = new CreateProductDto
                {
                    Name              = p.Name,
                    Description       = p.Description,
                    UnitPrice         = p.UnitPrice,
                    UnitWeight        = p.UnitWeight,
                    QuantityAvailable = p.QuantityAvailable,
                    CategoryId        = p.CategoryId,
                    SubCategoryId     = p.SubCategoryId,
                    ImagePaths        = productPaths,
                    ImageAlts         = productAlts,
                    Id = id
                };

                _editProduct.Execute(dto);
                return(Redirect("~/"));
            }
            catch (EntityNotFoundException e)
            {
                TempData["message"] = e.Message;
                return(Redirect($"~/Products/Edit/{id}"));
            }
            catch (EntityMissmatchException e)
            {
                TempData["message"] = e.Message;
                return(Redirect($"~/Products/Edit/{id}"));
            }
            catch (Exception e)
            {
                TempData["message"] = e.Message;
                return(Redirect($"~/Products/Edit/{id}"));
            }
        }