Implementing Code Review comments

This commit is contained in:
Ankur Gupta 2020-07-30 01:43:57 +05:30
parent b5eef05476
commit 23f1333aaa
5 changed files with 62 additions and 89 deletions

View File

@ -16,6 +16,7 @@
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<cosmodb.version>2.3.0</cosmodb.version>
</properties> </properties>
<dependencies> <dependencies>
@ -26,14 +27,17 @@
<dependency> <dependency>
<groupId>com.microsoft.azure</groupId> <groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId> <artifactId>spring-data-cosmosdb</artifactId>
<version>2.3.0</version> <version>${cosmodb.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -22,9 +22,13 @@ import java.util.Optional;
@RequestMapping("/products") @RequestMapping("/products")
public class ProductController { public class ProductController {
@Autowired
private ProductService productService; private ProductService productService;
@Autowired
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping @PostMapping
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody Product product) { public void create(@RequestBody Product product) {
@ -46,8 +50,8 @@ public class ProductController {
return productService.findProductByName(name); return productService.findProductByName(name);
} }
@GetMapping(value = "/category/{category}") @GetMapping(value = "/category")
public List<Product> getByCategory(@PathVariable String category) { public List<Product> getByCategory(@RequestParam String category) {
return productService.getProductsOfCategory(category); return productService.getProductsOfCategory(category);
} }

View File

@ -5,6 +5,11 @@ import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Document(collection = "products") @Document(collection = "products")
public class Product { public class Product {
@ -18,41 +23,4 @@ public class Product {
@PartitionKey @PartitionKey
private String productCategory; 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 + "]";
}
} }

View File

@ -25,8 +25,7 @@ public class ProductService {
} }
public List<Product> getProductsOfCategory(String category) { public List<Product> getProductsOfCategory(String category) {
List<Product> products = repository.findByProductCategory(category); return repository.findByProductCategory(category);
return products;
} }
public void saveProduct(Product product) { public void saveProduct(Product product) {

View File

@ -14,7 +14,7 @@ import java.util.Optional;
//Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties //Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties
//to run the integration test //to run the integration test
//@SpringBootTest //@SpringBootTest
public class AzurecosmodbApplicationIntegrationTest { public class AzurecosmodbApplicationManualTest {
@Autowired @Autowired
ProductRepository productRepository; ProductRepository productRepository;
@ -29,11 +29,9 @@ public class AzurecosmodbApplicationIntegrationTest {
product.setPrice(110.0); product.setPrice(110.0);
product.setProductName("Blue Shirt"); product.setProductName("Blue Shirt");
// Uncomment these lines when configured URI and keys for Azure Cosmos DB in application.properties productRepository.save(product);
// to run the integration test Optional<Product> retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt"));
// productRepository.save(product); Assert.notNull(retrievedProduct, "Retrieved Product is Null");
// Optional<Product> retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt"));
// Assert.notNull(retrievedProduct, "Retrieved Product is Null");
} }