From 743f9c6fcc0ea08cdf09f2a344fd5008ba9e2bef Mon Sep 17 00:00:00 2001 From: manalejandro Date: Sat, 24 Nov 2018 20:47:47 +0100 Subject: [PATCH] Logger --- .../mongodbcrud/MongodbcrudApplication.java | 14 +++---- .../controllers/IndexController.java | 41 ++++++++++++++----- src/main/resources/templates/edit.html | 8 ++-- src/main/resources/templates/error.html | 2 +- src/main/resources/templates/index.html | 8 ++-- src/main/resources/templates/new.html | 6 +-- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java index 4f13ec2..2934553 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java +++ b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java @@ -1,9 +1,9 @@ package com.manalejandro.mongodbcrud; import java.util.ArrayList; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; @@ -23,7 +23,7 @@ public class MongodbcrudApplication implements CommandLineRunner { @Autowired public MongodbcrudApplication(ItemService itemService) { this.itemService = itemService; - this.logger = Logger.getLogger(this.getClass().getCanonicalName()); + this.logger = LoggerFactory.getLogger(this.getClass()); } public static void main(String[] args) { @@ -32,13 +32,13 @@ public class MongodbcrudApplication implements CommandLineRunner { public void run(String... args) throws Exception { ArrayList items = new ArrayList(); - for (int i = 1; i <= 100; i++) { - items.add(new Item("prueba" + i, "apellido1" + i, "apellido2" + i)); + for (int i = 1; i <= 10; i++) { + items.add(new Item("nombre_" + i, "apellido1_" + i, "apellido2_" + i)); } if (this.itemService.saveAll(items)) { - this.logger.log(Level.INFO, "Datos de prueba cargados correctamente..."); + this.logger.info("Datos de prueba cargados correctamente..."); } else { - this.logger.log(Level.SEVERE, "Error: No se han podido cargar los datos"); + this.logger.error("Error: No se han podido cargar los datos"); } } } diff --git a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java index 1052fc0..c76978e 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java +++ b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java @@ -1,5 +1,7 @@ package com.manalejandro.mongodbcrud.controllers; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.stereotype.Controller; @@ -18,17 +20,20 @@ import com.manalejandro.mongodbcrud.vo.IndexVO; public class IndexController implements ErrorController { private ItemService itemService; + private Logger logger; @Autowired public IndexController(ItemService itemService) { this.itemService = itemService; + this.logger = LoggerFactory.getLogger(this.getClass()); } - @GetMapping("/") + @GetMapping({"/", "/index"}) public String index(final @ModelAttribute("indexForm") IndexForm indexForm, final Model model) { IndexVO indexVO = new IndexVO(); indexVO.getListItem().addAll(itemService.getAll()); model.addAttribute("indexVO", indexVO); + this.logger.info("GET index"); return "index"; } @@ -36,6 +41,7 @@ public class IndexController implements ErrorController { public String newItem(final Model model) { IndexForm indexForm = new IndexForm(); model.addAttribute("indexForm", indexForm); + this.logger.info("GET new item"); return "new"; } @@ -45,14 +51,15 @@ public class IndexController implements ErrorController { item.setNombre(indexForm.getNombre()); item.setApellido1(indexForm.getApellido1()); item.setApellido2(indexForm.getApellido2()); + this.logger.info("POST new item: " + item.getNombre() + " " + item.getApellido1() + " " + item.getApellido2()); if (!item.getNombre().isBlank() && !item.getApellido1().isBlank() && !item.getApellido2().isBlank()) { if (this.itemService.save(item)) { return "redirect:/"; } else { - return "error"; + return "redirect:/error"; } } else { - return "error"; + return "redirect:/error"; } } @@ -60,6 +67,7 @@ public class IndexController implements ErrorController { public String editItem(final @PathVariable String id, final Model model) { IndexForm indexForm = new IndexForm(); Item item = itemService.getItem(id); + this.logger.info("GET edit item: " + id); if (item != null) { indexForm.setId(item.getId()); indexForm.setNombre(item.getNombre()); @@ -68,13 +76,15 @@ public class IndexController implements ErrorController { model.addAttribute("indexForm", indexForm); return "edit"; } else { - return "redirect:/"; + return "redirect:/error"; } } @PostMapping("/{id}/edit") public String saveEditItem(final @ModelAttribute("indexForm") IndexForm indexForm) { - if (itemService.getItem(indexForm.getId()) != null) { + this.logger.info("POST edit item: " + indexForm.getId()); + if (itemService.getItem(indexForm.getId()) != null && !indexForm.getNombre().isBlank() + && !indexForm.getApellido1().isBlank() && !indexForm.getApellido2().isBlank()) { Item item = new Item(); item.setId(indexForm.getId()); item.setNombre(indexForm.getNombre()); @@ -83,23 +93,32 @@ public class IndexController implements ErrorController { if (this.itemService.save(item)) { return "redirect:/"; } else { - return "error"; + return "redirect:/error"; } } else { - return "error"; + return "redirect:/error"; } } @GetMapping("/{id}/delete") public String deleteItem(final @PathVariable String id) { - Item item = new Item(); - item.setId(id); - this.itemService.delete(item); - return "redirect:/"; + this.logger.info("DELETE item: " + id); + if (!id.isBlank()) { + Item item = this.itemService.getItem(id); + if (item != null) { + this.itemService.delete(item); + return "redirect:/"; + } else { + return "redirect:/error"; + } + } else { + return "redirect:/error"; + } } @GetMapping("/error") public String getErrorPath() { + this.logger.error("ERROR"); return "error"; } diff --git a/src/main/resources/templates/edit.html b/src/main/resources/templates/edit.html index 6df2c0a..c69b40a 100644 --- a/src/main/resources/templates/edit.html +++ b/src/main/resources/templates/edit.html @@ -3,20 +3,20 @@ xmlns:th="http://www.thymeleaf.org">
-

Edit Item

+

Edit Item

+ class="form-control" th:field="*{nombre}" required>
+ class="form-control" th:field="*{apellido1}" required>
+ class="form-control" th:field="*{apellido2}" required>
+

+ +

\ No newline at end of file diff --git a/src/main/resources/templates/new.html b/src/main/resources/templates/new.html index d87475e..de88140 100644 --- a/src/main/resources/templates/new.html +++ b/src/main/resources/templates/new.html @@ -7,15 +7,15 @@
+ class="form-control" th:field="*{nombre}" required>
+ class="form-control" th:field="*{apellido1}" required>
+ class="form-control" th:field="*{apellido2}" required>