diff --git a/pom.xml b/pom.xml index a3cf6a4..f6a4096 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - 4.0.0 @@ -15,7 +16,7 @@ org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE - + @@ -38,20 +39,28 @@ spring-boot-starter-thymeleaf - org.webjars - jquery - 3.3.1-1 + org.webjars + jquery + 3.3.1-1 - org.webjars - bootstrap - 4.1.3 + org.webjars + bootstrap + 4.1.3 org.springframework.boot spring-boot-starter-tomcat provided + + org.springframework + spring-webflux + + + org.projectlombok + lombok + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java index 50b84fc..3035e68 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java +++ b/src/main/java/com/manalejandro/mongodbcrud/MongodbcrudApplication.java @@ -1,6 +1,9 @@ package com.manalejandro.mongodbcrud; +import java.time.Duration; import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import org.slf4j.Logger; @@ -12,17 +15,16 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; +import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; - import com.manalejandro.mongodbcrud.model.Item; import com.manalejandro.mongodbcrud.services.ItemService; @SpringBootApplication @EnableAsync -@EnableMongoRepositories(basePackages = { "com.manalejandro.mongodbcrud.repositories" }) +@EnableReactiveMongoRepositories(basePackages = { "com.manalejandro.mongodbcrud.repositories" }) public class MongodbcrudApplication implements CommandLineRunner, AsyncConfigurer { private ItemService itemService; @@ -38,12 +40,12 @@ public class MongodbcrudApplication implements CommandLineRunner, AsyncConfigure SpringApplication.run(MongodbcrudApplication.class, args); } - public void run(String... args) throws Exception { - ArrayList items = new ArrayList(); + public void run(String... args) throws InterruptedException, ExecutionException { + final List items = new ArrayList(); for (int i = 1; i <= 10; i++) { items.add(new Item("nombre_" + i, "apellido1_" + i, "apellido2_" + i)); } - if (this.itemService.saveAll(items).get()) { + if (itemService.saveAll(items).block(Duration.ofSeconds(5))) { this.logger.info("Datos de prueba cargados correctamente..."); } else { this.logger.error("Error: No se han podido cargar los datos"); @@ -61,8 +63,9 @@ public class MongodbcrudApplication implements CommandLineRunner, AsyncConfigure return executor; } + @Bean public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new SimpleAsyncUncaughtExceptionHandler(); } - -} + +} \ No newline at end of file diff --git a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java index 8bab4bf..0a9ce9e 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java +++ b/src/main/java/com/manalejandro/mongodbcrud/controllers/IndexController.java @@ -34,7 +34,7 @@ public class IndexController implements ErrorController { public String index(final @ModelAttribute("indexForm") IndexForm indexForm, final Model model) throws InterruptedException, ExecutionException { IndexVO indexVO = new IndexVO(); - indexVO.getListItem().addAll(itemService.getAll().get()); + indexVO.getListItem().addAll(itemService.getAll()); model.addAttribute("indexVO", indexVO); this.logger.info("GET index"); return "index"; @@ -49,15 +49,14 @@ public class IndexController implements ErrorController { } @PostMapping("/new") - public String saveItem(final @ModelAttribute("indexForm") IndexForm indexForm) - throws InterruptedException, ExecutionException { + public String saveItem(final @ModelAttribute("indexForm") IndexForm indexForm) { Item item = new Item(); item.setNombre(indexForm.getNombre()); item.setApellido1(indexForm.getApellido1()); item.setApellido2(indexForm.getApellido2()); this.logger.info("POST new item: " + item.getNombre() + " " + item.getApellido1() + " " + item.getApellido2()); if (!item.getNombre().isBlank() && !item.getApellido1().isBlank() && !item.getApellido2().isBlank()) { - if (this.itemService.save(item).get()) { + if (this.itemService.save(item).block()) { return "redirect:/"; } else { return "redirect:/error"; @@ -68,10 +67,9 @@ public class IndexController implements ErrorController { } @GetMapping("/{id}/edit") - public String editItem(final @PathVariable String id, final Model model) - throws InterruptedException, ExecutionException { + public String editItem(final @PathVariable String id, final Model model) { IndexForm indexForm = new IndexForm(); - Item item = itemService.getItem(id).get(); + Item item = itemService.getItem(id).block(); this.logger.info("GET edit item: " + id); if (item != null) { indexForm.setId(item.getId()); @@ -86,8 +84,7 @@ public class IndexController implements ErrorController { } @PostMapping("/{id}/edit") - public String saveEditItem(final @ModelAttribute("indexForm") IndexForm indexForm) - throws InterruptedException, ExecutionException { + public String saveEditItem(final @ModelAttribute("indexForm") IndexForm indexForm) { this.logger.info("POST edit item: " + indexForm.getId()); if (itemService.getItem(indexForm.getId()) != null && !indexForm.getNombre().isBlank() && !indexForm.getApellido1().isBlank() && !indexForm.getApellido2().isBlank()) { @@ -96,7 +93,7 @@ public class IndexController implements ErrorController { item.setNombre(indexForm.getNombre()); item.setApellido1(indexForm.getApellido1()); item.setApellido2(indexForm.getApellido2()); - if (this.itemService.save(item).get()) { + if (this.itemService.save(item).block()) { return "redirect:/"; } else { return "redirect:/error"; @@ -107,10 +104,10 @@ public class IndexController implements ErrorController { } @GetMapping("/{id}/delete") - public String deleteItem(final @PathVariable String id) throws InterruptedException, ExecutionException { + public String deleteItem(final @PathVariable String id) { this.logger.info("DELETE item: " + id); if (!id.isBlank()) { - Item item = this.itemService.getItem(id).get(); + Item item = this.itemService.getItem(id).block(); if (item != null) { this.itemService.delete(item); return "redirect:/"; diff --git a/src/main/java/com/manalejandro/mongodbcrud/forms/IndexForm.java b/src/main/java/com/manalejandro/mongodbcrud/forms/IndexForm.java index 143de11..e877de8 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/forms/IndexForm.java +++ b/src/main/java/com/manalejandro/mongodbcrud/forms/IndexForm.java @@ -1,5 +1,16 @@ package com.manalejandro.mongodbcrud.forms; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@ToString +@Builder +@NoArgsConstructor +@AllArgsConstructor public class IndexForm { private String id; @@ -7,35 +18,4 @@ public class IndexForm { 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 index 2acc032..0861307 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/model/Item.java +++ b/src/main/java/com/manalejandro/mongodbcrud/model/Item.java @@ -3,54 +3,30 @@ package com.manalejandro.mongodbcrud.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.ToString; + +@Data +@ToString +@Builder +@NoArgsConstructor +@AllArgsConstructor +@RequiredArgsConstructor @Document public class Item { @Id private String id; + @NonNull private String nombre; + @NonNull private String apellido1; + @NonNull 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 index b7ae5f0..dcba3fe 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/repositories/ItemRepository.java +++ b/src/main/java/com/manalejandro/mongodbcrud/repositories/ItemRepository.java @@ -1,10 +1,10 @@ package com.manalejandro.mongodbcrud.repositories; -import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.stereotype.Repository; import com.manalejandro.mongodbcrud.model.Item; @Repository -public interface ItemRepository extends MongoRepository { +public interface ItemRepository extends ReactiveMongoRepository { } diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java index 590fcc9..7d1d880 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemService.java @@ -1,19 +1,21 @@ package com.manalejandro.mongodbcrud.services; import java.util.List; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import com.manalejandro.mongodbcrud.model.Item; +import reactor.core.publisher.Mono; + public interface ItemService { - public CompletableFuture getItem(String id); + public Mono getItem(String id); - public CompletableFuture> getAll(); + public List getAll() throws InterruptedException, ExecutionException; - public CompletableFuture save(Item item); + public Mono save(Item item); + + public Mono saveAll(List items); - public CompletableFuture saveAll(List items); - public void delete(Item item); } diff --git a/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java index 1b5e3d1..fdce015 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java +++ b/src/main/java/com/manalejandro/mongodbcrud/services/ItemServiceImpl.java @@ -1,7 +1,9 @@ package com.manalejandro.mongodbcrud.services; +import java.time.Duration; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; @@ -10,6 +12,8 @@ import org.springframework.stereotype.Service; import com.manalejandro.mongodbcrud.model.Item; import com.manalejandro.mongodbcrud.repositories.ItemRepository; +import reactor.core.publisher.Mono; + @Service public class ItemServiceImpl implements ItemService { @@ -21,29 +25,34 @@ public class ItemServiceImpl implements ItemService { } @Async - public CompletableFuture> getAll() { - return CompletableFuture.completedFuture(this.itemRepository.findAll()); + public List getAll() throws InterruptedException, ExecutionException { + return CompletableFuture + .completedFuture(this.itemRepository.findAll().collectList().block(Duration.ofSeconds(5))).get(); } @Async - public CompletableFuture getItem(String id) { - return this.itemRepository.findById(id).isPresent() - ? CompletableFuture.completedFuture(this.itemRepository.findById(id).get()) + public Mono getItem(String id) { + return this.itemRepository.findById(id).block(Duration.ofSeconds(5)) != null ? Mono.fromFuture( + CompletableFuture.completedFuture(this.itemRepository.findById(id).block(Duration.ofSeconds(5)))) : null; } @Async - public CompletableFuture save(Item item) { - return this.itemRepository.save(item) != null ? CompletableFuture.completedFuture(Boolean.TRUE) : CompletableFuture.completedFuture(Boolean.FALSE); + public Mono save(Item item) { + return this.itemRepository.save(item).block(Duration.ofSeconds(5)) != null + ? Mono.fromFuture(CompletableFuture.completedFuture(Boolean.TRUE)) + : Mono.fromFuture(CompletableFuture.completedFuture(Boolean.FALSE)); } @Async - public CompletableFuture saveAll(List items) { - return this.itemRepository.saveAll(items) != null ? CompletableFuture.completedFuture(Boolean.TRUE) : CompletableFuture.completedFuture(Boolean.FALSE); + public Mono saveAll(List items) { + return this.itemRepository.saveAll(items).collectList().block(Duration.ofSeconds(5)) != null + ? Mono.fromFuture(CompletableFuture.completedFuture(Boolean.TRUE)) + : Mono.fromFuture(CompletableFuture.completedFuture(Boolean.FALSE)); } @Async public void delete(Item item) { - this.itemRepository.delete(item); + this.itemRepository.delete(item).block(Duration.ofSeconds(5)); } } diff --git a/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java b/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java index b378a31..654870f 100644 --- a/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java +++ b/src/main/java/com/manalejandro/mongodbcrud/vo/IndexVO.java @@ -5,52 +5,23 @@ import java.util.List; import com.manalejandro.mongodbcrud.model.Item; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@ToString +@Builder +@NoArgsConstructor +@AllArgsConstructor public class IndexVO { - private List listItem = new ArrayList(); + private final List listItem = new ArrayList(); private String id; private String nombre; private String apellido1; private String apellido2; - 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 getApellido2() { - return apellido2; - } - - public void setApellido2(String apellido2) { - this.apellido2 = apellido2; - } - } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1e4eeff..20dd544 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,5 @@ server.servlet.context-path=/mongodbcrud server.port=3117 -spring.thymeleaf.cache=false \ No newline at end of file +spring.data.mongodb.host=localhost +spring.data.mongodb.port=27017 +spring.data.mongodb.database=test \ No newline at end of file