From 169dd24f0e4f42b408a14d6ef5883de15c6daee8 Mon Sep 17 00:00:00 2001 From: manalejandro Date: Sat, 24 Nov 2018 19:24:46 +0100 Subject: [PATCH] end CRUD --- .../mongodbcrud/MongodbcrudApplication.java | 12 +-- .../controllers/IndexController.java | 79 +++++++++++++++---- .../mongodbcrud/services/ItemService.java | 2 + .../mongodbcrud/services/ItemServiceImpl.java | 4 + src/main/resources/templates/edit.html | 22 +++++- src/main/resources/templates/index.html | 51 ++++++------ src/main/resources/templates/new.html | 25 ++++++ 7 files changed, 144 insertions(+), 51 deletions(-) create mode 100644 src/main/resources/templates/new.html diff --git a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java index e8bace3..4f13ec2 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java +++ b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java @@ -32,15 +32,9 @@ public class MongodbcrudApplication implements CommandLineRunner { public void run(String... args) throws Exception { ArrayList items = new ArrayList(); - items.add(new Item("prueba1", "apellido1", "apellido2")); - items.add(new Item("prueba2", "apellido1", "apellido2")); - items.add(new Item("prueba3", "apellido1", "apellido2")); - items.add(new Item("prueba4", "apellido1", "apellido2")); - items.add(new Item("prueba5", "apellido1", "apellido2")); - items.add(new Item("prueba6", "apellido1", "apellido2")); - items.add(new Item("prueba7", "apellido1", "apellido2")); - items.add(new Item("prueba8", "apellido1", "apellido2")); - items.add(new Item("prueba9", "apellido1", "apellido2")); + for (int i = 1; i <= 100; i++) { + items.add(new Item("prueba" + i, "apellido1" + i, "apellido2" + i)); + } if (this.itemService.saveAll(items)) { this.logger.log(Level.INFO, "Datos de prueba cargados correctamente..."); } else { diff --git a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java index 42efc65..1052fc0 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java +++ b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java @@ -25,32 +25,77 @@ public class IndexController implements ErrorController { } @GetMapping("/") - public String index(final @ModelAttribute IndexForm indexForm, final Model model) { + public String index(final @ModelAttribute("indexForm") IndexForm indexForm, final Model model) { IndexVO indexVO = new IndexVO(); indexVO.getListItem().addAll(itemService.getAll()); model.addAttribute("indexVO", indexVO); return "index"; } - @GetMapping("/{id}/edit") - public String editItem(final @PathVariable String id, final Model model) { - IndexVO indexVO = new IndexVO(); - Item item = itemService.getItem(id); - if (item != null) { - indexVO.setId(item.getId()); - indexVO.setNombre(item.getNombre()); - indexVO.setApellido1(item.getApellido1()); - indexVO.setApellido2(item.getApellido2()); - model.addAttribute("indexVO", indexVO); - return "edit"; + @GetMapping("/new") + public String newItem(final Model model) { + IndexForm indexForm = new IndexForm(); + model.addAttribute("indexForm", indexForm); + return "new"; + } + + @PostMapping("/new") + public String saveItem(final @ModelAttribute("indexForm") IndexForm indexForm) { + Item item = new Item(); + item.setNombre(indexForm.getNombre()); + item.setApellido1(indexForm.getApellido1()); + item.setApellido2(indexForm.getApellido2()); + if (!item.getNombre().isBlank() && !item.getApellido1().isBlank() && !item.getApellido2().isBlank()) { + if (this.itemService.save(item)) { + return "redirect:/"; + } else { + return "error"; + } } else { - return "redirect:/index"; + return "error"; } } - - @PostMapping("/{id}/save") - public String saveItem(final @ModelAttribute IndexForm indexForm, final Model model) { - return null; + + @GetMapping("/{id}/edit") + public String editItem(final @PathVariable String id, final Model model) { + IndexForm indexForm = new IndexForm(); + Item item = itemService.getItem(id); + if (item != null) { + indexForm.setId(item.getId()); + indexForm.setNombre(item.getNombre()); + indexForm.setApellido1(item.getApellido1()); + indexForm.setApellido2(item.getApellido2()); + model.addAttribute("indexForm", indexForm); + return "edit"; + } else { + return "redirect:/"; + } + } + + @PostMapping("/{id}/edit") + public String saveEditItem(final @ModelAttribute("indexForm") IndexForm indexForm) { + if (itemService.getItem(indexForm.getId()) != null) { + Item item = new Item(); + item.setId(indexForm.getId()); + item.setNombre(indexForm.getNombre()); + item.setApellido1(indexForm.getApellido1()); + item.setApellido2(indexForm.getApellido2()); + if (this.itemService.save(item)) { + return "redirect:/"; + } else { + return "error"; + } + } else { + return "error"; + } + } + + @GetMapping("/{id}/delete") + public String deleteItem(final @PathVariable String id) { + Item item = new Item(); + item.setId(id); + this.itemService.delete(item); + return "redirect:/"; } @GetMapping("/error") diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java index 267bf53..de21d15 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java @@ -13,4 +13,6 @@ public interface ItemService { public boolean save(Item item); public boolean saveAll(List items); + + public void delete(Item item); } diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java index 88231ef..dc829fa 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java @@ -33,4 +33,8 @@ public class ItemServiceImpl implements ItemService { public boolean saveAll(List items) { return this.itemRepository.saveAll(items) != null ? Boolean.TRUE : Boolean.FALSE; } + + public void delete(Item item) { + this.itemRepository.delete(item); + } } diff --git a/src/main/resources/templates/edit.html b/src/main/resources/templates/edit.html index 3c6c41a..6df2c0a 100644 --- a/src/main/resources/templates/edit.html +++ b/src/main/resources/templates/edit.html @@ -2,7 +2,25 @@
- - + +

Edit Item

+
+
+ +
+
+ +
+
+ +
+ + +
\ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index bdba96a..dece2af 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -2,28 +2,33 @@
- -
-

Items

-

Items in MongoDB CRUD

- - - - - - - - - - - - - - - - - -
NombrePrimer ApellidoSegundo ApellidoOpciones
[[${item.nombre}]][[${item.apellido1}]][[${item.apellido2}]]Editar
-
+ +
+

Items

+

Items in MongoDB CRUD

+ + + + + + + + + + + + + + + + + +
NombrePrimer ApellidoSegundo ApellidoOpciones
[[${item.nombre}]][[${item.apellido1}]][[${item.apellido2}]] +
+
+ \ No newline at end of file diff --git a/src/main/resources/templates/new.html b/src/main/resources/templates/new.html new file mode 100644 index 0000000..d87475e --- /dev/null +++ b/src/main/resources/templates/new.html @@ -0,0 +1,25 @@ + + +
+ +

New Item

+
+
+ +
+
+ +
+
+ +
+ + +
+ + \ No newline at end of file