Commit b38b8453 authored by Ben Anderson's avatar Ben Anderson

Moved generateDtoFromProduct to Service

parent 822f9200
...@@ -49,7 +49,7 @@ public class ProductController { ...@@ -49,7 +49,7 @@ public class ProductController {
} }
@PutMapping("/{sku}") @PutMapping("/{sku}")
public Mono<ResponseEntity<Product>> updateProduct(@PathVariable String sku, @RequestBody Product product){ public Mono<ResponseEntity<ProductDto>> updateProduct(@PathVariable String sku, @RequestBody Product product){
return productService.updateProductBySku(sku, product) return productService.updateProductBySku(sku, product)
.map(updatedProduct -> ResponseEntity.ok(updatedProduct)) .map(updatedProduct -> ResponseEntity.ok(updatedProduct))
.defaultIfEmpty(ResponseEntity.badRequest().build()); .defaultIfEmpty(ResponseEntity.badRequest().build());
......
...@@ -20,13 +20,4 @@ public class ProductDto { ...@@ -20,13 +20,4 @@ public class ProductDto {
private String category; private String category;
private int stock; private int stock;
private List<PromotionDto> promotions; private List<PromotionDto> promotions;
public static ProductDto generateDtoFromProduct(Product product) {
// TODO: Fetch the product's promotion from the Promotions com.nisum.ascend.inventory.service
List<PromotionDto> promotions = new ArrayList<>();
return new ProductDto(product.getSku(), product.getUpc(), product.getProductName(), product.getProductDescription(), product.getPrice(), product.getProductImageUrl(), product.getBrand(), product.getCategory(), product.getAvailableStock(), promotions);
}
} }
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.dto.PromotionDto;
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;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -8,33 +9,45 @@ import org.springframework.stereotype.Service; ...@@ -8,33 +9,45 @@ 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 java.util.ArrayList;
import java.util.List;
@Service @Service
public class ProductService { public class ProductService {
@Autowired @Autowired
ProductRepository productRepository; ProductRepository productRepository;
public Mono<Product> removeProductBySku(String sku) { public ProductDto generateDtoFromProduct(Product product) {
// TODO: Fetch the product's promotion from the Promotions com.nisum.ascend.inventory.service
List<PromotionDto> promotions = new ArrayList<>();
return new ProductDto(product.getSku(), product.getUpc(), product.getProductName(), product.getProductDescription(), product.getPrice(), product.getProductImageUrl(), product.getBrand(), product.getCategory(), product.getAvailableStock(), promotions);
}
public Mono<ProductDto> removeProductBySku(String sku) {
return productRepository return productRepository
.findBySku(sku) .findBySku(sku)
.flatMap(existingProduct -> productRepository.delete(existingProduct) .flatMap(existingProduct -> productRepository.delete(existingProduct)
.then(Mono.just(existingProduct))) .then(Mono.just(existingProduct)))
.map(this::generateDtoFromProduct)
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "Product Not Found"))); .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(this::generateDtoFromProduct)
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found"))); .switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found")));
} }
public Flux<ProductDto> findAllProducts() { public Flux<ProductDto> findAllProducts() {
return productRepository.findAll() return productRepository.findAll()
.map(ProductDto::generateDtoFromProduct); .map(this::generateDtoFromProduct);
} }
public Mono<Product> updateProductBySku(String sku, Product product){ public Mono<ProductDto> updateProductBySku(String sku, Product product){
return productRepository.findBySku(sku) return productRepository.findBySku(sku)
.flatMap(dbProduct -> { .flatMap(dbProduct -> {
dbProduct.setUpc(product.getUpc()); dbProduct.setUpc(product.getUpc());
...@@ -46,12 +59,12 @@ public class ProductService { ...@@ -46,12 +59,12 @@ public class ProductService {
dbProduct.setAvailableStock(product.getAvailableStock()); dbProduct.setAvailableStock(product.getAvailableStock());
dbProduct.setProductImageUrl(product.getProductImageUrl()); dbProduct.setProductImageUrl(product.getProductImageUrl());
return productRepository.save(dbProduct); return productRepository.save(dbProduct);
}); }).map(this::generateDtoFromProduct);
} }
public Mono<ProductDto> createProduct(Product product) { public Mono<ProductDto> createProduct(Product product) {
return productRepository.save(product) return productRepository.save(product)
.map(ProductDto::generateDtoFromProduct); .map(this::generateDtoFromProduct);
} }
} }
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