Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 4, 2019 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/574b255d98fec2bf386718e905bd2797 to your computer and use it in GitHub Desktop.
Save parzibyte/574b255d98fec2bf386718e905bd2797 to your computer and use it in GitHub Desktop.
@PostMapping(value = "/agregar")
public String guardarProducto(@ModelAttribute @Valid Producto producto, BindingResult bindingResult, RedirectAttributes redirectAttrs) {
if (bindingResult.hasErrors()) {
return "productos/agregar_producto";
}
if (productosRepository.findFirstByCodigo(producto.getCodigo()) != null) {
redirectAttrs
.addFlashAttribute("mensaje", "Ya existe un producto con ese código")
.addFlashAttribute("clase", "warning");
return "redirect:/productos/agregar";
}
productosRepository.save(producto);
redirectAttrs
.addFlashAttribute("mensaje", "Agregado correctamente")
.addFlashAttribute("clase", "success");
return "redirect:/productos/agregar";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment