end CRUD
This commit is contained in:
parent
503b18ad53
commit
169dd24f0e
@ -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 {
|
||||
|
@ -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")
|
||||
|
@ -13,4 +13,6 @@ public interface ItemService {
|
||||
public boolean save(Item item);
|
||||
|
||||
public boolean saveAll(List<Item> items);
|
||||
|
||||
public void delete(Item item);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
@ -2,28 +2,33 @@
|
||||
<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>
|
||||
<p>Items in MongoDB CRUD</p>
|
||||
<table class="table table-hover table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Nombre</th>
|
||||
<th>Primer Apellido</th>
|
||||
<th>Segundo Apellido</th>
|
||||
<th>Opciones</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="item : ${indexVO.listItem}">
|
||||
<td>[[${item.nombre}]]</td>
|
||||
<td>[[${item.apellido1}]]</td>
|
||||
<td>[[${item.apellido2}]]</td>
|
||||
<td><a th:href="@{/{id}/edit(id=${item.id})}">Editar</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<body class="container">
|
||||
<div>
|
||||
<h2 class="h2">Items</h2>
|
||||
<p>Items in MongoDB CRUD</p>
|
||||
<table class="table table-hover table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Nombre</th>
|
||||
<th>Primer Apellido</th>
|
||||
<th>Segundo Apellido</th>
|
||||
<th>Opciones</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="item : ${indexVO.listItem}">
|
||||
<td>[[${item.nombre}]]</td>
|
||||
<td>[[${item.apellido1}]]</td>
|
||||
<td>[[${item.apellido2}]]</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>
|
||||
<button th:onclick="'window.location=\'' + @{/new} + '\';'"
|
||||
class="btn btn-primary">Nuevo</button>
|
||||
</body>
|
||||
</html>
|
25
src/main/resources/templates/new.html
Normal file
25
src/main/resources/templates/new.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user