diff --git a/persistence-modules/spring-data-cosmosdb/pom.xml b/persistence-modules/spring-data-cosmosdb/pom.xml new file mode 100644 index 0000000000..8bb103879d --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + spring-data-cosmosdb + spring-data-cosmos-db + tutorial for spring-data-cosmosdb + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.microsoft.azure + spring-data-cosmosdb + 2.3.0 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/AzurecosmodbApplication.java b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/AzurecosmodbApplication.java new file mode 100644 index 0000000000..2b145d14cd --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/AzurecosmodbApplication.java @@ -0,0 +1,44 @@ +package com.baeldung.spring.data.cosmosdb; + +import com.azure.data.cosmos.CosmosKeyCredential; +import com.baeldung.spring.data.cosmosdb.repository.ProductRepository; +import com.microsoft.azure.spring.data.cosmosdb.config.AbstractCosmosConfiguration; +import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig; +import com.microsoft.azure.spring.data.cosmosdb.repository.config.EnableCosmosRepositories; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +@EnableCosmosRepositories(basePackageClasses = ProductRepository.class) +public class AzurecosmodbApplication extends AbstractCosmosConfiguration { + + public static void main(String[] args) { + SpringApplication.run(AzurecosmodbApplication.class, args); + } + + @Value("${azure.cosmosdb.uri}") + private String uri; + + @Value("${azure.cosmosdb.key}") + private String key; + + @Value("${azure.cosmosdb.secondaryKey}") + private String secondaryKey; + + @Value("${azure.cosmosdb.database}") + private String dbName; + + private CosmosKeyCredential cosmosKeyCredential; + + @Bean + public CosmosDBConfig getConfig() { + this.cosmosKeyCredential = new CosmosKeyCredential(key); + CosmosDBConfig cosmosdbConfig = CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName) + .build(); + return cosmosdbConfig; + } + +} diff --git a/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/controller/ProductController.java b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/controller/ProductController.java new file mode 100644 index 0000000000..c1e38c9601 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/controller/ProductController.java @@ -0,0 +1,54 @@ +package com.baeldung.spring.data.cosmosdb.controller; + +import com.baeldung.spring.data.cosmosdb.entity.Product; +import com.baeldung.spring.data.cosmosdb.service.ProductService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/products") +public class ProductController { + + @Autowired + private ProductService productService; + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public void create(@RequestBody Product product) { + productService.saveProduct(product); + } + + @GetMapping(value = "/{id}/category/{category}") + public Optional get(@PathVariable String id, @PathVariable String category) { + return productService.findById(id, category); + } + + @DeleteMapping(value = "/{id}/category/{category}") + public void delete(@PathVariable String id, @PathVariable String category) { + productService.delete(id, category); + } + + @GetMapping + public List getByName(@RequestParam String name) { + return productService.findProductByName(name); + } + + @GetMapping(value = "/category/{category}") + public List getByCategory(@PathVariable String category) { + return productService.getProductsOfCategory(category); + } + +} diff --git a/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/entity/Product.java b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/entity/Product.java new file mode 100644 index 0000000000..07bb8889f5 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/entity/Product.java @@ -0,0 +1,58 @@ +package com.baeldung.spring.data.cosmosdb.entity; + +import com.microsoft.azure.spring.data.cosmosdb.core.mapping.Document; +import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey; + +import org.springframework.data.annotation.Id; + +@Document(collection = "products") +public class Product { + + @Id + private String productid; + + private String productName; + + private double price; + + @PartitionKey + private String productCategory; + + public String getProductid() { + return productid; + } + + public void setProductid(String productid) { + this.productid = productid; + } + + public String getProductName() { + return productName; + } + + public void setProductName(String productName) { + this.productName = productName; + } + + public String getProductCategory() { + return productCategory; + } + + public void setProductCategory(String productCategory) { + this.productCategory = productCategory; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + @Override + public String toString() { + return "Product [productid=" + productid + ", productName=" + productName + ", price=" + price + ", productCategory=" + productCategory + "]"; + } + +} diff --git a/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/repository/ProductRepository.java b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/repository/ProductRepository.java new file mode 100644 index 0000000000..29dc85a2cf --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/repository/ProductRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.data.cosmosdb.repository; + +import com.baeldung.spring.data.cosmosdb.entity.Product; +import com.microsoft.azure.spring.data.cosmosdb.repository.CosmosRepository; + +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ProductRepository extends CosmosRepository { + List findByProductName(String productName); + + List findByProductCategory(String category); +} diff --git a/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/service/ProductService.java b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/service/ProductService.java new file mode 100644 index 0000000000..6846d5205c --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/service/ProductService.java @@ -0,0 +1,39 @@ +package com.baeldung.spring.data.cosmosdb.service; + +import com.azure.data.cosmos.PartitionKey; +import com.baeldung.spring.data.cosmosdb.entity.Product; +import com.baeldung.spring.data.cosmosdb.repository.ProductRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class ProductService { + + @Autowired + private ProductRepository repository; + + public List findProductByName(String productName) { + return repository.findByProductName(productName); + } + + public Optional findById(String productId, String category) { + return repository.findById(productId, new PartitionKey(category)); + } + + public List getProductsOfCategory(String category) { + List products = repository.findByProductCategory(category); + return products; + } + + public void saveProduct(Product product) { + repository.save(product); + } + + public void delete(String productId, String category) { + repository.deleteById(productId, new PartitionKey(category)); + } +} diff --git a/persistence-modules/spring-data-cosmosdb/src/main/resources/application.properties b/persistence-modules/spring-data-cosmosdb/src/main/resources/application.properties new file mode 100644 index 0000000000..ba99ea2e6e --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/resources/application.properties @@ -0,0 +1,4 @@ +azure.cosmosdb.uri=cosmodb-uri +azure.cosmosdb.key=cosmodb-primary-key +azure.cosmosdb.secondaryKey=cosmodb-second-key +azure.cosmosdb.database=cosmodb-name \ No newline at end of file diff --git a/persistence-modules/spring-data-cosmosdb/src/test/java/com/baeldung/spring/data/cosmosdb/AzurecosmodbApplicationIntegrationTest.java b/persistence-modules/spring-data-cosmosdb/src/test/java/com/baeldung/spring/data/cosmosdb/AzurecosmodbApplicationIntegrationTest.java new file mode 100644 index 0000000000..0ef6af15e1 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/test/java/com/baeldung/spring/data/cosmosdb/AzurecosmodbApplicationIntegrationTest.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.data.cosmosdb; + +import com.azure.data.cosmos.PartitionKey; +import com.baeldung.spring.data.cosmosdb.entity.Product; +import com.baeldung.spring.data.cosmosdb.repository.ProductRepository; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.util.Assert; + +import java.util.Optional; + +//Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties +//to run the integration test +//@SpringBootTest +public class AzurecosmodbApplicationIntegrationTest { + + @Autowired + ProductRepository productRepository; + + // Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties + // to run the integration test + // @Test + public void givenProductIsCreated_whenCallFindById_thenProductIsFound() { + Product product = new Product(); + product.setProductid("1001"); + product.setProductCategory("Shirt"); + product.setPrice(110.0); + product.setProductName("Blue Shirt"); + + // Uncomment these lines when configured URI and keys for Azure Cosmos DB in application.properties + // to run the integration test + // productRepository.save(product); + // Optional retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt")); + // Assert.notNull(retrievedProduct, "Retrieved Product is Null"); + + } + +}