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..8928e68 --- /dev/null +++ b/src/main/java/com/javatest/api/ProductRestController.java @@ -0,0 +1,56 @@ +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.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@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(); + } + + @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/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..920a41e --- /dev/null +++ b/src/main/java/com/javatest/service/ProductService.java @@ -0,0 +1,19 @@ +package com.javatest.service; + +import com.javatest.document.Product; + +import java.util.List; +import java.util.Optional; + + +public interface ProductService { + + List getAll(); + + Optional 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..25a7271 --- /dev/null +++ b/src/main/java/com/javatest/service/impl/ProductServiceImpl.java @@ -0,0 +1,56 @@ +package com.javatest.service.impl; + +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 + public ProductServiceImpl(ProductRepository repository) { + this.repository = repository; + } + + @Override + public List getAll() { + return repository.findAll(); + } + + @Override + public Optional getById(String id) { + Optional unitOptional = repository.findById(id); + if (unitOptional.isEmpty()) + return Optional.empty(); + return unitOptional; + } + + @Override + public Boolean deleteById(String id) { + repository.deleteById(id); + return repository.findById(id).isEmpty(); + } + + @Override + public Product create(Product product) { + Product product1 = new Product(); + try { + product1 = repository.save(product); + + }catch (RuntimeException e){ + log.error(e.getMessage()); + } + return product1; + } +} 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