This commit is contained in:
manalejandro 2018-11-24 20:47:47 +01:00
parent 169dd24f0e
commit 743f9c6fcc
6 changed files with 50 additions and 29 deletions

View File

@ -1,9 +1,9 @@
package com.manalejandro.mongodbcrud; package com.manalejandro.mongodbcrud;
import java.util.ArrayList; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
@ -23,7 +23,7 @@ public class MongodbcrudApplication implements CommandLineRunner {
@Autowired @Autowired
public MongodbcrudApplication(ItemService itemService) { public MongodbcrudApplication(ItemService itemService) {
this.itemService = itemService; this.itemService = itemService;
this.logger = Logger.getLogger(this.getClass().getCanonicalName()); this.logger = LoggerFactory.getLogger(this.getClass());
} }
public static void main(String[] args) { public static void main(String[] args) {
@ -32,13 +32,13 @@ public class MongodbcrudApplication implements CommandLineRunner {
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
ArrayList<Item> items = new ArrayList<Item>(); ArrayList<Item> items = new ArrayList<Item>();
for (int i = 1; i <= 100; i++) { for (int i = 1; i <= 10; i++) {
items.add(new Item("prueba" + i, "apellido1" + i, "apellido2" + i)); items.add(new Item("nombre_" + i, "apellido1_" + i, "apellido2_" + i));
} }
if (this.itemService.saveAll(items)) { if (this.itemService.saveAll(items)) {
this.logger.log(Level.INFO, "Datos de prueba cargados correctamente..."); this.logger.info("Datos de prueba cargados correctamente...");
} else { } 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");
} }
} }
} }

View File

@ -1,5 +1,7 @@
package com.manalejandro.mongodbcrud.controllers; package com.manalejandro.mongodbcrud.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -18,17 +20,20 @@ import com.manalejandro.mongodbcrud.vo.IndexVO;
public class IndexController implements ErrorController { public class IndexController implements ErrorController {
private ItemService itemService; private ItemService itemService;
private Logger logger;
@Autowired @Autowired
public IndexController(ItemService itemService) { public IndexController(ItemService itemService) {
this.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) { public String index(final @ModelAttribute("indexForm") IndexForm indexForm, final Model model) {
IndexVO indexVO = new IndexVO(); IndexVO indexVO = new IndexVO();
indexVO.getListItem().addAll(itemService.getAll()); indexVO.getListItem().addAll(itemService.getAll());
model.addAttribute("indexVO", indexVO); model.addAttribute("indexVO", indexVO);
this.logger.info("GET index");
return "index"; return "index";
} }
@ -36,6 +41,7 @@ public class IndexController implements ErrorController {
public String newItem(final Model model) { public String newItem(final Model model) {
IndexForm indexForm = new IndexForm(); IndexForm indexForm = new IndexForm();
model.addAttribute("indexForm", indexForm); model.addAttribute("indexForm", indexForm);
this.logger.info("GET new item");
return "new"; return "new";
} }
@ -45,14 +51,15 @@ public class IndexController implements ErrorController {
item.setNombre(indexForm.getNombre()); item.setNombre(indexForm.getNombre());
item.setApellido1(indexForm.getApellido1()); item.setApellido1(indexForm.getApellido1());
item.setApellido2(indexForm.getApellido2()); 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 (!item.getNombre().isBlank() && !item.getApellido1().isBlank() && !item.getApellido2().isBlank()) {
if (this.itemService.save(item)) { if (this.itemService.save(item)) {
return "redirect:/"; return "redirect:/";
} else { } else {
return "error"; return "redirect:/error";
} }
} else { } 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) { public String editItem(final @PathVariable String id, final Model model) {
IndexForm indexForm = new IndexForm(); IndexForm indexForm = new IndexForm();
Item item = itemService.getItem(id); Item item = itemService.getItem(id);
this.logger.info("GET edit item: " + id);
if (item != null) { if (item != null) {
indexForm.setId(item.getId()); indexForm.setId(item.getId());
indexForm.setNombre(item.getNombre()); indexForm.setNombre(item.getNombre());
@ -68,13 +76,15 @@ public class IndexController implements ErrorController {
model.addAttribute("indexForm", indexForm); model.addAttribute("indexForm", indexForm);
return "edit"; return "edit";
} else { } else {
return "redirect:/"; return "redirect:/error";
} }
} }
@PostMapping("/{id}/edit") @PostMapping("/{id}/edit")
public String saveEditItem(final @ModelAttribute("indexForm") IndexForm indexForm) { 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 item = new Item();
item.setId(indexForm.getId()); item.setId(indexForm.getId());
item.setNombre(indexForm.getNombre()); item.setNombre(indexForm.getNombre());
@ -83,23 +93,32 @@ public class IndexController implements ErrorController {
if (this.itemService.save(item)) { if (this.itemService.save(item)) {
return "redirect:/"; return "redirect:/";
} else { } else {
return "error"; return "redirect:/error";
} }
} else { } else {
return "error"; return "redirect:/error";
} }
} }
@GetMapping("/{id}/delete") @GetMapping("/{id}/delete")
public String deleteItem(final @PathVariable String id) { public String deleteItem(final @PathVariable String id) {
Item item = new Item(); this.logger.info("DELETE item: " + id);
item.setId(id); if (!id.isBlank()) {
Item item = this.itemService.getItem(id);
if (item != null) {
this.itemService.delete(item); this.itemService.delete(item);
return "redirect:/"; return "redirect:/";
} else {
return "redirect:/error";
}
} else {
return "redirect:/error";
}
} }
@GetMapping("/error") @GetMapping("/error")
public String getErrorPath() { public String getErrorPath() {
this.logger.error("ERROR");
return "error"; return "error";
} }

View File

@ -3,20 +3,20 @@
xmlns:th="http://www.thymeleaf.org"> xmlns:th="http://www.thymeleaf.org">
<div th:replace="fragments/head :: head" /> <div th:replace="fragments/head :: head" />
<body class="container"> <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})}" <form th:action="@{/{id}/edit(id=${indexForm.id})}"
th:object="${indexForm}" method="post"> th:object="${indexForm}" method="post">
<div class="form-group"> <div class="form-group">
<label for="nombre">Nombre:</label> <input type="text" <label for="nombre">Nombre:</label> <input type="text"
class="form-control" th:field="*{nombre}"> class="form-control" th:field="*{nombre}" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="apellido1">Primer Apellido:</label> <input type="text" <label for="apellido1">Primer Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido1}"> class="form-control" th:field="*{apellido1}" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="apellido2">Segundo Apellido:</label> <input type="text" <label for="apellido2">Segundo Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido2}"> class="form-control" th:field="*{apellido2}" required>
</div> </div>
<button type="submit" class="btn btn-primary">Enviar</button> <button type="submit" class="btn btn-primary">Enviar</button>
<button th:onclick="'window.location=\'' + @{/} + '\';return false;'" <button th:onclick="'window.location=\'' + @{/} + '\';return false;'"

View File

@ -4,7 +4,7 @@
<div th:replace="fragments/head :: head" /> <div th:replace="fragments/head :: head" />
<body> <body>
<div class="text-center container"> <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"> <h5 class="h5 text-info">
<a th:href="@{/}">Go to Index</a> <a th:href="@{/}">Go to Index</a>
</h5> </h5>

View File

@ -6,7 +6,7 @@
<div> <div>
<h2 class="h2">Items</h2> <h2 class="h2">Items</h2>
<p>Items in MongoDB CRUD</p> <p>Items in MongoDB CRUD</p>
<table class="table table-hover table-striped"> <table class="table table-hover table-striped text-center">
<thead> <thead>
<tr> <tr>
<th>Nombre</th> <th>Nombre</th>
@ -28,7 +28,9 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<p>
<button th:onclick="'window.location=\'' + @{/new} + '\';'" <button th:onclick="'window.location=\'' + @{/new} + '\';'"
class="btn btn-primary">Nuevo</button> class="btn btn-primary">Nuevo</button>
</p>
</body> </body>
</html> </html>

View File

@ -7,15 +7,15 @@
<form th:action="@{/new}" th:object="${indexForm}" method="post"> <form th:action="@{/new}" th:object="${indexForm}" method="post">
<div class="form-group"> <div class="form-group">
<label for="nombre">Nombre:</label> <input type="text" <label for="nombre">Nombre:</label> <input type="text"
class="form-control" th:field="*{nombre}"> class="form-control" th:field="*{nombre}" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="apellido1">Primer Apellido:</label> <input type="text" <label for="apellido1">Primer Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido1}"> class="form-control" th:field="*{apellido1}" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="apellido2">Segundo Apellido:</label> <input type="text" <label for="apellido2">Segundo Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido2}"> class="form-control" th:field="*{apellido2}" required>
</div> </div>
<button type="submit" class="btn btn-primary">Enviar</button> <button type="submit" class="btn btn-primary">Enviar</button>
<button th:onclick="'window.location=\'' + @{/} + '\';return false;'" <button th:onclick="'window.location=\'' + @{/} + '\';return false;'"