diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 3e1a90b737..522c281386 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -55,6 +55,7 @@ spring-boot-persistence-mongodb spring-data-cassandra spring-data-cassandra-reactive + spring-data-cosmosdb spring-data-couchbase-2 spring-data-dynamodb spring-data-eclipselink diff --git a/persistence-modules/spring-data-cosmosdb/pom.xml b/persistence-modules/spring-data-cosmosdb/pom.xml new file mode 100644 index 0000000000..75cc830578 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/pom.xml @@ -0,0 +1,51 @@ + + + 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 + 2.3.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.microsoft.azure + spring-data-cosmosdb + ${cosmodb.version} + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/AzureCosmosDbApplication.java b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/AzureCosmosDbApplication.java new file mode 100644 index 0000000000..5a1764adc6 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/AzureCosmosDbApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.data.cosmosdb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AzureCosmosDbApplication { + + public static void main(String[] args) { + SpringApplication.run(AzureCosmosDbApplication.class, args); + } +} diff --git a/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/config/AzureCosmosDbConfiguration.java b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/config/AzureCosmosDbConfiguration.java new file mode 100644 index 0000000000..7d3322faf6 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/config/AzureCosmosDbConfiguration.java @@ -0,0 +1,37 @@ +package com.baeldung.spring.data.cosmosdb.config; + +import com.azure.data.cosmos.CosmosKeyCredential; +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.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableCosmosRepositories(basePackages = "com.baeldung.spring.data.cosmosdb.repository") +public class AzureCosmosDbConfiguration extends AbstractCosmosConfiguration { + + @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..25f88bac72 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/controller/ProductController.java @@ -0,0 +1,53 @@ +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 { + + private ProductService productService; + + @Autowired + public ProductController(ProductService productService) { + this.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); + } + +} 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..10bbe1b9ae --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/entity/Product.java @@ -0,0 +1,26 @@ +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; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Document(collection = "products") +public class Product { + + @Id + private String productid; + + private String productName; + + private double price; + + @PartitionKey + private String 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..1e4a2987a1 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/repository/ProductRepository.java @@ -0,0 +1,14 @@ +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); + +} 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..0d1cf7c6a6 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/main/java/com/baeldung/spring/data/cosmosdb/service/ProductService.java @@ -0,0 +1,38 @@ +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 { + + private ProductRepository repository; + + @Autowired + public ProductService(ProductRepository repository) { + this.repository = 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 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/AzureCosmosDbApplicationManualTest.java b/persistence-modules/spring-data-cosmosdb/src/test/java/com/baeldung/spring/data/cosmosdb/AzureCosmosDbApplicationManualTest.java new file mode 100644 index 0000000000..9170068173 --- /dev/null +++ b/persistence-modules/spring-data-cosmosdb/src/test/java/com/baeldung/spring/data/cosmosdb/AzureCosmosDbApplicationManualTest.java @@ -0,0 +1,33 @@ +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; + +@SpringBootTest +public class AzureCosmosDbApplicationManualTest { + + @Autowired + ProductRepository productRepository; + + @Test + public void givenProductIsCreated_whenCallFindById_thenProductIsFound() { + Product product = new Product(); + product.setProductid("1001"); + product.setProductCategory("Shirt"); + product.setPrice(110.0); + product.setProductName("Blue Shirt"); + + productRepository.save(product); + Product retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt")) + .orElse(null); + Assert.notNull(retrievedProduct, "Retrieved Product is Null"); + + } + +}