Commit 7155b769 authored by Ben Anderson's avatar Ben Anderson

Resolved merge conflict with AFP-58

parents 823c3fdd 67ebf5fa
...@@ -8,15 +8,19 @@ import org.springframework.http.HttpStatus; ...@@ -8,15 +8,19 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.nisum.ascend.inventory.service.ProductService; import com.nisum.ascend.inventory.service.ProductService;
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 reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
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;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import com.nisum.ascend.inventory.service.ProductService; import com.nisum.ascend.inventory.service.ProductService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@RestController @RestController
...@@ -25,6 +29,13 @@ public class ProductController { ...@@ -25,6 +29,13 @@ public class ProductController {
@Autowired @Autowired
ProductService productService; ProductService productService;
@DeleteMapping(value = "/{sku}")
public Mono<ResponseEntity<Void>> deleteProduct(@PathVariable String sku) {
return productService.removeProductBySku(sku)
.map(res -> ResponseEntity.ok().<Void>build())
.defaultIfEmpty(ResponseEntity.notFound().build());
}
@GetMapping("/{sku}") @GetMapping("/{sku}")
public ResponseEntity<Mono<ProductDto>> getProductBySku(@PathVariable String sku) { public ResponseEntity<Mono<ProductDto>> getProductBySku(@PathVariable String sku) {
Mono<ProductDto> monoProd = productService.getProductBySku(sku); Mono<ProductDto> monoProd = productService.getProductBySku(sku);
......
package com.nisum.ascend.inventory.repository; package com.nisum.ascend.inventory.repository;
import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.model.Product; import com.nisum.ascend.inventory.model.Product;
import org.springframework.data.mongodb.repository.Query; import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
...@@ -9,6 +10,5 @@ import reactor.core.publisher.Mono; ...@@ -9,6 +10,5 @@ import reactor.core.publisher.Mono;
@Repository @Repository
public interface ProductRepository extends ReactiveMongoRepository<Product, String> { public interface ProductRepository extends ReactiveMongoRepository<Product, String> {
Mono<Product> findBySku(String sku); Mono<Product> findBySku(String sku);
} }
package com.nisum.ascend.inventory.service; package com.nisum.ascend.inventory.service;
import com.nisum.ascend.inventory.dto.ProductDto; import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.exception.ResourceNotFoundException; import com.nisum.ascend.inventory.exception.ResourceNotFoundException;
import com.nisum.ascend.inventory.model.Product; import com.nisum.ascend.inventory.model.Product;
...@@ -9,21 +8,21 @@ import org.springframework.stereotype.Service; ...@@ -9,21 +8,21 @@ 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;
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 {
@Autowired @Autowired
ProductRepository productRepository; ProductRepository productRepository;
public Mono<Product> removeProductBySku(String sku) {
return productRepository
.findBySku(sku)
.flatMap(existingProduct -> productRepository.delete(existingProduct)
.then(Mono.just(existingProduct)))
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "Product Not Found")));
}
public Mono<ProductDto> getProductBySku(String sku){ public Mono<ProductDto> getProductBySku(String sku){
return productRepository.findBySku(sku) return productRepository.findBySku(sku)
.map(existingProduct -> generateDtoFromProduct(existingProduct)) .map(existingProduct -> generateDtoFromProduct(existingProduct))
...@@ -54,4 +53,5 @@ public class ProductService { ...@@ -54,4 +53,5 @@ public class ProductService {
return productRepository.save(product) return productRepository.save(product)
.map(ProductDto::generateDtoFromProduct); .map(ProductDto::generateDtoFromProduct);
} }
} }
...@@ -100,6 +100,32 @@ class ProductControllerTest { ...@@ -100,6 +100,32 @@ class ProductControllerTest {
}); });
} }
/*
If you want to do this test, make sure to insert the following in MongoDB Atlas:
,
"sku": "SH=1123",
"upc":"3d2dsd9cm",
"productName":"Blue Sweater",
"productDescription":"A Nice red sweater",
"price":23.99,
"availableStock":45,
"blockedStock":0,
"productImageUrl":"",
"brand":"SweatCo",
"category":"Sweatshirts"
*/
@Test
public void testDeleteProduct(){
String sku = "SH=1123";
webTestClient.delete().uri("/api/products".concat("/{sku}"),sku)
.accept(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.exchange()
.expectStatus().isOk()
.expectBody(Void.class);
System.out.println("Deletion Successful. Ending test...");
}
@Test @Test
void postProduct() { void postProduct() {
String sku = "SH=" + UUID.randomUUID(); String sku = "SH=" + UUID.randomUUID();
......
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