Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
feature: shoptransmitter implementation
Browse files Browse the repository at this point in the history
implement the method createOrder and updateOrder
module order bind to eureka_server

linked #48
  • Loading branch information
MeloLawson committed Jan 7, 2024
1 parent ec84327 commit 4d25fff
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 35 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ To manage the microservices of the application, you need to first launch the mod
Then launch the module api_gateway and the module mediator.
You can have access to the Dashboard of the eureka_server at http://localhost:8080. You will see all the microservices.

Server port for the modules :
- EUREKA_SERVER : 8080
- API_GATEWAY : 9191
- MEDIATOR : 8081
- ACCOUNT : 8082
- BANK-DATABASE : 8083
- TRANSACTION : 8084
- PRODUCT : 8085
- SHOP-DATABASE : 8086
- ORDER : 8087

### For the endpoints documentation
You can access the documentation of each endpoints after launching at this address : http://<localhost:service port>/swagger-ui/index.html#. This page also allow you to try the different endpoints of the concerned service.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/bank_db
spring.datasource.username=bank_user
# postgresql database password
spring.datasource.password=bank_password

spring.application.name=BANK-DATABASE-SERVICE
#Tomcat server port
server.port=8083
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class WebClientConfig {
public WebClient webClient() {

return WebClient.builder()
.baseUrl("http://localhost:9000")// TODO change when connecting to API_GATEWAY
.baseUrl("http://localhost:8087")// TODO change when connecting to API_GATEWAY
.defaultCookie("cookie-name", "cookie-value")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ResponseEntity<String> newOrderAndPayment(@RequestBody ObjectHolder objec
if(mediatorResponse1.getCurrentOperationStep().equals(OperationStep.CREATE_ORDER)
&& mediatorResponse1.getOperationStatus().equals(OperationStatus.SUCCESS)){
orderId = mediatorResponse1.getItemId();

System.out.println("ID ORDER "+mediatorResponse1.getItemId());
//Send the payment info and the amount of the order
MediatorResponse mediatorResponse2 = mediatorService.notify(OperationStep.CREATE_TRANSACTION,objectHolder.getPaymentMethod(),objectHolder.getAmount(), objectHolder.getReceiver());
transactionId = mediatorResponse2.getItemId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public MediatorResponse notify(OperationStep operationStep, OrderStatus orderSta
}
}
//Step : update the order (when payment process is done (success or not))
TransmitterResponse<String> response = shopTransmitter.updateOrder(orderStatus, orderId);
TransmitterResponse<String> response = shopTransmitter.updateOrder(orderId, orderStatus);
if(response.getOperationStep().equals(OperationStep.UPDATE_ORDER)
&& response.getOperationStatus().equals(OperationStatus.SUCCESS) ){
message = Messages.format("order_update", orderId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ public TransmitterResponse<TransactionDto> createTransaction(PaymentMethodDto pa
response.setData(transaction);

//TODO communication with BankController
try{
Mono<ResponseEntity<String>> monoTransac = webClient.post()
.uri("/webclient")
.body(Mono.just("paymentMethod"), String.class)
.retrieve()
.toEntity(String.class);

monoTransac.subscribe(
responseEntity -> {
System.out.println("Status: " + responseEntity.getStatusCode());
System.out.println("Location URI: " + responseEntity.getHeaders().getLocation());
System.out.println("Message: " + responseEntity.getBody());
});
}catch (Exception e){
System.out.println("error");
}
// try{
// Mono<ResponseEntity<String>> monoTransac = webClient.post()
// .uri("/webclient")
// .body(Mono.just("paymentMethod"), String.class)
// .retrieve()
// .toEntity(String.class);
//
// monoTransac.subscribe(
// responseEntity -> {
// System.out.println("Status: " + responseEntity.getStatusCode());
// System.out.println("Location URI: " + responseEntity.getHeaders().getLocation());
// System.out.println("Message: " + responseEntity.getBody());
// });
// }catch (Exception e){
// System.out.println("error");
// }


return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,85 @@
import com.cashmanager.server.common.enumeration.OperationStatus;
import com.cashmanager.server.common.enumeration.OperationStep;
import com.cashmanager.server.common.enumeration.OrderStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.UUID;

@Component
public class ShopTransmitter {
private final WebClient webClient;

public ShopTransmitter(WebClient webClient) {
this.webClient = webClient;
}

/**
* Function that send the HttpRequest to the OrderController to create a new order
* @param cart list of products and quantity
* @return TransmitterResponse
*/
public TransmitterResponse<OrderDto> createOrder(CartDto cart){
//TODO à supprimer
OrderDto order = new OrderDto(LocalDateTime.now().toString(),cart);
order.setId(UUID.fromString("df6c25cc-b0f6-45e1-be8a-d6de6bf7e463"));
TransmitterResponse<OrderDto> response = new TransmitterResponse<>();
response.setData(order);
response.setOperationStep(OperationStep.CREATE_ORDER);
response.setOperationStatus(OperationStatus.SUCCESS);

//TODO communication with the ShopController
//Communication with the ShopModule
try{
Mono<ResponseEntity<OrderDto>> monoResponse = webClient.post()
.uri("/api/shop/orders")
.body(Mono.just(cart), CartDto.class)
.retrieve()
.toEntity(OrderDto.class);
ResponseEntity<OrderDto> responseEntity = monoResponse.block(Duration.of(1000, ChronoUnit.MILLIS));
assert responseEntity != null;
if(responseEntity.getStatusCode().is2xxSuccessful()){
response.setData(responseEntity.getBody());
response.setOperationStatus(OperationStatus.SUCCESS);
}else{
response.setMessage(Objects.requireNonNull(responseEntity.getBody()).toString());
response.setOperationStatus(OperationStatus.ERROR);
}
}catch (Exception e){
response.setOperationStatus(OperationStatus.ERROR);
response.setMessage(e.getMessage());
}
return response;
}

public TransmitterResponse<String> updateOrder(OrderStatus orderStatus, UUID orderId){
//TODO à supprimer
public TransmitterResponse<String> updateOrder(UUID orderId, OrderStatus orderStatus){
TransmitterResponse<String> response = new TransmitterResponse<>();
response.setOperationStep(OperationStep.UPDATE_ORDER);
response.setOperationStatus(OperationStatus.SUCCESS);

//Communication with the ShopModule
try {
Mono<ResponseEntity<String>> monoResponse = webClient.post()
.uri("/api/shop/orders/" + orderId)
.body(Mono.just(orderStatus), OrderStatus.class)
.retrieve()
.toEntity(String.class);

//TODO communication with the ShopController
ResponseEntity<String> responseEntity = monoResponse.block(Duration.of(1000, ChronoUnit.MILLIS));
assert responseEntity != null;
if(responseEntity.getStatusCode().is2xxSuccessful()){
response.setOperationStatus(OperationStatus.SUCCESS);
}else{
response.setOperationStatus(OperationStatus.ERROR);
response.setMessage(responseEntity.getBody());
}
}catch (Exception e){
response.setOperationStatus(OperationStatus.ERROR);
response.setMessage(e.getMessage());
}
return response;
}

public TransmitterResponse<String> reverseInventory(UUID orderId){
//TODO communication with the ShopController

//TODO communication with the ShopModule

return null;
}
Expand Down
16 changes: 16 additions & 0 deletions Server/shop/order/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
<description>order</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -54,6 +59,17 @@
<version>1.5.5.Final</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ResponseEntity<?> updateOrderStatus(@PathVariable UUID orderId, @RequestB
return new ResponseEntity<>(HttpStatus.OK);
}
else{
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
return new ResponseEntity<>("Order Not Found",HttpStatus.NOT_FOUND);
}
}
}
7 changes: 5 additions & 2 deletions Server/shop/order/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
spring.application.name=ORDER-SERVICE
server.port=8085
server.port=8087
spring.datasource.url=jdbc:postgresql://localhost:5432/shop_db
spring.datasource.username=shop_user
spring.datasource.password=shop_password
spring.datasource.password=shop_password

Config for binding to the eureka-server
eureka.client.service-url.defaultZone=http://localhost:8080/eureka
1 change: 1 addition & 0 deletions Server/shop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<modules>
<module>shop_database</module>
<module>product</module>
<module>order</module>
</modules>

<dependencies>
Expand Down

0 comments on commit 4d25fff

Please sign in to comment.