This commit is contained in:
manalejandro 2018-11-24 19:24:46 +01:00
parent 503b18ad53
commit 169dd24f0e
7 changed files with 144 additions and 51 deletions

View File

@ -32,15 +32,9 @@ public class MongodbcrudApplication implements CommandLineRunner {
public void run(String... args) throws Exception {
ArrayList<Item> items = new ArrayList<Item>();
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 {

View File

@ -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 "redirect:/index";
return "error";
}
} else {
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")

View File

@ -13,4 +13,6 @@ public interface ItemService {
public boolean save(Item item);
public boolean saveAll(List<Item> items);
public void delete(Item item);
}

View File

@ -33,4 +33,8 @@ public class ItemServiceImpl implements ItemService {
public boolean saveAll(List<Item> items) {
return this.itemRepository.saveAll(items) != null ? Boolean.TRUE : Boolean.FALSE;
}
public void delete(Item item) {
this.itemRepository.delete(item);
}
}

View File

@ -2,7 +2,25 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<div th:replace="fragments/head :: head" />
<body>
<body class="container">
<h2 class="h1 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}">
</div>
<div class="form-group">
<label for="apellido1">Primer Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido1}">
</div>
<div class="form-group">
<label for="apellido2">Segundo Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido2}">
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
<button th:onclick="'window.location=\'' + @{/} + '\';return false;'"
class="btn btn-default">Volver</button>
</form>
</body>
</html>

View File

@ -2,9 +2,9 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<div th:replace="fragments/head :: head" />
<body>
<div class="container">
<h2>Items</h2>
<body class="container">
<div>
<h2 class="h2">Items</h2>
<p>Items in MongoDB CRUD</p>
<table class="table table-hover table-striped">
<thead>
@ -20,10 +20,15 @@
<td>[[${item.nombre}]]</td>
<td>[[${item.apellido1}]]</td>
<td>[[${item.apellido2}]]</td>
<td><a th:href="@{/{id}/edit(id=${item.id})}">Editar</a></td>
<td><button class="btn btn-info"
th:onclick="'window.location=\'' + @{/{id}/edit(id=${item.id})} + '\';'">Editar</button>
<button class="btn btn-danger"
th:onclick="'window.confirm(\'Va a elimitar el Item\') ? window.location=\'' + @{/{id}/delete(id=${item.id})} + '\' : false;'">Borrar</button></td>
</tr>
</tbody>
</table>
</div>
</div>
<button th:onclick="'window.location=\'' + @{/new} + '\';'"
class="btn btn-primary">Nuevo</button>
</body>
</html>

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<div th:replace="fragments/head :: head" />
<body class="container">
<h2 class="h2 text-center">New Item</h2>
<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}">
</div>
<div class="form-group">
<label for="apellido1">Primer Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido1}">
</div>
<div class="form-group">
<label for="apellido2">Segundo Apellido:</label> <input type="text"
class="form-control" th:field="*{apellido2}">
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
<button th:onclick="'window.location=\'' + @{/} + '\';return false;'"
class="btn btn-default">Volver</button>
</form>
</body>
</html>