public ActionResult Edit(int id, GetManufacturerDto dto)
 {
     if (!ModelState.IsValid)
     {
         return(View(dto));
     }
     try
     {
         // TODO: Add update logic here
         _editManufacturer.Execute(dto);
         return(RedirectToAction(nameof(Index)));
     }
     catch (EntityNotFoundException)
     {
         return(RedirectToAction(nameof(Index)));
     }
     catch (EntityAlreadyExistsException)
     {
         TempData["error"] = "Manufacturer with that name already exists, try a different one.";
         return(View(dto));
     }
     catch
     {
         return(RedirectToAction(nameof(Index)));
     }
 }
Example #2
0
 public IActionResult Put(int id, [FromBody] GetManufacturerDto dto)
 {
     try
     {
         dto.Id = id;
         _editManufacturerCommand.Execute(dto);
         return(StatusCode(204));
     }
     catch
     {
         return(StatusCode(422));
     }
 }
        public void Execute(GetManufacturerDto request)
        {
            var manufacturer = Context.Manufacturers.Find(request.Id);

            if (manufacturer == null)
            {
                throw new EntityNotFoundException();
            }

            if (request.Name != manufacturer.Name && Context.Manufacturers.Any(m => m.Name == request.Name))
            {
                throw new EntityAlreadyExistsException();
            }

            manufacturer.Name = request.Name;
            Context.SaveChanges();
        }