Commit 67ebf5fa authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

[AFP-58] Fixed merge conflict with dev

parents 76cfd59e 9353e892
package com.nisum.ascend.inventory.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
@Configuration
@EnableWebFlux
public class WebFluxConfig implements WebFluxConfigurer {
}
package com.nisum.ascend.inventory.controller; package com.nisum.ascend.inventory.controller;
import com.nisum.ascend.inventory.dto.ProductDto; import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.exception.ResourceAlreadyExistsException;
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;
...@@ -12,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -12,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController @RestController
@RequestMapping("/api/products") @RequestMapping("/api/products")
...@@ -37,4 +41,9 @@ public class ProductController { ...@@ -37,4 +41,9 @@ public class ProductController {
public ResponseEntity<Flux<ProductDto>> getAllProducts() { public ResponseEntity<Flux<ProductDto>> getAllProducts() {
return ResponseEntity.ok(productService.findAllProducts()); return ResponseEntity.ok(productService.findAllProducts());
} }
@PostMapping("")
public Mono<ProductDto> postProduct(@RequestBody Product product) {
return productService.createProduct(product).onErrorMap(throwable -> new ResourceAlreadyExistsException());
}
} }
\ No newline at end of file
package com.nisum.ascend.inventory.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
public class ResourceAlreadyExistsException extends RuntimeException {
public ResourceAlreadyExistsException() {
super("A resource with the provided SKU already exists");
}
}
package com.nisum.ascend.inventory.exception;
import com.mongodb.DuplicateKeyException;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;
@Component
@Order(-2)
class RestWebExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof ResourceAlreadyExistsException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
// marks the response as complete and forbids writing to it
return exchange.getResponse().setComplete();
}
return Mono.error(ex);
}
}
...@@ -24,6 +24,7 @@ public class Product { ...@@ -24,6 +24,7 @@ public class Product {
private String brand; private String brand;
private String category; private String category;
public Product() {}
public Product(String sku, String upc, String productName, String productDescription, float price, int availableStock, public Product(String sku, String upc, String productName, String productDescription, float price, int availableStock,
String productImageUrl, String brand, String category) { String productImageUrl, String brand, String category) {
this.sku = sku; this.sku = sku;
......
...@@ -8,7 +8,6 @@ import org.springframework.stereotype.Service; ...@@ -8,7 +8,6 @@ import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import com.nisum.ascend.inventory.repository.ProductRepository; import com.nisum.ascend.inventory.repository.ProductRepository;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import static com.nisum.ascend.inventory.dto.ProductDto.generateDtoFromProduct; import static com.nisum.ascend.inventory.dto.ProductDto.generateDtoFromProduct;
@Service @Service
...@@ -35,4 +34,9 @@ public class ProductService { ...@@ -35,4 +34,9 @@ public class ProductService {
.map(ProductDto::generateDtoFromProduct); .map(ProductDto::generateDtoFromProduct);
} }
public Mono<ProductDto> createProduct(Product product) {
return productRepository.save(product)
.map(ProductDto::generateDtoFromProduct);
}
} }
...@@ -6,6 +6,7 @@ import com.nisum.ascend.inventory.repository.ProductRepository; ...@@ -6,6 +6,7 @@ import com.nisum.ascend.inventory.repository.ProductRepository;
import com.nisum.ascend.inventory.service.ProductService; import com.nisum.ascend.inventory.service.ProductService;
import com.nisum.ascend.inventory.controller.ProductController; import com.nisum.ascend.inventory.controller.ProductController;
import com.nisum.ascend.inventory.model.Product;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -34,6 +35,11 @@ import java.util.List; ...@@ -34,6 +35,11 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import reactor.core.publisher.Mono;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
...@@ -95,7 +101,6 @@ class ProductControllerTest { ...@@ -95,7 +101,6 @@ class ProductControllerTest {
}); });
} }
/* /*
If you want to do this test, make sure to insert the following in MongoDB Atlas: If you want to do this test, make sure to insert the following in MongoDB Atlas:
, ,
...@@ -122,5 +127,18 @@ class ProductControllerTest { ...@@ -122,5 +127,18 @@ class ProductControllerTest {
System.out.println("Deletion Successful. Ending test..."); System.out.println("Deletion Successful. Ending test...");
} }
@Test
void postProduct() {
String sku = "SH=" + UUID.randomUUID();
Product product = new Product(sku, "3d2dsd9cm", "Red Sweater",
"A Nice red sweater", (float) 23.99, 45, "",
"SweatCo", "Sweatshirts");
webTestClient.post().uri("/api/products")
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(Mono.just(product), Product.class)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.sku", sku);
}
} }
\ No newline at end of file
spring.config.import=classpath:secret.properties
server.port=8080
spring.data.mongodb.uri=mongodb+srv://admin:${db.password}@inventory-promotions.d4nfz.mongodb\
.net/${spring.data.mongodb.database}?retryWrites=true&w=majority
spring.data.mongodb.database=test
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