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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.nisum.ascend.inventory.service.ProductService;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import org.springframework.web.reactive.function.server.ServerResponse;
......@@ -41,12 +36,12 @@ public class ProductController {
return ResponseEntity.ok(productService.findAllProducts());
}
// @PutMapping("/{sku}")
// public Mono<ResponseEntity<Product>> updateProduct(@PathVariable String sku, @RequestBody Product product){
// return productService.updateProductBySku(sku,user)
// .map(updatedProduct -> ResponseEntity.ok(updatedProduct))
// .defaultIfEmpty(ResponseEntity.badRequest().build());
// }
@PutMapping("/{sku}")
public Mono<ResponseEntity<Product>> updateProduct(@PathVariable String sku, @RequestBody Product product){
return productService.updateProductBySku(sku, product)
.map(updatedProduct -> ResponseEntity.ok(updatedProduct))
.defaultIfEmpty(ResponseEntity.badRequest().build());
}
@PostMapping("")
public Mono<ProductDto> postProduct(@RequestBody Product product) {
......
......@@ -35,18 +35,17 @@ public class ProductService {
.map(ProductDto::generateDtoFromProduct);
}
public Mono<Product> updateProduct(String sku, Product product){
return productRepository.findById(sku)
public Mono<Product> updateProductBySku(String sku, Product product){
return productRepository.findBySku(sku)
.flatMap(dbProduct -> {
dbProduct.setUpc(product.getUpc());
dbProduct.setProductName(product.getProductName());
dbProduct.setProductDescription(product.getProductDescription());
dbProduct.setBrand(product.getBrand());
dbProduct.setCategory(product.getCategory());
dbProduct.setPrice(product.getPrice());
dbProduct.setAvailableStock(product.getAvailableStock());
dbProduct.setBlockedStock(product.getBlockedStock());
dbProduct.setProductImageUrl(product.getProductImageUrl());
dbProduct.setBrand(product.getBrand());
dbProduct.setCategory(product.getCategory());
return productRepository.save(dbProduct);
});
}
......
......@@ -81,7 +81,6 @@ class ProductControllerTest {
.jsonPath("$.sku", sku);
}
@Test
void testFindAllProducts(){
webTestClient.get()
......@@ -101,9 +100,6 @@ class ProductControllerTest {
});
}
@Test
void postProduct() {
String sku = "SH=" + UUID.randomUUID();
......@@ -118,4 +114,38 @@ class ProductControllerTest {
.expectBody()
.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