Commit 41ab3dda authored by Tejas Sharma's avatar Tejas Sharma

added inventory service to check if product and sufficient quantity is...

added inventory service to check if product and sufficient quantity is available before placing the order
parent 1daecc8a
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.nisum</groupId>
<artifactId>ordering-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>inventory-service</artifactId>
<packaging>jar</packaging>
<name>inventory-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-webflux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb-reactive -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package org.nisum;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class InventoryServiceApplication
{
public static void main( String[] args )
{
SpringApplication.run(InventoryServiceApplication.class);
}
}
package org.nisum.controller;
import org.nisum.model.Inventory;
import org.nisum.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/inventory")
public class InventoryController {
@Autowired
private InventoryService inventoryService;
@PostMapping("/create")
public Mono<Inventory> AddProductToInventory(@RequestBody Inventory inventory){
return inventoryService.createInventory(inventory);
}
@GetMapping("/check/{productId}/{quantity}")
public ResponseEntity<Boolean> checkProductAvailability(@PathVariable("productId") String productId, @PathVariable("quantity") int quantity){
return inventoryService.checkProductAvailability(productId,quantity);
}
}
package org.nisum.model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.time.LocalDateTime;
@Data
@Document(collection = "inventory")
public class Inventory {
@Id
private String id;
private String productId;
private boolean inStock;
private int quantity;
private LocalDateTime timeStamp;
}
package org.nisum.repository;
import org.nisum.model.Inventory;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface InventoryRepository extends ReactiveMongoRepository<Inventory,String> {
Optional<Inventory> findByProductId(String productId);
}
package org.nisum.service;
import org.nisum.model.Inventory;
import org.nisum.repository.InventoryRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.util.Optional;
@Service
public class InventoryService {
@Autowired
private InventoryRepository inventoryRepository;
public static final Logger log = LoggerFactory.getLogger(InventoryService.class);
public Mono<Inventory> createInventory(Inventory inventory) {
inventory.setTimeStamp(LocalDateTime.now());
inventory.setInStock(true);
return inventoryRepository.save(inventory).doOnSuccess(inventory1 -> {
log.info("product with product id: "+inventory1.getProductId() + " added successfully to the inventory");
});
}
public ResponseEntity<Boolean> checkProductAvailability(String productId, int requiredQuantity){
Inventory inventory = inventoryRepository.findByProductId(productId).orElse(null);
if(inventory != null){
if(inventory.getQuantity() >= requiredQuantity){
return ResponseEntity.ok(true);
}else{
return ResponseEntity.ok(false);
}
}else {
return ResponseEntity.ok(false);
}
}
}
server:
port: 8082
spring:
data:
mongodb:
uri: mongodb://localhost:27017/inventory_db
\ No newline at end of file
package org.nisum;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class InventoryServiceApplicationTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public InventoryServiceApplicationTest(String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( InventoryServiceApplicationTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
package org.nisum.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient.Builder webClientBuilder(){
return WebClient.builder();
}
}
...@@ -18,10 +18,15 @@ public class OrderController { ...@@ -18,10 +18,15 @@ public class OrderController {
} }
// @PostMapping("/createOrder") // @PostMapping("/createOrder")
// public Mono<Order> createOrder(@RequestBody Order order){ // public Mono<String> createOrder(@RequestBody Order order){
// return orderService.createOrder(order); // return orderService.createOrder(order);
// } // }
@PostMapping("/place")
public Mono<String> placeOrder(@RequestParam(name = "productId") String productId, @RequestParam(name = "quantity") int quantity){
return orderService.placeOrder(productId,quantity);
}
// @GetMapping("/{id}") // @GetMapping("/{id}")
// public Mono<Order> getOrderById(@PathVariable String id){ // public Mono<Order> getOrderById(@PathVariable String id){
// return orderService.getOrderById(id); // return orderService.getOrderById(id);
......
...@@ -14,6 +14,7 @@ import org.springframework.data.domain.Sort; ...@@ -14,6 +14,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
...@@ -27,26 +28,50 @@ public class OrderService { ...@@ -27,26 +28,50 @@ public class OrderService {
@Autowired @Autowired
private OrderRepository orderRepository; private OrderRepository orderRepository;
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
// private final KafkaTemplate<String,String> kafkaTemplate; // private final KafkaTemplate<String,String> kafkaTemplate;
private final WebClient webClient;
@Autowired
public OrderService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://localhost:8082/inventory").build();
}
// public Mono<Order> createOrder(Order order){ public Mono<String> placeOrder(String productId, int quantity){
// return checkProductAvailability(productId, quantity)
// if(order.getProductId() == null){ .flatMap(isAvailable -> {
// return Mono.error(new OrderNotFoundException("Order Id cannot be null")); if(isAvailable){
// } return createOrder(productId,quantity);
// order.setStatus("Placed"); }
// order.setTimeStamp(LocalDateTime.now()); return Mono.just("Product is out of stock or insufficient quantity.");
// return orderRepository.save(order).doOnSuccess( });
// order1 -> kafkaTemplate
// .send("order-status-topic", }
// "Order with product id: " + order1.getProductId()
// +" is placed successfully" )); private Mono<Boolean> checkProductAvailability(String productId, int quantity){
// return webClient.get()
// } .uri("/check/{productId}/{quantity}")
.retrieve()
.bodyToMono(Boolean.class);
}
private Mono<String> createOrder(String productId, int quantity){
Order order = new Order();
if(productId == null){
log.error("product id cannot be null");
}
order.setStatus("Placed");
order.setTimeStamp(LocalDateTime.now());
order.setProductId(productId);
order.setQuantity(quantity);
orderRepository.save(order).doOnSuccess(order1 -> {
log.info("order with order id: " + order1.getId() + " placed successfully");
});
return Mono.just("Order placed successfully for product " + order.getProductId());
}
public Flux<Order> getAllOrders(int page, int size) { public Flux<Order> getAllOrders(int page, int size) {
Pageable pageable = PageRequest.of(page,size); Pageable pageable = PageRequest.of(page,size);
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
<modules> <modules>
<module>notification-service</module> <module>notification-service</module>
<module>order-service</module> <module>order-service</module>
<module>inventory-service</module>
</modules> </modules>
<properties> <properties>
......
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