Commit 93388320 authored by Tejas Sharma's avatar Tejas Sharma

replaced String.class with Product object in parallel and sequential req res...

replaced String.class with Product object in parallel and sequential req res mothods in service and controller layer
parent 5e67d4ed
......@@ -9,6 +9,6 @@ public class WebClientConfig {
@Bean
public WebClient.Builder webClientBuilder(){
return WebClient.builder();
return WebClient.builder().baseUrl("");
}
}
......@@ -2,6 +2,7 @@ package org.nisum.controller;
import org.nisum.dto.InventoryDTO;
import org.nisum.model.Inventory;
import org.nisum.model.Product;
import org.nisum.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
......@@ -22,7 +23,7 @@ public class InventoryController {
private InventoryService inventoryService;
@Autowired
private MessageSource messageSource;
// http://localhost:8082/inventory/create
@PostMapping("/create")
public Mono<ResponseEntity<String>> AddProductToInventory(@RequestBody InventoryDTO inventoryDTO, Locale locale){
return inventoryService.createInventory(inventoryDTO)
......@@ -34,28 +35,25 @@ public class InventoryController {
}
// http://localhost:8082/inventory/check
@GetMapping("/check/{productId}/{quantity}")
public ResponseEntity<Boolean> checkProductAvailability(@PathVariable("productId") String productId, @PathVariable("quantity") int quantity){
return inventoryService.checkProductAvailability(productId,quantity);
}
// http://localhost:8082/inventory/allRecords
// http://localhost:8082/inventory/allRecords
@GetMapping("/allRecords")
public Flux<InventoryDTO> getAllInventories(){
return inventoryService.getAllInventories();
}
// http://localhost:8082/inventory/sequential
@GetMapping("/sequential")
public Mono<List<String>> getSequentialInventoryDetails() {
public Mono<List<Product>> getSequentialInventoryDetails() {
return inventoryService.getSequentialInventoryDetails();
}
@GetMapping("/sequential1")
public Mono<List<String>> getSequentialInventoryDetails1() {
return inventoryService.getSequentialInventoryDetails();
}
// http://localhost:8082/inventory/parallel
@GetMapping("/parallel")
public Mono<List<String>> getParallelInventoryDetails() {
public Mono<List<Product>> getParallelInventoryDetails() {
return inventoryService.getParallelInventoryDetails();
}
......
package org.nisum.model;
import lombok.Data;
@Data
public class Dimensions {
private double width;
private double height;
private double depth;
// Getters and Setters for dimensions fields
}
\ No newline at end of file
package org.nisum.model;
import lombok.Data;
@Data
public class Meta {
private String createdAt;
private String updatedAt;
private String barcode;
private String qrCode;
}
\ No newline at end of file
package org.nisum.model;
import lombok.Data;
import java.util.List;
@Data
public class Product {
private int id;
private String title;
private String description;
private String category;
private double price;
private double discountPercentage;
private double rating;
private int stock;
private List<String> tags;
private String brand;
private String sku;
private int weight;
private Dimensions dimensions;
private String warrantyInformation;
private String shippingInformation;
private String availabilityStatus;
private List<Review> reviews;
private String returnPolicy;
private int minimumOrderQuantity;
private Meta meta;
private List<String> images;
private String thumbnail;
}
package org.nisum.model;
import lombok.Data;
@Data
public class Review {
private int rating;
private String comment;
private String date;
private String reviewerName;
private String reviewerEmail;
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package org.nisum.service;
import org.nisum.dto.InventoryDTO;
import org.nisum.mapper.InventoryMapper;
import org.nisum.model.Inventory;
import org.nisum.model.Product;
import org.nisum.repository.InventoryRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -15,7 +16,6 @@ import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Service
public class InventoryService {
......@@ -27,6 +27,7 @@ public class InventoryService {
@Autowired
private WebClient.Builder webClientBuilder;
Boolean status;
public static final Logger log = LoggerFactory.getLogger(InventoryService.class);
......@@ -87,38 +88,40 @@ public class InventoryService {
}
//sequential request-response model
public Mono<List<String>> getSequentialInventoryDetails(){
// instead of string make it return object for both parallel and sequential
// instead of hardcoding the base url create a property in yml and pass it in web client config file
public Mono<List<Product>> getSequentialInventoryDetails(){
return webClientBuilder.baseUrl("https://dummyjson.com")
.build()
.get()
.uri("/products/1")
.retrieve()
.bodyToMono(String.class)
.bodyToMono(Product.class)
.flatMap(s ->
webClientBuilder.baseUrl("https://dummyjson.com")
.build()
.get()
.uri("/products/2")
.retrieve()
.bodyToMono(String.class)
.bodyToMono(Product.class)
.map(s1 -> List.of(s,s1)));
}
//parallel request response model
public Mono<List<String>> getParallelInventoryDetails(){
Mono<String> product1 =webClientBuilder.baseUrl("https://dummyjson.com")
public Mono<List<Product>> getParallelInventoryDetails(){
Mono<Product> product1 =webClientBuilder.baseUrl("https://dummyjson.com")
.build()
.get()
.uri("/products/1")
.retrieve()
.bodyToMono(String.class);
Mono<String> product2 =webClientBuilder.baseUrl("https://dummyjson.com")
.bodyToMono(Product.class);
Mono<Product> product2 =webClientBuilder.baseUrl("https://dummyjson.com")
.build()
.get()
.uri("/products/2")
.retrieve()
.bodyToMono(String.class);
.bodyToMono(Product.class);
return Mono.zip(product1,product2)
.map(objects -> List.of(objects.getT1(),objects.getT2()));
......
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