first sprint
This commit is contained in:
parent
75115585a6
commit
d0e072a50e
@ -1 +1,2 @@
|
|||||||
## MongoDB CRUD with SpringBoot
|
## MongoDB CRUD with SpringBoot
|
||||||
|
# mvn spring-boot:run -f pom.xml
|
||||||
|
@ -1,14 +1,50 @@
|
|||||||
package com.manalejandro.mongodbcrud;
|
package com.manalejandro.mongodbcrud;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||||
|
|
||||||
|
import com.manalejandro.mongodbcrud.model.Item;
|
||||||
|
import com.manalejandro.mongodbcrud.services.ItemService;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableMongoRepositories(basePackages = { "com.manalejandro.mongodbcrud.repositories" })
|
@EnableMongoRepositories(basePackages = { "com.manalejandro.mongodbcrud.repositories" })
|
||||||
public class MongodbcrudApplication {
|
public class MongodbcrudApplication implements CommandLineRunner {
|
||||||
|
|
||||||
|
private ItemService itemService;
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public MongodbcrudApplication(ItemService itemService) {
|
||||||
|
this.itemService = itemService;
|
||||||
|
this.logger = Logger.getLogger(this.getClass().getCanonicalName());
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(MongodbcrudApplication.class, args);
|
SpringApplication.run(MongodbcrudApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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"));
|
||||||
|
if (this.itemService.saveAll(items)) {
|
||||||
|
this.logger.log(Level.INFO, "Datos de prueba cargados correctamente...");
|
||||||
|
} else {
|
||||||
|
this.logger.log(Level.SEVERE, "Error: No se han podido cargar los datos");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,11 @@ 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;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
import com.manalejandro.mongodbcrud.forms.IndexForm;
|
import com.manalejandro.mongodbcrud.forms.IndexForm;
|
||||||
import com.manalejandro.mongodbcrud.model.Item;
|
import com.manalejandro.mongodbcrud.model.Item;
|
||||||
import com.manalejandro.mongodbcrud.services.ItemService;
|
import com.manalejandro.mongodbcrud.services.ItemService;
|
||||||
@ -21,24 +24,38 @@ public class IndexController implements ErrorController {
|
|||||||
this.itemService = itemService;
|
this.itemService = itemService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/")
|
@GetMapping("/")
|
||||||
public String index(final @ModelAttribute IndexForm indexForm, final Model model) {
|
public String index(final @ModelAttribute IndexForm indexForm, final Model model) {
|
||||||
IndexVO indexVO = new IndexVO();
|
IndexVO indexVO = new IndexVO();
|
||||||
if (indexForm.getId() == null) {
|
indexVO.getListItem().addAll(itemService.getAll());
|
||||||
indexVO.getListItem().addAll(itemService.getAll());
|
|
||||||
} else {
|
|
||||||
Item item = itemService.getItem(indexForm.getId());
|
|
||||||
indexForm.setId(item.getId());
|
|
||||||
indexForm.setNombre(item.getNombre());
|
|
||||||
indexForm.setApellido1(item.getApellido1());
|
|
||||||
indexForm.setApellido2(item.getApellido2());
|
|
||||||
}
|
|
||||||
model.addAttribute("indexVO", indexVO);
|
model.addAttribute("indexVO", indexVO);
|
||||||
return "index";
|
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";
|
||||||
|
} else {
|
||||||
|
return "redirect:/index";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{id}/save")
|
||||||
|
public String saveItem(final @ModelAttribute IndexForm indexForm, final Model model) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/error")
|
||||||
public String getErrorPath() {
|
public String getErrorPath() {
|
||||||
return "error";
|
return "error";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ public class Item {
|
|||||||
|
|
||||||
public Item() {
|
public Item() {
|
||||||
super();
|
super();
|
||||||
};
|
}
|
||||||
|
|
||||||
public Item(String nombre, String apellido1, String apellido2) {
|
public Item(String nombre, String apellido1, String apellido2) {
|
||||||
this.nombre = nombre;
|
this.nombre = nombre;
|
||||||
|
@ -10,4 +10,7 @@ public interface ItemService {
|
|||||||
|
|
||||||
public List<Item> getAll();
|
public List<Item> getAll();
|
||||||
|
|
||||||
|
public boolean save(Item item);
|
||||||
|
|
||||||
|
public boolean saveAll(List<Item> items);
|
||||||
}
|
}
|
||||||
|
@ -25,4 +25,12 @@ public class ItemServiceImpl implements ItemService {
|
|||||||
public Item getItem(String id) {
|
public Item getItem(String id) {
|
||||||
return this.itemRepository.findById(id).isPresent() ? this.itemRepository.findById(id).get() : null;
|
return this.itemRepository.findById(id).isPresent() ? this.itemRepository.findById(id).get() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean save(Item item) {
|
||||||
|
return this.itemRepository.save(item) != null ? Boolean.TRUE : Boolean.FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean saveAll(List<Item> items) {
|
||||||
|
return this.itemRepository.saveAll(items) != null ? Boolean.TRUE : Boolean.FALSE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ public class IndexVO {
|
|||||||
private String id;
|
private String id;
|
||||||
private String nombre;
|
private String nombre;
|
||||||
private String apellido1;
|
private String apellido1;
|
||||||
private String apellito2;
|
private String apellido2;
|
||||||
|
|
||||||
public List<Item> getListItem() {
|
public List<Item> getListItem() {
|
||||||
return listItem;
|
return listItem;
|
||||||
@ -45,12 +45,12 @@ public class IndexVO {
|
|||||||
this.apellido1 = apellido1;
|
this.apellido1 = apellido1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getApellito2() {
|
public String getApellido2() {
|
||||||
return apellito2;
|
return apellido2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApellito2(String apellito2) {
|
public void setApellido2(String apellido2) {
|
||||||
this.apellito2 = apellito2;
|
this.apellido2 = apellido2;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
server.servlet.context-path=/mongodbcrud
|
server.servlet.context-path=/mongodbcrud
|
||||||
server.port=3117
|
server.port=3117
|
||||||
|
spring.thymeleaf.cache=false
|
0
src/main/resources/static/css/main.css
Normal file
0
src/main/resources/static/css/main.css
Normal file
0
src/main/resources/static/js/main.js
Normal file
0
src/main/resources/static/js/main.js
Normal file
8
src/main/resources/templates/edit.html
Normal file
8
src/main/resources/templates/edit.html
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<div th:replace="fragments/head :: head" />
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,10 +1,13 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
<head>
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
<meta charset="UTF-8">
|
<div th:replace="fragments/head :: head" />
|
||||||
<title>Error</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
<body>
|
||||||
Error
|
<div class="text-center container">
|
||||||
|
<h1 class="h1 text-warning bg-danger">ERROR!!</h1>
|
||||||
|
<h5 class="h5 text-info">
|
||||||
|
<a th:href="@{/}">Go to Index</a>
|
||||||
|
</h5>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
9
src/main/resources/templates/fragments/head.html
Normal file
9
src/main/resources/templates/fragments/head.html
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<head th:fragment="head">
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Demo project for Spring Boot MongoDB CRUD</title>
|
||||||
|
<link rel="stylesheet" th:href="@{/webjars/bootstrap/4.1.3/css/bootstrap.min.css}">
|
||||||
|
<link rel="stylesheet" th:href="@{/css/main.css}">
|
||||||
|
<script th:src="@{/webjars/jquery/3.3.1-1/jquery.min.js}"></script>
|
||||||
|
<script th:src="@{/webjars/bootstrap/4.1.3/js/bootstrap.min.js}"></script>
|
||||||
|
<script th:src="@{/js/main.js}"></script>
|
||||||
|
</head>
|
@ -1,10 +1,29 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
<head>
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
<meta charset="UTF-8">
|
<div th:replace="fragments/head :: head" />
|
||||||
<title>Insert title here</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user