Commit 14dde8b3 authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

[AFP-57] Work on PUT product endpoint

parent d321aeed
...@@ -6,17 +6,12 @@ import com.nisum.ascend.inventory.model.Product; ...@@ -6,17 +6,12 @@ import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nisum.ascend.inventory.service.ProductService; import com.nisum.ascend.inventory.service.ProductService;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
...@@ -41,12 +36,12 @@ public class ProductController { ...@@ -41,12 +36,12 @@ public class ProductController {
return ResponseEntity.ok(productService.findAllProducts()); return ResponseEntity.ok(productService.findAllProducts());
} }
// @PutMapping("/{sku}") @PutMapping("/{sku}")
// public Mono<ResponseEntity<Product>> updateProduct(@PathVariable String sku, @RequestBody Product product){ public Mono<ResponseEntity<Product>> updateProduct(@PathVariable String sku, @RequestBody Product product){
// return productService.updateProductBySku(sku,user) return productService.updateProductBySku(sku, product)
// .map(updatedProduct -> ResponseEntity.ok(updatedProduct)) .map(updatedProduct -> ResponseEntity.ok(updatedProduct))
// .defaultIfEmpty(ResponseEntity.badRequest().build()); .defaultIfEmpty(ResponseEntity.badRequest().build());
// } }
@PostMapping("") @PostMapping("")
public Mono<ProductDto> postProduct(@RequestBody Product product) { public Mono<ProductDto> postProduct(@RequestBody Product product) {
......
...@@ -35,18 +35,17 @@ public class ProductService { ...@@ -35,18 +35,17 @@ public class ProductService {
.map(ProductDto::generateDtoFromProduct); .map(ProductDto::generateDtoFromProduct);
} }
public Mono<Product> updateProduct(String sku, Product product){ public Mono<Product> updateProductBySku(String sku, Product product){
return productRepository.findById(sku) return productRepository.findBySku(sku)
.flatMap(dbProduct -> { .flatMap(dbProduct -> {
dbProduct.setUpc(product.getUpc()); dbProduct.setUpc(product.getUpc());
dbProduct.setProductName(product.getProductName()); dbProduct.setProductName(product.getProductName());
dbProduct.setProductDescription(product.getProductDescription()); dbProduct.setProductDescription(product.getProductDescription());
dbProduct.setBrand(product.getBrand());
dbProduct.setCategory(product.getCategory());
dbProduct.setPrice(product.getPrice()); dbProduct.setPrice(product.getPrice());
dbProduct.setAvailableStock(product.getAvailableStock()); dbProduct.setAvailableStock(product.getAvailableStock());
dbProduct.setBlockedStock(product.getBlockedStock());
dbProduct.setProductImageUrl(product.getProductImageUrl()); dbProduct.setProductImageUrl(product.getProductImageUrl());
dbProduct.setBrand(product.getBrand());
dbProduct.setCategory(product.getCategory());
return productRepository.save(dbProduct); return productRepository.save(dbProduct);
}); });
} }
......
...@@ -81,7 +81,6 @@ class ProductControllerTest { ...@@ -81,7 +81,6 @@ class ProductControllerTest {
.jsonPath("$.sku", sku); .jsonPath("$.sku", sku);
} }
@Test @Test
void testFindAllProducts(){ void testFindAllProducts(){
webTestClient.get() webTestClient.get()
...@@ -101,9 +100,6 @@ class ProductControllerTest { ...@@ -101,9 +100,6 @@ class ProductControllerTest {
}); });
} }
@Test @Test
void postProduct() { void postProduct() {
String sku = "SH=" + UUID.randomUUID(); String sku = "SH=" + UUID.randomUUID();
...@@ -118,4 +114,38 @@ class ProductControllerTest { ...@@ -118,4 +114,38 @@ class ProductControllerTest {
.expectBody() .expectBody()
.jsonPath("$.sku", sku); .jsonPath("$.sku", sku);
} }
@Test
public void updateProduct(){
double newPrice = 49.99;
String sku = "SH=1123";
Product product = new Product(sku, "3d2dsd9cm", "Blue Sweater",
"A Nice red sweater", (float) newPrice, 45, "",
"SweatCo", "Sweatshirts");
webTestClient.put().uri("/api/products".concat("/{sku}"),sku)
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.accept(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(Mono.just(product),Product.class)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.price").isEqualTo(newPrice);
}
@Test
public void updateProduct_notFound(){
double newPrice = 49.99;
String sku = "SH=2021";
Product product = new Product(sku, "3d2dsd9cm", "Blue Sweater",
"A Nice red sweater", (float) 23.99, 45, "",
"SweatCo", "Sweatshirts");
webTestClient.put().uri("/api/products".concat("/{sku}"),sku)
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.accept(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(Mono.just(product),Product.class)
.exchange()
.expectStatus().isBadRequest();
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment