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 {
|
public void run(String... args) throws Exception {
|
||||||
ArrayList<Item> items = new ArrayList<Item>();
|
ArrayList<Item> items = new ArrayList<Item>();
|
||||||
items.add(new Item("prueba1", "apellido1", "apellido2"));
|
for (int i = 1; i <= 100; i++) {
|
||||||
items.add(new Item("prueba2", "apellido1", "apellido2"));
|
items.add(new Item("prueba" + i, "apellido1" + i, "apellido2" + i));
|
||||||
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"));
|
|
||||||
if (this.itemService.saveAll(items)) {
|
if (this.itemService.saveAll(items)) {
|
||||||
this.logger.log(Level.INFO, "Datos de prueba cargados correctamente...");
|
this.logger.log(Level.INFO, "Datos de prueba cargados correctamente...");
|
||||||
} else {
|
} else {
|
||||||
|
@ -25,32 +25,77 @@ public class IndexController implements ErrorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/")
|
@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 indexVO = new IndexVO();
|
||||||
indexVO.getListItem().addAll(itemService.getAll());
|
indexVO.getListItem().addAll(itemService.getAll());
|
||||||
model.addAttribute("indexVO", indexVO);
|
model.addAttribute("indexVO", indexVO);
|
||||||
return "index";
|
return "index";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}/edit")
|
@GetMapping("/new")
|
||||||
public String editItem(final @PathVariable String id, final Model model) {
|
public String newItem(final Model model) {
|
||||||
IndexVO indexVO = new IndexVO();
|
IndexForm indexForm = new IndexForm();
|
||||||
Item item = itemService.getItem(id);
|
model.addAttribute("indexForm", indexForm);
|
||||||
if (item != null) {
|
return "new";
|
||||||
indexVO.setId(item.getId());
|
}
|
||||||
indexVO.setNombre(item.getNombre());
|
|
||||||
indexVO.setApellido1(item.getApellido1());
|
@PostMapping("/new")
|
||||||
indexVO.setApellido2(item.getApellido2());
|
public String saveItem(final @ModelAttribute("indexForm") IndexForm indexForm) {
|
||||||
model.addAttribute("indexVO", indexVO);
|
Item item = new Item();
|
||||||
return "edit";
|
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 {
|
} else {
|
||||||
return "redirect:/index";
|
return "error";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "error";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/{id}/save")
|
@GetMapping("/{id}/edit")
|
||||||
public String saveItem(final @ModelAttribute IndexForm indexForm, final Model model) {
|
public String editItem(final @PathVariable String id, final Model model) {
|
||||||
return null;
|
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")
|
@GetMapping("/error")
|
||||||
|
@ -13,4 +13,6 @@ public interface ItemService {
|
|||||||
public boolean save(Item item);
|
public boolean save(Item item);
|
||||||
|
|
||||||
public boolean saveAll(List<Item> items);
|
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) {
|
public boolean saveAll(List<Item> items) {
|
||||||
return this.itemRepository.saveAll(items) != null ? Boolean.TRUE : Boolean.FALSE;
|
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"
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
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>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
@ -2,9 +2,9 @@
|
|||||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
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>
|
<body class="container">
|
||||||
<div class="container">
|
<div>
|
||||||
<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">
|
||||||
<thead>
|
<thead>
|
||||||
@ -20,10 +20,15 @@
|
|||||||
<td>[[${item.nombre}]]</td>
|
<td>[[${item.nombre}]]</td>
|
||||||
<td>[[${item.apellido1}]]</td>
|
<td>[[${item.apellido1}]]</td>
|
||||||
<td>[[${item.apellido2}]]</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>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
<button th:onclick="'window.location=\'' + @{/new} + '\';'"
|
||||||
|
class="btn btn-primary">Nuevo</button>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</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