Commit 23eded19 authored by Syed Javed Ali's avatar Syed Javed Ali

Added GET end point to fetch all records,delete end point, update endpoint and...

Added GET end point to fetch all records,delete end point, update endpoint and update by id, added validation
parent 245c1d2e
...@@ -8,12 +8,17 @@ import lombok.extern.slf4j.Slf4j; ...@@ -8,12 +8,17 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.validation.Valid;
@RestController @RestController
@RequestMapping(value = ProductConstants.PRODUCT_SERVICE_ROOT,produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = ProductConstants.PRODUCT_SERVICE_ROOT,produces = MediaType.APPLICATION_JSON_VALUE)
@Slf4j @Slf4j
@Validated
public class ProductController { public class ProductController {
@Autowired @Autowired
...@@ -24,20 +29,56 @@ public class ProductController { ...@@ -24,20 +29,56 @@ public class ProductController {
@PostMapping(value = "/product-create",consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value = "/product-create",consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public Mono<Product> createProduct(@RequestBody Product product){ public Mono<Product> createProduct(@Valid @RequestBody Product product){
log.info("Calling service saveProduct() ..."); log.info("Calling service saveProduct() ...");
return productService.saveProduct(productMapper.toDto(product)) return productService.saveProduct(productMapper.toDto(product))
.map(productMapper::toModel) .map(productMapper::toModel)
.log(); .log();
} }
@GetMapping(value = "/product-get/{id}") @GetMapping(value = "/product-get/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public Mono<Product> fetchProduct(@PathVariable String id){ public Mono<Product> fetchProduct(@PathVariable String id){
log.info("Calling service getProduct()..."); log.info("Calling service getProduct()...");
return productService.getProduct(id) return productService.getProduct(id)
.map(productMapper::toModel) .map(productMapper::toModel)
.log(); .log();
}
@GetMapping(value = "/product-all",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.FOUND)
public Flux<Product> fetchProducts(){
log.info("Calling service getProducts()...");
return productService.getProducts()
.map(productMapper::toModel)
.log();
}
@PutMapping(value = "/product-update",consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public Mono<Product> modifyProduct(@Valid @RequestBody Product product){
log.info("Calling service updateProduct()...");
return productService.updateProduct(productMapper.toDto(product))
.map(productMapper::toModel)
.log();
}
@DeleteMapping(value = "/product-delete/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public Mono<Boolean> deleteById(@PathVariable String id){
log.info("Calling service removeById()...");
return productService.removeById(id)
.log();
}
@PutMapping("/product-update-price/{id}")
@ResponseStatus(HttpStatus.OK)
public Mono<Product> modifyProductPrice(@PathVariable String id,
@Valid @RequestBody Product product){
log.info("Calling service updateProductPrice()...");
return productService.updateProductPrice(id,productMapper.toDto(product))
.map(productMapper::toModel)
.log();
} }
} }
...@@ -3,6 +3,8 @@ package com.nisum.example.model; ...@@ -3,6 +3,8 @@ package com.nisum.example.model;
import lombok.*; import lombok.*;
import lombok.experimental.FieldDefaults; import lombok.experimental.FieldDefaults;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
@Data @Data
...@@ -12,8 +14,15 @@ import java.util.List; ...@@ -12,8 +14,15 @@ import java.util.List;
@FieldDefaults(level = AccessLevel.PRIVATE) @FieldDefaults(level = AccessLevel.PRIVATE)
public class Product { public class Product {
String prodId; String prodId;
@NotNull(message = "name should not be null")
@NotEmpty(message = "name cannot be empty")
String prodName; String prodName;
@NotNull(message = "description should not be null")
@NotEmpty(message = "description cannot be empty")
String prodDescription; String prodDescription;
@NotNull(message = "price should not be null")
Double prodPrice; Double prodPrice;
@NotNull(message = "Locations can not be null")
List<Location> prodLocationList; List<Location> prodLocationList;
} }
package com.nisum.example.service; package com.nisum.example.service;
import com.nisum.example.dto.ProductDTO; import com.nisum.example.dto.ProductDTO;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
public interface IProductService { public interface IProductService {
Mono<ProductDTO> saveProduct(ProductDTO productDTO); Mono<ProductDTO> saveProduct(ProductDTO productDTO);
Mono<ProductDTO> getProduct(String id); Mono<ProductDTO> getProduct(String id);
Flux<ProductDTO> getProducts();
Mono<ProductDTO> updateProduct(ProductDTO productDTO);
Mono<Boolean> removeById(String id);
Mono<ProductDTO> updateProductPrice(String id,ProductDTO productDTO);
} }
...@@ -4,7 +4,11 @@ import com.nisum.example.dto.ProductDTO; ...@@ -4,7 +4,11 @@ import com.nisum.example.dto.ProductDTO;
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.Mono; import reactor.core.publisher.Mono;
/** /**
...@@ -35,7 +39,42 @@ public class ProductServiceImpl implements IProductService { ...@@ -35,7 +39,42 @@ public class ProductServiceImpl implements IProductService {
*/ */
@Override @Override
public Mono<ProductDTO> getProduct(String id) { public Mono<ProductDTO> getProduct(String id) {
log.info("Calling service findById()..."); log.info("Calling template findById()...");
return reactiveMongoTemplate.findById(id,ProductDTO.class); return reactiveMongoTemplate.findById(id,ProductDTO.class);
} }
@Override
public Flux<ProductDTO> getProducts() {
log.info("Calling template findAll()...");
return reactiveMongoTemplate.findAll(ProductDTO.class);
}
@Override
public Mono<ProductDTO> updateProduct(ProductDTO productDTO) {
log.info("Calling template save()...");
return reactiveMongoTemplate.save(productDTO);
}
@Override
public Mono<Boolean> removeById(String id) {
log.info("Calling template remove()...");
return reactiveMongoTemplate.remove(Query.query(Criteria.where("id").is(id)),
ProductDTO.class)
.flatMap(deleteResult -> Mono.just(deleteResult.wasAcknowledged()));
}
@Override
public Mono<ProductDTO> updateProductPrice(String id,ProductDTO productDTO) {
log.info("Calling template findAndModify()...");
return reactiveMongoTemplate.findById(id,ProductDTO.class)
.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