commit 75115585a681a406c27497b67e25736ee511c930 Author: manalejandro Date: Sun Nov 18 21:10:51 2018 +0100 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..547589c --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +## MongoDB CRUD with SpringBoot diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..48afa21 --- /dev/null +++ b/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + com.manalejandro + mongodbcrud + 0.0.1-SNAPSHOT + war + + mongodbcrud + Demo project for Spring Boot MongoDB CRUD + + + org.springframework.boot + spring-boot-starter-parent + 2.1.0.RELEASE + + + + + UTF-8 + UTF-8 + 11 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb-reactive + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + org.springframework.boot + spring-boot-starter-test + test + + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java new file mode 100644 index 0000000..5c42615 --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java @@ -0,0 +1,14 @@ +package com.manalejandro.mongodbcrud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +@SpringBootApplication +@EnableMongoRepositories(basePackages = { "com.manalejandro.mongodbcrud.repositories" }) +public class MongodbcrudApplication { + + public static void main(String[] args) { + SpringApplication.run(MongodbcrudApplication.class, args); + } +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/ServletInitializer.java b/src/main/java/com/manalejandro/mongodbcrud/ServletInitializer.java new file mode 100644 index 0000000..a662692 --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/ServletInitializer.java @@ -0,0 +1,13 @@ +package com.manalejandro.mongodbcrud; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +public class ServletInitializer extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(MongodbcrudApplication.class); + } + +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java new file mode 100644 index 0000000..81a9c45 --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java @@ -0,0 +1,44 @@ +package com.manalejandro.mongodbcrud.controllers; + +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.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import com.manalejandro.mongodbcrud.forms.IndexForm; +import com.manalejandro.mongodbcrud.model.Item; +import com.manalejandro.mongodbcrud.services.ItemService; +import com.manalejandro.mongodbcrud.vo.IndexVO; + +@Controller +public class IndexController implements ErrorController { + + private ItemService itemService; + + @Autowired + public IndexController(ItemService itemService) { + this.itemService = itemService; + } + + @RequestMapping("/") + 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()); + } + model.addAttribute("indexVO", indexVO); + return "index"; + } + + public String getErrorPath() { + return "error"; + } + +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/forms/IndexForm.java b/src/main/java/com/manalejandro/mongodbcrud/forms/IndexForm.java new file mode 100644 index 0000000..143de11 --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/forms/IndexForm.java @@ -0,0 +1,41 @@ +package com.manalejandro.mongodbcrud.forms; + +public class IndexForm { + + private String id; + private String nombre; + private String apellido1; + private String apellido2; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public String getApellido1() { + return apellido1; + } + + public void setApellido1(String apellido1) { + this.apellido1 = apellido1; + } + + public String getApellido2() { + return apellido2; + } + + public void setApellido2(String apellido2) { + this.apellido2 = apellido2; + } +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/model/Item.java b/src/main/java/com/manalejandro/mongodbcrud/model/Item.java new file mode 100644 index 0000000..ed28a2d --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/model/Item.java @@ -0,0 +1,56 @@ +package com.manalejandro.mongodbcrud.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class Item { + + @Id + private String id; + private String nombre; + private String apellido1; + private String apellido2; + + public Item() { + super(); + }; + + public Item(String nombre, String apellido1, String apellido2) { + this.nombre = nombre; + this.apellido1 = apellido1; + this.apellido2 = apellido2; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public String getApellido1() { + return apellido1; + } + + public void setApellido1(String apellido1) { + this.apellido1 = apellido1; + } + + public String getApellido2() { + return apellido2; + } + + public void setApellido2(String apellido2) { + this.apellido2 = apellido2; + } +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/repositories/ItemRepository.java b/src/main/java/com/manalejandro/mongodbcrud/repositories/ItemRepository.java new file mode 100644 index 0000000..b7ae5f0 --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/repositories/ItemRepository.java @@ -0,0 +1,10 @@ +package com.manalejandro.mongodbcrud.repositories; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +import com.manalejandro.mongodbcrud.model.Item; + +@Repository +public interface ItemRepository extends MongoRepository { +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java new file mode 100644 index 0000000..3ca664a --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java @@ -0,0 +1,13 @@ +package com.manalejandro.mongodbcrud.services; + +import java.util.List; + +import com.manalejandro.mongodbcrud.model.Item; + +public interface ItemService { + + public Item getItem(String id); + + public List getAll(); + +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java new file mode 100644 index 0000000..eaf9f7e --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java @@ -0,0 +1,28 @@ +package com.manalejandro.mongodbcrud.services; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.manalejandro.mongodbcrud.model.Item; +import com.manalejandro.mongodbcrud.repositories.ItemRepository; + +@Service +public class ItemServiceImpl implements ItemService { + + private ItemRepository itemRepository; + + @Autowired + public ItemServiceImpl(ItemRepository itemRepository) { + this.itemRepository = itemRepository; + } + + public List getAll() { + return this.itemRepository.findAll(); + } + + public Item getItem(String id) { + return this.itemRepository.findById(id).isPresent() ? this.itemRepository.findById(id).get() : null; + } +} diff --git a/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java b/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java new file mode 100644 index 0000000..55f9d3c --- /dev/null +++ b/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java @@ -0,0 +1,56 @@ +package com.manalejandro.mongodbcrud.vo; + +import java.util.ArrayList; +import java.util.List; + +import com.manalejandro.mongodbcrud.model.Item; + +public class IndexVO { + + private List listItem = new ArrayList(); + private String id; + private String nombre; + private String apellido1; + private String apellito2; + + public List getListItem() { + return listItem; + } + + public void setListItem(List listItem) { + this.listItem = listItem; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public String getApellido1() { + return apellido1; + } + + public void setApellido1(String apellido1) { + this.apellido1 = apellido1; + } + + public String getApellito2() { + return apellito2; + } + + public void setApellito2(String apellito2) { + this.apellito2 = apellito2; + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..027cc48 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.servlet.context-path=/mongodbcrud +server.port=3117 \ No newline at end of file diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..78a776a --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,10 @@ + + + + +Error + + +Error + + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..2571001 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,10 @@ + + + + +Insert title here + + + + + \ No newline at end of file diff --git a/src/test/java/com/manalejandro/mongodbcrud/MongodbcrudApplicationTests.java b/src/test/java/com/manalejandro/mongodbcrud/MongodbcrudApplicationTests.java new file mode 100644 index 0000000..91c2d2a --- /dev/null +++ b/src/test/java/com/manalejandro/mongodbcrud/MongodbcrudApplicationTests.java @@ -0,0 +1,16 @@ +package com.manalejandro.mongodbcrud; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class MongodbcrudApplicationTests { + + @Test + public void contextLoads() { + } + +}