From b118860f10b5b25c4b389c96b8f3e3a925cc440e Mon Sep 17 00:00:00 2001 From: ciret Date: Wed, 22 Mar 2023 12:03:12 -0400 Subject: [PATCH 1/2] Estructura del proyecto e implementacion de servicio getAll --- pom.xml | 45 +++++++++++++++ src/main/java/com/javatest/Application.java | 11 ++++ .../javatest/api/ProductRestController.java | 33 +++++++++++ .../java/com/javatest/document/Product.java | 57 +++++++++++++++++++ .../repository/ProductRepository.java | 10 ++++ .../com/javatest/service/ProductService.java | 18 ++++++ .../service/impl/ProductServiceImpl.java | 41 +++++++++++++ src/main/resources/application.properties | 7 +++ 8 files changed, 222 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/javatest/Application.java create mode 100644 src/main/java/com/javatest/api/ProductRestController.java create mode 100644 src/main/java/com/javatest/document/Product.java create mode 100644 src/main/java/com/javatest/repository/ProductRepository.java create mode 100644 src/main/java/com/javatest/service/ProductService.java create mode 100644 src/main/java/com/javatest/service/impl/ProductServiceImpl.java create mode 100644 src/main/resources/application.properties diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f7dc58b --- /dev/null +++ b/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.4 + + + + com.javatest + javatest + 0.0.1-SNAPSHOT + jar + + javaTest + + + 17 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/src/main/java/com/javatest/Application.java b/src/main/java/com/javatest/Application.java new file mode 100644 index 0000000..6e323bb --- /dev/null +++ b/src/main/java/com/javatest/Application.java @@ -0,0 +1,11 @@ +package com.javatest; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/src/main/java/com/javatest/api/ProductRestController.java b/src/main/java/com/javatest/api/ProductRestController.java new file mode 100644 index 0000000..ece65e5 --- /dev/null +++ b/src/main/java/com/javatest/api/ProductRestController.java @@ -0,0 +1,33 @@ +package com.javatest.api; + + +import com.javatest.document.Product; +import com.javatest.service.ProductService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping(value = "/") +public class ProductRestController { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + private final ProductService service; + + @Autowired + public ProductRestController(ProductService service) { + this.service = service; + } + + @RequestMapping(value = "/products", method = RequestMethod.GET) + public List getAll() { + log.info("Getting all products."); + return service.getAll(); + } +} diff --git a/src/main/java/com/javatest/document/Product.java b/src/main/java/com/javatest/document/Product.java new file mode 100644 index 0000000..8aabfa0 --- /dev/null +++ b/src/main/java/com/javatest/document/Product.java @@ -0,0 +1,57 @@ +package com.javatest.document; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.math.BigDecimal; + +@Document +public class Product { + + @Id + private String _id; + private String name; + private String description; + private BigDecimal price; + private Integer amount; + + public String get_id() { + return _id; + } + + public void set_id(String _id) { + this._id = _id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public Integer getAmount() { + return amount; + } + + public void setAmount(Integer amount) { + this.amount = amount; + } +} diff --git a/src/main/java/com/javatest/repository/ProductRepository.java b/src/main/java/com/javatest/repository/ProductRepository.java new file mode 100644 index 0000000..8c37bca --- /dev/null +++ b/src/main/java/com/javatest/repository/ProductRepository.java @@ -0,0 +1,10 @@ +package com.javatest.repository; + + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; +import com.javatest.document.Product; + +@Repository +public interface ProductRepository extends MongoRepository { +} diff --git a/src/main/java/com/javatest/service/ProductService.java b/src/main/java/com/javatest/service/ProductService.java new file mode 100644 index 0000000..fff539f --- /dev/null +++ b/src/main/java/com/javatest/service/ProductService.java @@ -0,0 +1,18 @@ +package com.javatest.service; + +import com.javatest.document.Product; + +import java.util.List; + + +public interface ProductService { + + List getAll(); + + Product getById(String id); + + Boolean deleteById(String id); + + Product create(Product product); + +} diff --git a/src/main/java/com/javatest/service/impl/ProductServiceImpl.java b/src/main/java/com/javatest/service/impl/ProductServiceImpl.java new file mode 100644 index 0000000..a60c521 --- /dev/null +++ b/src/main/java/com/javatest/service/impl/ProductServiceImpl.java @@ -0,0 +1,41 @@ +package com.javatest.service.impl; + +import com.javatest.document.Product; +import com.javatest.repository.ProductRepository; +import com.javatest.service.ProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class ProductServiceImpl implements ProductService { + + private final ProductRepository repository; + + @Autowired + public ProductServiceImpl(ProductRepository repository) { + this.repository = repository; + } + + @Override + public List getAll() { + return repository.findAll(); + } + + @Override + public Product getById(String id) { + return repository.findById(id).get(); + } + + @Override + public Boolean deleteById(String id) { + repository.deleteById(id); + return repository.findById(id).isEmpty(); + } + + @Override + public Product create(Product product) { + return repository.save(product); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..d9c31b7 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,7 @@ + +spring.data.mongodb.authentication-database=admin +spring.data.mongodb.username=root +spring.data.mongodb.password=example +spring.data.mongodb.database=prod +spring.data.mongodb.port=27017 +spring.data.mongodb.host=localhost From 1674619640b2b4d964580b0afe915fff9835a129 Mon Sep 17 00:00:00 2001 From: ciret Date: Wed, 22 Mar 2023 12:41:31 -0400 Subject: [PATCH 2/2] Implementacion de servicio getById, create y delete --- .../javatest/api/ProductRestController.java | 29 +++++++++++++++++-- .../com/javatest/service/ProductService.java | 3 +- .../service/impl/ProductServiceImpl.java | 21 ++++++++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/javatest/api/ProductRestController.java b/src/main/java/com/javatest/api/ProductRestController.java index ece65e5..8928e68 100644 --- a/src/main/java/com/javatest/api/ProductRestController.java +++ b/src/main/java/com/javatest/api/ProductRestController.java @@ -6,11 +6,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; @RestController @RequestMapping(value = "/") @@ -30,4 +30,27 @@ public List getAll() { log.info("Getting all products."); return service.getAll(); } + + @RequestMapping(value = "/products/{id}", method = RequestMethod.GET) + public ResponseEntity getById(@PathVariable String id) { + log.info("Getting product by Id."); + Optional product = service.getById(id); + if (product.isEmpty()) + return ResponseEntity.ok("No existe un producto con ese ID"); + else + return ResponseEntity.ok(product.get()); + } + + @PostMapping("/products") + public ResponseEntity create(@RequestBody Product product) { + log.info("Creating new product"); + Product productNew = service.create(product); + return ResponseEntity.ok(productNew); + } + + @DeleteMapping("/products/{id}") + public void delete(@PathVariable String id) { + log.info("Deleting product"); + service.deleteById(id); + } } diff --git a/src/main/java/com/javatest/service/ProductService.java b/src/main/java/com/javatest/service/ProductService.java index fff539f..920a41e 100644 --- a/src/main/java/com/javatest/service/ProductService.java +++ b/src/main/java/com/javatest/service/ProductService.java @@ -3,13 +3,14 @@ import com.javatest.document.Product; import java.util.List; +import java.util.Optional; public interface ProductService { List getAll(); - Product getById(String id); + Optional getById(String id); Boolean deleteById(String id); diff --git a/src/main/java/com/javatest/service/impl/ProductServiceImpl.java b/src/main/java/com/javatest/service/impl/ProductServiceImpl.java index a60c521..25a7271 100644 --- a/src/main/java/com/javatest/service/impl/ProductServiceImpl.java +++ b/src/main/java/com/javatest/service/impl/ProductServiceImpl.java @@ -3,14 +3,19 @@ import com.javatest.document.Product; import com.javatest.repository.ProductRepository; import com.javatest.service.ProductService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; +import java.util.Optional; @Component public class ProductServiceImpl implements ProductService { + private final Logger log = LoggerFactory.getLogger(getClass()); + private final ProductRepository repository; @Autowired @@ -24,8 +29,11 @@ public List getAll() { } @Override - public Product getById(String id) { - return repository.findById(id).get(); + public Optional getById(String id) { + Optional unitOptional = repository.findById(id); + if (unitOptional.isEmpty()) + return Optional.empty(); + return unitOptional; } @Override @@ -36,6 +44,13 @@ public Boolean deleteById(String id) { @Override public Product create(Product product) { - return repository.save(product); + Product product1 = new Product(); + try { + product1 = repository.save(product); + + }catch (RuntimeException e){ + log.error(e.getMessage()); + } + return product1; } }