Commit 245c1d2e authored by Syed Javed Ali's avatar Syed Javed Ali

Added POST end point for adding product and GET end point to fetch product based on id

parent 6d8eb80c
package com.nisum.example.constants;
/**
* Product Constants with equivalent values
*/
public interface ProductConstants {
String PRODUCT_SERVICE_ROOT="product-service";
}
......@@ -4,16 +4,16 @@ import com.nisum.example.constants.ProductConstants;
import com.nisum.example.mapper.ProductMapper;
import com.nisum.example.model.Product;
import com.nisum.example.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping(value = ProductConstants.PRODUCT_SERVICE_ROOT,produces = MediaType.APPLICATION_JSON_VALUE)
@Slf4j
public class ProductController {
@Autowired
......@@ -22,11 +22,22 @@ public class ProductController {
@Autowired
private IProductService productService;
@PostMapping(value = "/product-create")
@PostMapping(value = "/product-create",consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Mono<Product> createProduct(@RequestBody Product product){
log.info("Calling service saveProduct() ...");
return productService.saveProduct(productMapper.toDto(product))
.map(productMapper::toModel)
.log();
}
@GetMapping(value = "/product-get/{id}")
@ResponseStatus(HttpStatus.OK)
public Mono<Product> fetchProduct(@PathVariable String id){
log.info("Calling service getProduct()...");
return productService.getProduct(id)
.map(productMapper::toModel)
.log();
}
}
......@@ -4,7 +4,7 @@ import com.nisum.example.dto.LocationDTO;
import com.nisum.example.model.Location;
import org.mapstruct.Mapper;
@Mapper
@Mapper(componentModel = "spring")
public interface LocationMapper {
LocationDTO toDto(Location location);
}
......@@ -11,5 +11,6 @@ public interface ProductMapper {
@Mapping(source = "product.prodLocationList",target = "prodLocationDTOList")
ProductDTO toDto(Product product);
@Mapping(source = "productDTO.prodLocationDTOList",target = "prodLocationList")
Product toModel(ProductDTO productDTO);
}
......@@ -6,4 +6,5 @@ import reactor.core.publisher.Mono;
public interface IProductService {
Mono<ProductDTO> saveProduct(ProductDTO productDTO);
Mono<ProductDTO> getProduct(String id);
}
package com.nisum.example.service;
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.stereotype.Service;
import reactor.core.publisher.Mono;
/**
* Product Service class
*/
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
/**
*Perform save operation of one product
* @param productDTO
* @return Mono<ProductDTO>
*/
@Override
public Mono<ProductDTO> saveProduct(ProductDTO productDTO) {
log.info("Calling template save()...");
return reactiveMongoTemplate.save(productDTO);
}
/**
* Perform get operation based on id
* @param id
* @return Mono<ProductDTO>
*/
@Override
public Mono<ProductDTO> getProduct(String id) {
log.info("Calling service findById()...");
return reactiveMongoTemplate.findById(id,ProductDTO.class);
}
}
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