Commit d373c0f8 authored by sgandhi's avatar sgandhi

endOfTransaction end point created.

parent cfcc382f
package com.nisum.offertransactionservice;
import com.nisum.offertransactionservice.dao.OfferLookupRepo;
import com.nisum.offertransactionservice.model.OfferLookup;
import com.safeway.epe.exception.ExceptionResponseHandlerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
......@@ -16,7 +12,6 @@ public class OffertransactionserviceApplication {
SpringApplication.run(OffertransactionserviceApplication.class, args);
}
@Bean
public ExceptionResponseHandlerImpl getExceptionService(){
return new ExceptionResponseHandlerImpl();
......
package com.nisum.offertransactionservice.controller;
import com.nisum.offertransactionservice.model.EndOfSaleReq;
import com.nisum.offertransactionservice.model.EndOfSaleEntity;
import com.nisum.offertransactionservice.model.OfferTransactionRequest;
import com.nisum.offertransactionservice.model.OfferTransactionResponse;
import com.nisum.offertransactionservice.service.EndOfSaleService;
import com.nisum.offertransactionservice.service.EndOfTransactionService;
import com.nisum.offertransactionservice.service.OfferCallingPEService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -26,6 +26,9 @@ public class OfferTransactionController {
@Autowired
private EndOfSaleService endOfSaleService;
@Autowired
private EndOfTransactionService endOfTransactionService;
@PostMapping("offerTransactionCall")
public OfferTransactionResponse getOfferTransactionResponse(@Valid @RequestBody OfferTransactionRequest offerTransactionRequest) {
return offerCallingPEService.getDiscountedItemList(offerTransactionRequest);
......@@ -38,6 +41,13 @@ public class OfferTransactionController {
return new ResponseEntity<String>(uuid.toString(), HttpStatus.OK);
}
@PostMapping("/endOfTransaction")
public ResponseEntity<String> endOfTransaction(@Valid @RequestBody String uuid) {
String response = endOfTransactionService.endOfTransaction(uuid);
return new ResponseEntity(response, HttpStatus.OK);
}
}
......
package com.nisum.offertransactionservice.service;
import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import javax.annotation.PostConstruct;
import java.util.Objects;
import static com.nisum.offertransactionservice.util.ExceptionUtil.handleError;
@Service
@PropertySource("classpath:application.properties")
@Slf4j
public class EndOfTransactionService {
private WebClient webClient;
@Value("${store.consumer.url}")
private String storeConsumerUrl;
@Value("${store.consumer.baseUrl}")
private String baseUrl;
@PostConstruct
public void init() {
webClient = WebClient.builder().baseUrl(baseUrl).
defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).
filter(logRequest()).
filter(logResponse()).
build();
}
private ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
log.info("Store Consumer Request {}", clientRequest);
return Mono.just(clientRequest);
});
}
private ExchangeFilterFunction logResponse() {
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
log.info("Store Consumer Request {}", clientResponse);
return Mono.just(clientResponse);
});
}
public String endOfTransaction(String uuid) {
if (Objects.isNull(uuid) || uuid.isEmpty()) {
log.error("Offer transaction uuid is empty or null");
throw new GlobalApiGenericException("Unable to get the Offer transaction uuid ", new Exception("exception e"), "400", false);
}
log.info("Store Consumer WebClient call Start");
Mono<String> endOfTransactionRes = webClient.post().
uri(storeConsumerUrl).
accept(MediaType.APPLICATION_JSON).
contentType(MediaType.APPLICATION_JSON).
body(Mono.just(uuid), String.class).
retrieve().
onStatus(HttpStatus::is4xxClientError, clientResponse -> handleError(clientResponse)).
onStatus(HttpStatus::is5xxServerError, clientResponse -> handleError(clientResponse)).
bodyToMono(String.class);
log.debug("Store consumer WebClient call End");
return endOfTransactionRes.toString();
}
}
......@@ -6,29 +6,20 @@ import com.nisum.offertransactionservice.dao.OfferLookupRepo;
import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException;
import com.nisum.offertransactionservice.model.*;
import com.safeway.epe.model.Response;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
@Service
@PropertySource("classpath:application.properties")
@Slf4j
......@@ -64,53 +55,52 @@ public class OfferCallingPEService {
private ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
log.info("Promotional Request {}",clientRequest);
log.info("Promotional Request {}", clientRequest);
return Mono.just(clientRequest);
});
}
private ExchangeFilterFunction logResponse() {
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
log.info("Promotional Request {}",clientResponse);
log.info("Promotional Request {}", clientResponse);
return Mono.just(clientResponse);
});
}
public OfferTransactionResponse getDiscountedItemList(OfferTransactionRequest offerTransactionRequest) {
log.info("Inside getDiscountedItemList Method");
List<OfferLookup> eligibleOffer = new ArrayList();
offerLookupRepo.findAll().forEach(eligibleOffer::add);
if(eligibleOffer.isEmpty()){
if (eligibleOffer.isEmpty()) {
log.error("Offer lookup Object is empty");
throw new GlobalApiGenericException("Unable to get the Offer Response ",new Exception("exception e"),"400",false);
throw new GlobalApiGenericException("Unable to get the Offer Response ", new Exception("exception e"), "400", false);
}
log.debug("Offer lookup Object {}",offerLookupRepo);
log.debug("Offer lookup Object {}", offerLookupRepo);
OfferTransactionResponse offerTransactionResponse;
PERequest peRequest = offerConverter.apply(offerTransactionRequest);
peRequest.setEligibleOffers(eligibleOffer);
log.info("Promotional Engine WebClient call Start");
Flux<PEResponse> peResponseFlux = webClient.post().
uri(promotionEngineUrl).
accept(MediaType.APPLICATION_JSON).
contentType(MediaType.APPLICATION_JSON).
body(Mono.just(peRequest), PERequest.class).
retrieve().
onStatus(HttpStatus::is4xxClientError, clientResponse -> handleError(clientResponse)).
onStatus(HttpStatus::is5xxServerError, clientResponse -> handleError(clientResponse)).
bodyToFlux(PEResponse.class);
uri(promotionEngineUrl).
accept(MediaType.APPLICATION_JSON).
contentType(MediaType.APPLICATION_JSON).
body(Mono.just(peRequest), PERequest.class).
retrieve().
onStatus(HttpStatus::is4xxClientError, clientResponse -> handleError(clientResponse)).
onStatus(HttpStatus::is5xxServerError, clientResponse -> handleError(clientResponse)).
bodyToFlux(PEResponse.class);
log.debug("Promotional Engine WebClient call End");
offerTransactionResponse = peResponseToOfferTransactionResConverter.apply(peResponseFlux.toStream().findFirst().get());
log.debug("Offer Transaction Response {}",offerTransactionResponse);
log.debug("Offer Transaction Response {}", offerTransactionResponse);
return offerTransactionResponse;
}
private Mono<? extends Throwable> handleError(ClientResponse clientResponse)throws GlobalApiGenericException {
private Mono<? extends Throwable> handleError(ClientResponse clientResponse) throws GlobalApiGenericException {
final Mono<Response> responseMono = clientResponse.bodyToMono(Response.class);
return responseMono.flatMap(response -> {
log.error("Error Response While Calling PE Service {}",response);
throw new GlobalApiGenericException(response, response.getHttpStatusCode());
return responseMono.flatMap(response -> {
log.error("Error Response While Calling PE Service {}", response);
throw new GlobalApiGenericException(response, response.getHttpStatusCode());
});
}
}
package com.nisum.offertransactionservice.util;
import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException;
import com.safeway.epe.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.reactive.function.client.ClientResponse;
import reactor.core.publisher.Mono;
@Slf4j
public class ExceptionUtil {
public static Mono<? extends Throwable> handleError(ClientResponse clientResponse)throws GlobalApiGenericException {
final Mono<Response> responseMono = clientResponse.bodyToMono(Response.class);
return responseMono.flatMap(response -> {
log.error("Error Response While Calling Store Consumer Service {}",response);
throw new GlobalApiGenericException(response, response.getHttpStatusCode());
});
}
}
......@@ -3,3 +3,6 @@ spring.datasource.username=user
spring.datasource.password=password123
promotion.engine.calculate.discount.url=/promotionEngine/calculateDiscount
promotion.engine.baseUrl=http://localhost:8081
store.consumer.url=central/producer
//TODO: change the baseUrl post confirmation from Amar
store.consumer.baseUrl=http://localhost:8081
\ No newline at end of file
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