Commit 45d46a2a authored by Sarika Sama's avatar Sarika Sama

additional operations

parent e00c5e0d
......@@ -4,13 +4,17 @@ package com.nisum.webfluxmongodb.controller;
import com.nisum.webfluxmongodb.dto.ProductDto;
import com.nisum.webfluxmongodb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import static org.apache.commons.lang3.StringUtils.isBlank;
@RestController
@RequestMapping("/products")
public class ProductController {
public class ProductController
{
@Autowired
private ProductService service;
......@@ -47,5 +51,17 @@ public class ProductController {
}
@GetMapping("/search")
public Mono<ProductDto> getSearchAllProducts(@PathVariable(name="quantity") int qty, @PathVariable(name="name") String name, @PathVariable(name="price") Double price) {
if(null != name || !isBlank(name)){
return service.getProductsByName(name);
}else if(qty>0){
return service.getProductByQuantity(qty);
}
return service.getProductByPrice(price);
}
}
\ No newline at end of file
......@@ -8,7 +8,8 @@ import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductDto {
public class ProductDto
{
private String id;
private String name;
......
......@@ -10,7 +10,8 @@ import org.springframework.data.mongodb.core.mapping.Document;
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "products")
public class Product {
public class Product
{
@Id
private String id;
private String name;
......
......@@ -6,8 +6,18 @@ import org.springframework.data.domain.Range;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Repository
public interface ProductRepository extends ReactiveMongoRepository<Product,String> {
public interface ProductRepository extends ReactiveMongoRepository<Product,String>
{
Flux<ProductDto> findByPriceBetween(Range<Double> priceRange);
Mono<ProductDto> findByName(String name);
Mono<ProductDto> findByPrice(double price);
Mono<ProductDto> findByQuantity(int qty);
}
......@@ -5,46 +5,74 @@ import com.nisum.webfluxmongodb.repository.ProductRepository;
import com.nisum.webfluxmongodb.utils.AppUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Range;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
@Service
public class ProductService {
public class ProductService
{
@Autowired
private ProductRepository repository;
public Flux<ProductDto> getProducts(){
public Flux<ProductDto> getProducts()
{
return repository.findAll().map(AppUtils::entityToDto);
}
public Mono<ProductDto> getProduct(String id){
public Mono<ProductDto> getProduct(String id)
{
return repository.findById(id).map(AppUtils::entityToDto);
}
public Flux<ProductDto> getProductInRange(double min,double max){
public Flux<ProductDto> getProductInRange(double min,double max)
{
return repository.findByPriceBetween(Range.closed(min,max));
}
public Mono<ProductDto> saveProduct(Mono<ProductDto> productDtoMono){
public Mono<ProductDto> saveProduct(Mono<ProductDto> productDtoMono)
{
System.out.println("service method called ...");
return productDtoMono.map(AppUtils::dtoToEntity)
.flatMap(repository::insert)
.map(AppUtils::entityToDto);
}
public Mono<ProductDto> updateProduct(Mono<ProductDto> productDtoMono,String id){
public Mono<ProductDto> updateProduct(Mono<ProductDto> productDtoMono,String id)
{
return repository.findById(id)
.flatMap(p->productDtoMono.map(AppUtils::dtoToEntity)
.doOnNext(e->e.setId(id)))
.flatMap(repository::save)
.map(AppUtils::entityToDto);
}
public Mono<Void> deleteProduct(String id){
public Mono<Void> deleteProduct(String id)
{
return repository.deleteById(id);
}
public Mono<ProductDto> getProductsByName(@RequestParam String name){
return (Mono<ProductDto>) repository.findByName(name);
}
public Mono<ProductDto> getProductByPrice(@RequestParam double price){
return (repository.findByPrice(price));
}
public Mono<ProductDto>getProductByQuantity(@RequestParam int qty){
return repository.findByQuantity(qty) ;
// Mono<ProductDto> productDtoMono = repository.findByQuantity(qty);
// Mono<ProductDto> productDtoMono1=productDtoMono.map(productDto -> productDto.getQty());
// return productDtoMono1;
}
}
\ No newline at end of file
......@@ -7,13 +7,15 @@ import org.springframework.beans.BeanUtils;
public class AppUtils {
public static ProductDto entityToDto(Product product) {
public static ProductDto entityToDto(Product product)
{
ProductDto productDto = new ProductDto();
BeanUtils.copyProperties(product, productDto);
return productDto;
}
public static Product dtoToEntity(ProductDto productDto) {
public static Product dtoToEntity(ProductDto productDto)
{
Product product = new Product();
BeanUtils.copyProperties(productDto, product);
return product;
......
server:
port: 9290
spring:
data:
mongodb:
database: prod_db
host: localhost
port: 27017
server.port=8088
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=prod_db
\ 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