Commit 66d5f1fc authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

Merge branch 'dev' of https://gitlab.mynisum.com/ascend/inventory-backend into AFP-57

Need to update AFP-57
parents e8e02a98 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 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;
...@@ -14,6 +15,13 @@ import reactor.core.publisher.Mono; ...@@ -14,6 +15,13 @@ 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.RestController;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import com.nisum.ascend.inventory.service.ProductService;
@RestController @RestController
@RequestMapping("/api/products") @RequestMapping("/api/products")
...@@ -42,3 +50,9 @@ public class ProductController { ...@@ -42,3 +50,9 @@ public class ProductController {
} }
@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;
......
...@@ -11,6 +11,13 @@ import com.nisum.ascend.inventory.repository.ProductRepository; ...@@ -11,6 +11,13 @@ 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;
import com.nisum.ascend.inventory.exception.ResourceAlreadyExistsException;
import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import com.nisum.ascend.inventory.repository.ProductRepository;
@Service @Service
public class ProductService { public class ProductService {
...@@ -45,4 +52,8 @@ public class ProductService { ...@@ -45,4 +52,8 @@ public class ProductService {
} }
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)
...@@ -96,4 +102,20 @@ class ProductControllerTest { ...@@ -96,4 +102,20 @@ class ProductControllerTest {
} }
@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