Commit d90fbf02 authored by John Lam's avatar John Lam

get product by sku controller / service

parent 448064c7
package com.nisum.ascend.inventory.controller; package com.nisum.ascend.inventory.controller;
import com.nisum.ascend.inventory.dto.ProductDto;
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.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.RestController;
import com.nisum.ascend.inventory.service.ProductService; import com.nisum.ascend.inventory.service.ProductService;
import reactor.core.publisher.Mono;
@RestController @RestController
@RequestMapping("/products")
public class ProductController { public class ProductController {
@Autowired @Autowired
ProductService productService; ProductService productService;
@GetMapping("{sku}")
public ResponseEntity<Mono<ProductDto>> getProductDto(@PathVariable String sku) {
Mono<ProductDto> monoProd = productService.getProduct(sku);
HttpStatus status = monoProd != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<>(monoProd, status);
}
} }
...@@ -24,6 +24,5 @@ public class ProductDto { ...@@ -24,6 +24,5 @@ public class ProductDto {
PromotionDto promotion = new PromotionDto(); PromotionDto promotion = new PromotionDto();
return new ProductDto(product.getSku(), product.getUpc(), product.getProdName(), product.getProdDesc(), product.getPrice(), product.getProdImgUrl(), product.getBrand(), product.getCategory(), product.getAvailableStock(), promotion); return new ProductDto(product.getSku(), product.getUpc(), product.getProdName(), product.getProdDesc(), product.getPrice(), product.getProdImgUrl(), product.getBrand(), product.getCategory(), product.getAvailableStock(), promotion);
} }
} }
package com.nisum.ascend.inventory.exception;
import org.springframework.http.HttpStatus;
public class ResourceNotFoundException extends Exception{
HttpStatus status;
public ResourceNotFoundException(HttpStatus status, String message) {
super(message);
this.status = status;
}
}
...@@ -8,7 +8,7 @@ import org.springframework.data.mongodb.core.mapping.Document; ...@@ -8,7 +8,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
@Getter @Getter
@Setter @Setter
@Document @Document(collection = "products")
public class Product { public class Product {
@Id @Id
private String id; private String id;
......
package com.nisum.ascend.inventory.repository; package com.nisum.ascend.inventory.repository;
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.ReactiveMongoRepository; import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
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);
} }
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.model.Product;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; 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 static com.nisum.ascend.inventory.dto.ProductDto.generateDtoFromProduct;
@Service @Service
public class ProductService { public class ProductService {
@Autowired @Autowired
ProductRepository productRepository; ProductRepository productRepository;
public Mono<ProductDto> getProduct(String sku){
return productRepository.findBySku(sku)
.map(existingProduct -> generateDtoFromProduct(existingProduct))
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found")));
}
} }
...@@ -3,4 +3,4 @@ spring.config.import=classpath:secret.properties ...@@ -3,4 +3,4 @@ spring.config.import=classpath:secret.properties
server.port=8080 server.port=8080
spring.data.mongodb.uri=mongodb+srv://admin:${db.password}@inventory-promotions.d4nfz.mongodb\ spring.data.mongodb.uri=mongodb+srv://admin:${db.password}@inventory-promotions.d4nfz.mongodb\
.net/${spring.data.mongodb.database}?retryWrites=true&w=majority .net/${spring.data.mongodb.database}?retryWrites=true&w=majority
spring.data.mongodb.database=products-promotions-DB spring.data.mongodb.database=test
package com.nisum.ascend.inventory.controller;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ProductControllerTest {
@BeforeEach
void setUp() {
}
@Test
void getProductDto() {
}
}
\ 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