From d0e072a50eb47654d449574ca493ab66ee0f7c81 Mon Sep 17 00:00:00 2001 From: manalejandro Date: Sat, 24 Nov 2018 15:59:28 +0100 Subject: [PATCH] first sprint --- README.md | 1 + .../mongodbcrud/MongodbcrudApplication.java | 38 ++++++++++++++++- .../controllers/IndexController.java | 41 +++++++++++++------ .../manalejandro/mongodbcrud/model/Item.java | 2 +- .../mongodbcrud/services/ItemService.java | 3 ++ .../mongodbcrud/services/ItemServiceImpl.java | 8 ++++ .../manalejandro/mongodbcrud/vo/IndexVO.java | 10 ++--- src/main/resources/application.properties | 3 +- src/main/resources/static/css/main.css | 0 src/main/resources/static/js/main.js | 0 src/main/resources/templates/edit.html | 8 ++++ src/main/resources/templates/error.html | 15 ++++--- .../resources/templates/fragments/head.html | 9 ++++ src/main/resources/templates/index.html | 31 +++++++++++--- 14 files changed, 137 insertions(+), 32 deletions(-) create mode 100644 src/main/resources/static/css/main.css create mode 100644 src/main/resources/static/js/main.js create mode 100644 src/main/resources/templates/edit.html create mode 100644 src/main/resources/templates/fragments/head.html diff --git a/README.md b/README.md index 547589c..e9d599a 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ ## MongoDB CRUD with SpringBoot +# mvn spring-boot:run -f pom.xml diff --git a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java index 5c42615..e8bace3 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java +++ b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java @@ -1,14 +1,50 @@ 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.autoconfigure.SpringBootApplication; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; +import com.manalejandro.mongodbcrud.model.Item; +import com.manalejandro.mongodbcrud.services.ItemService; + @SpringBootApplication @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) { SpringApplication.run(MongodbcrudApplication.class, args); } + + public void run(String... args) throws Exception { + ArrayList items = new ArrayList(); + 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"); + } + } } diff --git a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java index 81a9c45..42efc65 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java +++ b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java @@ -4,8 +4,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.stereotype.Controller; 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.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.model.Item; import com.manalejandro.mongodbcrud.services.ItemService; @@ -21,24 +24,38 @@ public class IndexController implements ErrorController { this.itemService = itemService; } - @RequestMapping("/") + @GetMapping("/") public String index(final @ModelAttribute IndexForm indexForm, final Model model) { IndexVO indexVO = new IndexVO(); - if (indexForm.getId() == null) { - 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()); - } + 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"; + } else { + return "redirect:/index"; + } + } + + @PostMapping("/{id}/save") + public String saveItem(final @ModelAttribute IndexForm indexForm, final Model model) { + return null; + } + + @GetMapping("/error") public String getErrorPath() { return "error"; } - + } diff --git a/src/main/java/com/manalejandro/mongodbcrud/model/Item.java b/src/main/java/com/manalejandro/mongodbcrud/model/Item.java index ed28a2d..2acc032 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/model/Item.java +++ b/src/main/java/com/manalejandro/mongodbcrud/model/Item.java @@ -14,7 +14,7 @@ public class Item { public Item() { super(); - }; + } public Item(String nombre, String apellido1, String apellido2) { this.nombre = nombre; diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java index 3ca664a..267bf53 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java @@ -10,4 +10,7 @@ public interface ItemService { public List getAll(); + public boolean save(Item item); + + public boolean saveAll(List items); } diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java index eaf9f7e..88231ef 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java @@ -25,4 +25,12 @@ public class ItemServiceImpl implements ItemService { public Item getItem(String id) { 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 items) { + return this.itemRepository.saveAll(items) != null ? Boolean.TRUE : Boolean.FALSE; + } } diff --git a/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java b/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java index 55f9d3c..b378a31 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java +++ b/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java @@ -11,7 +11,7 @@ public class IndexVO { private String id; private String nombre; private String apellido1; - private String apellito2; + private String apellido2; public List getListItem() { return listItem; @@ -45,12 +45,12 @@ public class IndexVO { this.apellido1 = apellido1; } - public String getApellito2() { - return apellito2; + public String getApellido2() { + return apellido2; } - public void setApellito2(String apellito2) { - this.apellito2 = apellito2; + public void setApellido2(String apellido2) { + this.apellido2 = apellido2; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 027cc48..1e4eeff 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,3 @@ server.servlet.context-path=/mongodbcrud -server.port=3117 \ No newline at end of file +server.port=3117 +spring.thymeleaf.cache=false \ No newline at end of file diff --git a/src/main/resources/static/css/main.css b/src/main/resources/static/css/main.css new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/static/js/main.js b/src/main/resources/static/js/main.js new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/templates/edit.html b/src/main/resources/templates/edit.html new file mode 100644 index 0000000..3c6c41a --- /dev/null +++ b/src/main/resources/templates/edit.html @@ -0,0 +1,8 @@ + + +
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index 78a776a..1f80a29 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -1,10 +1,13 @@ - - - -Error - + +
-Error +
+

ERROR!!

+
+ Go to Index +
+
\ No newline at end of file diff --git a/src/main/resources/templates/fragments/head.html b/src/main/resources/templates/fragments/head.html new file mode 100644 index 0000000..ebaadd6 --- /dev/null +++ b/src/main/resources/templates/fragments/head.html @@ -0,0 +1,9 @@ + + + Demo project for Spring Boot MongoDB CRUD + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 2571001..bdba96a 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -1,10 +1,29 @@ - - - -Insert title here - + +
- +
+

Items

+

Items in MongoDB CRUD

+ + + + + + + + + + + + + + + + + +
NombrePrimer ApellidoSegundo ApellidoOpciones
[[${item.nombre}]][[${item.apellido1}]][[${item.apellido2}]]Editar
+
\ No newline at end of file