Skip to content

Commit

Permalink
Remove webflux
Browse files Browse the repository at this point in the history
  • Loading branch information
minhtranq-nashtechglobal committed Jul 18, 2024
1 parent da74260 commit c00cd6c
Show file tree
Hide file tree
Showing 55 changed files with 811 additions and 2,224 deletions.
4 changes: 0 additions & 4 deletions backoffice-bff/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions cart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion cart/src/main/java/com/yas/cart/CartApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.yas.cart.config.ServiceUrlConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(ServiceUrlConfig.class)
@SpringBootApplication
public class CartApplication {

public static void main(String[] args) {
Expand Down
15 changes: 15 additions & 0 deletions cart/src/main/java/com/yas/cart/config/RestClientConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yas.cart.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;

@Configuration
public class RestClientConfig {

@Bean
public RestClient restClient() {
return RestClient.builder().build();
}

}
17 changes: 0 additions & 17 deletions cart/src/main/java/com/yas/cart/config/WebClientConfig.java

This file was deleted.

2 changes: 1 addition & 1 deletion cart/src/main/java/com/yas/cart/service/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<CartGetDetailVm> getCartDetailByCustomerId(String customerId) {
.toList();
}

public CartGetDetailVm addToCart(List<CartItemVm> cartItemVms) {
public CartGetDetailVm addToCart(List<CartItemVm> cartItemVms) {
// Call API to check all products will be added to cart are existed
List<Long> productIds = cartItemVms.stream().map(CartItemVm::productId).toList();
List<ProductThumbnailVm> productThumbnailVmList = productService.getProducts(productIds);
Expand Down
19 changes: 8 additions & 11 deletions cart/src/main/java/com/yas/cart/service/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@
import java.net.URI;
import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;

import com.yas.cart.config.ServiceUrlConfig;
import com.yas.cart.viewmodel.ProductThumbnailVm;

@Service
@RequiredArgsConstructor
public class ProductService {
private final WebClient webClient;
private final RestClient restClient;
private final ServiceUrlConfig serviceUrlConfig;

public ProductService(WebClient webClient, ServiceUrlConfig serviceUrlConfig) {
this.webClient = webClient;
this.serviceUrlConfig = serviceUrlConfig;
}

public List<ProductThumbnailVm> getProducts(List<Long> ids) {
final URI url = UriComponentsBuilder
.fromHttpUrl(serviceUrlConfig.product())
.path("/storefront/products/list-featured")
.queryParam("productId", ids)
.build()
.toUri();
return webClient.get()
return restClient.get()
.uri(url)
.retrieve()
.bodyToFlux(ProductThumbnailVm.class)
.collectList()
.block();
.toEntity(new ParameterizedTypeReference<List<ProductThumbnailVm>>(){})
.getBody();
}
}
4 changes: 0 additions & 4 deletions customer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yas.customer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;

@Configuration
public class RestClientConfig {

@Bean
public RestClient restClient() {
return RestClient.builder().build();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import com.yas.customer.viewmodel.address.AddressDetailVm;
import com.yas.customer.viewmodel.address.AddressVm;
import com.yas.customer.viewmodel.address.AddressPostVm;
import java.nio.charset.StandardCharsets;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;
import com.yas.customer.exception.NotFoundException;
import com.yas.customer.exception.AccessDeniedException;
Expand All @@ -19,14 +22,11 @@
import java.util.List;

@Service
@RequiredArgsConstructor
public class LocationService {
private final WebClient webClient;
private final ServiceUrlConfig serviceUrlConfig;

public LocationService(WebClient webClient, ServiceUrlConfig serviceUrlConfig) {
this.webClient = webClient;
this.serviceUrlConfig = serviceUrlConfig;
}
private final RestClient restClient;
private final ServiceUrlConfig serviceUrlConfig;

public List<AddressDetailVm> getAddressesByIdList(List<Long> ids) {
final String jwt = ((Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getTokenValue();
Expand All @@ -37,15 +37,17 @@ public List<AddressDetailVm> getAddressesByIdList(List<Long> ids) {
.buildAndExpand()
.toUri();

return webClient.get()
return restClient.get()
.uri(url)
.headers(h->h.setBearerAuth(jwt))
.retrieve()
.onStatus(
HttpStatus.UNAUTHORIZED::equals,
response -> response.bodyToMono(String.class).map(AccessDeniedException::new))
.bodyToMono(new ParameterizedTypeReference<List<AddressDetailVm>>() {})
.block();
(request, response) -> {
String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
throw new AccessDeniedException(body);
})
.body(new ParameterizedTypeReference<List<AddressDetailVm>>() {});
}

public AddressDetailVm getAddressById(Long id) {
Expand All @@ -56,15 +58,17 @@ public AddressDetailVm getAddressById(Long id) {
.buildAndExpand(id)
.toUri();

return webClient.get()
return restClient.get()
.uri(url)
.headers(h->h.setBearerAuth(jwt))
.retrieve()
.onStatus(
HttpStatus.NOT_FOUND::equals,
response -> response.bodyToMono(String.class).map(NotFoundException::new))
.bodyToMono(AddressDetailVm.class)
.block();
HttpStatus.NOT_FOUND::equals,
(request, response) -> {
String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
throw new AccessDeniedException(body);
})
.body(AddressDetailVm.class);
}

public AddressVm createAddress(AddressPostVm addressPostVm) {
Expand All @@ -75,24 +79,35 @@ public AddressVm createAddress(AddressPostVm addressPostVm) {
.buildAndExpand()
.toUri();

return webClient.post()
return restClient.post()
.uri(url)
.headers(h->h.setBearerAuth(jwt))
.bodyValue(addressPostVm)
.body(addressPostVm)
.retrieve()
.onStatus(
HttpStatus.UNAUTHORIZED::equals,
response -> response.bodyToMono(String.class).map(AccessDeniedException::new))
HttpStatus.UNAUTHORIZED::equals,
(request, response) -> {
String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
throw new AccessDeniedException(body);
})
.onStatus(
HttpStatus.FORBIDDEN::equals,
response -> response.bodyToMono(String.class).map(AccessDeniedException::new))
HttpStatus.FORBIDDEN::equals,
(request, response) -> {
String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
throw new AccessDeniedException(body);
})
.onStatus(
HttpStatus.BAD_REQUEST::equals,
response -> response.bodyToMono(String.class).map(NotFoundException::new))
HttpStatus.BAD_REQUEST::equals,
(request, response) -> {
String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
throw new NotFoundException(body);
})
.onStatus(
HttpStatus.NOT_FOUND::equals,
response -> response.bodyToMono(String.class).map(NotFoundException::new))
.bodyToMono(AddressVm.class)
.block();
HttpStatus.NOT_FOUND::equals,
(request, response) -> {
String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
throw new NotFoundException(body);
})
.body(AddressVm.class);
}
}
Loading

0 comments on commit c00cd6c

Please sign in to comment.