Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/> <!-- lookup parent from com.javatest.repository -->
</parent>

<groupId>com.javatest</groupId>
<artifactId>javatest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>javaTest</name>

<properties>
<java.version>17</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


11 changes: 11 additions & 0 deletions src/main/java/com/javatest/Application.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/javatest/api/ProductRestController.java
Original file line number Diff line number Diff line change
@@ -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<Product> getAll() {
log.info("Getting all products.");
return service.getAll();
}

@RequestMapping(value = "/products/{id}", method = RequestMethod.GET)
public ResponseEntity<Object> getById(@PathVariable String id) {
log.info("Getting product by Id.");
Optional<Product> 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<Object> 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);
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/javatest/document/Product.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/javatest/repository/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -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<Product, String> {
}
19 changes: 19 additions & 0 deletions src/main/java/com/javatest/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -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<Product> getAll();

Optional<Product> getById(String id);

Boolean deleteById(String id);

Product create(Product product);

}
56 changes: 56 additions & 0 deletions src/main/java/com/javatest/service/impl/ProductServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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<Product> getAll() {
return repository.findAll();
}

@Override
public Optional<Product> getById(String id) {
Optional<Product> 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;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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