Logger
This commit is contained in:
parent
169dd24f0e
commit
743f9c6fcc
@ -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<Item> items = new ArrayList<Item>();
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.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";
|
||||
}
|
||||
|
||||
|
@ -3,20 +3,20 @@
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<div th:replace="fragments/head :: head" />
|
||||
<body class="container">
|
||||
<h2 class="h1 text-center">Edit Item</h2>
|
||||
<h2 class="h2 text-center">Edit Item</h2>
|
||||
<form th:action="@{/{id}/edit(id=${indexForm.id})}"
|
||||
th:object="${indexForm}" method="post">
|
||||
<div class="form-group">
|
||||
<label for="nombre">Nombre:</label> <input type="text"
|
||||
class="form-control" th:field="*{nombre}">
|
||||
class="form-control" th:field="*{nombre}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="apellido1">Primer Apellido:</label> <input type="text"
|
||||
class="form-control" th:field="*{apellido1}">
|
||||
class="form-control" th:field="*{apellido1}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="apellido2">Segundo Apellido:</label> <input type="text"
|
||||
class="form-control" th:field="*{apellido2}">
|
||||
class="form-control" th:field="*{apellido2}" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Enviar</button>
|
||||
<button th:onclick="'window.location=\'' + @{/} + '\';return false;'"
|
||||
|
@ -4,7 +4,7 @@
|
||||
<div th:replace="fragments/head :: head" />
|
||||
<body>
|
||||
<div class="text-center container">
|
||||
<h1 class="h1 text-warning bg-danger">ERROR!!</h1>
|
||||
<h1 class="h1 text-danger">ERROR!!</h1>
|
||||
<h5 class="h5 text-info">
|
||||
<a th:href="@{/}">Go to Index</a>
|
||||
</h5>
|
||||
|
@ -6,7 +6,7 @@
|
||||
<div>
|
||||
<h2 class="h2">Items</h2>
|
||||
<p>Items in MongoDB CRUD</p>
|
||||
<table class="table table-hover table-striped">
|
||||
<table class="table table-hover table-striped text-center">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Nombre</th>
|
||||
@ -28,7 +28,9 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p>
|
||||
<button th:onclick="'window.location=\'' + @{/new} + '\';'"
|
||||
class="btn btn-primary">Nuevo</button>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
@ -7,15 +7,15 @@
|
||||
<form th:action="@{/new}" th:object="${indexForm}" method="post">
|
||||
<div class="form-group">
|
||||
<label for="nombre">Nombre:</label> <input type="text"
|
||||
class="form-control" th:field="*{nombre}">
|
||||
class="form-control" th:field="*{nombre}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="apellido1">Primer Apellido:</label> <input type="text"
|
||||
class="form-control" th:field="*{apellido1}">
|
||||
class="form-control" th:field="*{apellido1}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="apellido2">Segundo Apellido:</label> <input type="text"
|
||||
class="form-control" th:field="*{apellido2}">
|
||||
class="form-control" th:field="*{apellido2}" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Enviar</button>
|
||||
<button th:onclick="'window.location=\'' + @{/} + '\';return false;'"
|
||||
|
Loading…
Reference in New Issue
Block a user