Commit bd6704cc authored by Syed Javed Ali's avatar Syed Javed Ali

Added ProductRepository and CustomProductRepository

parent ae4e5b43
...@@ -65,18 +65,28 @@ public class ProductController { ...@@ -65,18 +65,28 @@ public class ProductController {
@DeleteMapping(value = "/product-delete/{id}",produces = MediaType.APPLICATION_JSON_VALUE) @DeleteMapping(value = "/product-delete/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public Mono<Boolean> deleteById(@PathVariable String id){ public Mono<Boolean> deleteProductById(@PathVariable String id){
log.info("Calling service removeById()..."); log.info("Calling service removeById()...");
return productService.removeById(id) return productService.removeById(id)
.log(); .log();
} }
@PutMapping("/product-update-price/{id}") @PutMapping("/product-update-price")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public Mono<Product> modifyProductPrice(@PathVariable String id, public Mono<Product> modifyProductPrice(@RequestParam("prodId") String id,
@Valid @RequestBody Product product){ @RequestParam("prodPrice") Double prodPrice){
log.info("prodId:"+id+" "+"prodPrice "+prodPrice);
log.info("Calling service updateProductPrice()..."); log.info("Calling service updateProductPrice()...");
return productService.updateProductPrice(id,productMapper.toDto(product)) return productService.updateProductPrice(id,prodPrice)
.map(productMapper::toModel)
.log();
}
@GetMapping("/product-get-name/{name}")
@ResponseStatus(HttpStatus.OK)
public Flux<Product> fetchProductByName(@PathVariable("name") String prodName){
log.info("Calling service getProductByName()...");
return productService.getProductByName(prodName)
.map(productMapper::toModel) .map(productMapper::toModel)
.log(); .log();
......
package com.nisum.example.repository;
import com.nisum.example.dto.ProductDTO;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public interface CustomProductRepository {
Mono<ProductDTO> changeProductPrice(String id,Double newPrice);
Mono<Boolean> clearProductById(String id);
Flux<ProductDTO> pickProductByName(String prodName);
}
package com.nisum.example.repository;
import com.nisum.example.dto.ProductDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Slf4j
public class CustomProductRepositoryImpl implements CustomProductRepository {
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
//TODO result not as expected
@Override
public Mono<ProductDTO> changeProductPrice(String id, Double newPrice) {
Query query=new Query(Criteria.where("prodId").is(id));
Update update=new Update().set("prodPrice",newPrice);
log.info("Calling findandModify()....");
return reactiveMongoTemplate.findAndModify(query,update,ProductDTO.class);
}
@Override
public Mono<Boolean> clearProductById(String id) {
return reactiveMongoTemplate.remove(Query.query(Criteria.where("prodId").is(id)),
ProductDTO.class)
.flatMap(deleteResult -> Mono.just(deleteResult.wasAcknowledged()));
}
@Override
public Flux<ProductDTO> pickProductByName(String prodName) {
log.info("Calling template find()...");
return reactiveMongoTemplate
.find(Query.query(Criteria.where("prodName").is(prodName)),
ProductDTO.class);
}
}
package com.nisum.example.repository;
import com.nisum.example.dto.ProductDTO;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends ReactiveMongoRepository<ProductDTO,String>,CustomProductRepository {
}
...@@ -11,5 +11,6 @@ public interface IProductService { ...@@ -11,5 +11,6 @@ public interface IProductService {
Flux<ProductDTO> getProducts(); Flux<ProductDTO> getProducts();
Mono<ProductDTO> updateProduct(ProductDTO productDTO); Mono<ProductDTO> updateProduct(ProductDTO productDTO);
Mono<Boolean> removeById(String id); Mono<Boolean> removeById(String id);
Mono<ProductDTO> updateProductPrice(String id,ProductDTO productDTO); Mono<ProductDTO> updateProductPrice(String id,Double prodPrice);
Flux<ProductDTO> getProductByName(String prodName);
} }
package com.nisum.example.service; package com.nisum.example.service;
import com.nisum.example.dto.ProductDTO; import com.nisum.example.dto.ProductDTO;
import com.nisum.example.repository.ProductRepository;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
...@@ -21,6 +19,9 @@ public class ProductServiceImpl implements IProductService { ...@@ -21,6 +19,9 @@ public class ProductServiceImpl implements IProductService {
@Autowired @Autowired
private ReactiveMongoTemplate reactiveMongoTemplate; private ReactiveMongoTemplate reactiveMongoTemplate;
@Autowired
private ProductRepository productRepository;
/** /**
*Perform save operation of one product *Perform save operation of one product
* @param productDTO * @param productDTO
...@@ -28,8 +29,8 @@ public class ProductServiceImpl implements IProductService { ...@@ -28,8 +29,8 @@ public class ProductServiceImpl implements IProductService {
*/ */
@Override @Override
public Mono<ProductDTO> saveProduct(ProductDTO productDTO) { public Mono<ProductDTO> saveProduct(ProductDTO productDTO) {
log.info("Calling template save()..."); log.info("Calling repository save()...");
return reactiveMongoTemplate.save(productDTO); return productRepository.save(productDTO);
} }
/** /**
...@@ -39,42 +40,37 @@ public class ProductServiceImpl implements IProductService { ...@@ -39,42 +40,37 @@ public class ProductServiceImpl implements IProductService {
*/ */
@Override @Override
public Mono<ProductDTO> getProduct(String id) { public Mono<ProductDTO> getProduct(String id) {
log.info("Calling template findById()..."); log.info("Calling repository findById()...");
return reactiveMongoTemplate.findById(id,ProductDTO.class); return productRepository.findById(id);
} }
@Override @Override
public Flux<ProductDTO> getProducts() { public Flux<ProductDTO> getProducts() {
log.info("Calling template findAll()..."); log.info("Calling repository findAll()...");
return reactiveMongoTemplate.findAll(ProductDTO.class); return productRepository.findAll();
} }
@Override @Override
public Mono<ProductDTO> updateProduct(ProductDTO productDTO) { public Mono<ProductDTO> updateProduct(ProductDTO productDTO) {
log.info("Calling template save()..."); log.info("Calling repository save()...");
return reactiveMongoTemplate.save(productDTO); return productRepository.save(productDTO);
} }
@Override @Override
public Mono<Boolean> removeById(String id) { public Mono<Boolean> removeById(String id) {
log.info("Calling template remove()..."); log.info("Calling repository clearById()...");
return reactiveMongoTemplate.remove(Query.query(Criteria.where("id").is(id)), return productRepository.clearProductById(id);
ProductDTO.class) }
.flatMap(deleteResult -> Mono.just(deleteResult.wasAcknowledged()));
@Override
public Mono<ProductDTO> updateProductPrice(String id,Double prodPrice) {
log.info("Calling repository changePrice()...");
return productRepository.changeProductPrice(id,prodPrice);
} }
@Override @Override
public Mono<ProductDTO> updateProductPrice(String id,ProductDTO productDTO) { public Flux<ProductDTO> getProductByName(String prodName) {
log.info("Calling template findAndModify()..."); log.info("Calling repository pickProductByName()...");
return reactiveMongoTemplate.findById(id,ProductDTO.class) return productRepository.pickProductByName(prodName);
.flatMap(productDto->
reactiveMongoTemplate.findAndModify(Query
.query(Criteria.where("id").is(id)),
Update.update("prodPrice",productDTO.getProdPrice()),
ProductDTO.class)
.flatMap(result -> {
result.setProdPrice(productDto.getProdPrice());
return Mono.just(result);
}));
} }
} }
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