Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Payment Service entities and commands #25

Merged
merged 30 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d1c5de9
Intialize payments service and postgreSQL database
AhmedNasserG Mar 24, 2024
15785c1
Added wallet model entity, repository, service and controller
AhmedNasserG Mar 24, 2024
18a5dfa
Added payment request model entity, repository, service, and controller
AhmedNasserG Mar 24, 2024
56afa98
Merge branch 'main' into payments-add-wallet-model
AhmedNasserG Mar 24, 2024
521d0f7
Added `GetWallet` request and response
AhmedNasserG Mar 4, 2024
5133540
Added walletTransaction model entity, repository, service and controller
Abdullah204 Mar 24, 2024
4edf922
completed wallet withdraw method by adding transaction to wallettrans…
Abdullah204 Mar 24, 2024
7fe047b
Changed transactiontype to enum and removed useless check
Abdullah204 Mar 24, 2024
6dccc85
Merge branch 'main' into payments-add-wallet-model
Abdulaziz-Hassan Apr 19, 2024
4c80838
Add Command pattern setup
Abdulaziz-Hassan Apr 19, 2024
f036054
Add RabbitMQ setup
Abdulaziz-Hassan Apr 19, 2024
ac83e16
Add PaymentRequest commands
Abdulaziz-Hassan Apr 19, 2024
e6eca6d
Add PaymentTransaction commands
Abdulaziz-Hassan Apr 19, 2024
122225d
Add Wallet commands
Abdulaziz-Hassan Apr 19, 2024
cf44528
Add WalletTransaction commands
Abdulaziz-Hassan Apr 19, 2024
cce2483
Delete docker compose and use the main one in the top hierarchy
Abdulaziz-Hassan Apr 19, 2024
9996f9c
Fix WithdrawFromWallet command
Abdulaziz-Hassan Apr 19, 2024
20e99a7
Fix Wallet and WalletTransaction models
Abdulaziz-Hassan Apr 19, 2024
c2f57b1
Fix Custom Queries in PaymentTransaction and WalletTransaction reposi…
Abdulaziz-Hassan Apr 21, 2024
357b53b
Merge branch 'main' into payments-add-wallet-model
Abdulaziz-Hassan Apr 21, 2024
f534fc1
Fix Command Response return arguments to using HTTP status codes
Abdulaziz-Hassan Apr 21, 2024
0e24bc8
Add linting to payments service
AhmedNasserG Apr 21, 2024
94d5697
fix payments pom.xml file
AhmedNasserG Apr 21, 2024
f81fbe0
clean payments code
AhmedNasserG Apr 21, 2024
1eaa72c
fix linting issues
AhmedNasserG Apr 21, 2024
b14162e
fix linting issues
AhmedNasserG Apr 21, 2024
e946744
Update shared/src/main/java/com/workup/shared/commands/payments/dto/P…
Abdulaziz-Hassan Apr 21, 2024
1adeec3
Update shared/src/main/java/com/workup/shared/commands/payments/dto/W…
Abdulaziz-Hassan Apr 21, 2024
c2a3be0
Add some validation to some commands
Abdulaziz-Hassan Apr 21, 2024
a39aa76
Fix linting issues
Abdulaziz-Hassan Apr 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ services:
networks:
- microservices
- jobs

# ----- PAYMENTS MICROSERVICE -------
service_payments:
build: ./services/payments
depends_on:
payments_db:
condition: service_healthy
service_mq:
condition: service_started
networks:
- microservices
- payments

jobs_db:
image: cassandra:4.0.7
Expand All @@ -42,8 +54,19 @@ services:
ports:
- "9042:9042"

payments_db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: payments_password
POSTGRES_USER: payments_user
POSTGRES_DB: payments_database
ports:
- "5432:5432"

networks:
microservices:
driver: bridge
jobs:
driver: bridge
payments:
driver: bridge
11 changes: 0 additions & 11 deletions services/payments/docker-compose.yml

This file was deleted.

47 changes: 47 additions & 0 deletions services/payments/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<description>payments</description>
<properties>
<java.version>21</java.version>
<plugin.prettier.goal>write</plugin.prettier.goal>
</properties>
<dependencies>
<dependency>
Expand All @@ -32,6 +33,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
Expand Down Expand Up @@ -76,7 +81,49 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>0.22</version>
<configuration>
<prettierJavaVersion>2.0.0</prettierJavaVersion>
<printWidth>90</printWidth>
<tabWidth>2</tabWidth>
<useTabs>false</useTabs>
<ignoreConfigFile>true</ignoreConfigFile>
<ignoreEditorConfig>true</ignoreEditorConfig>
<!-- Use <inputGlobs> to override the default input patterns -->
<inputGlobs>
<!-- These are the default patterns, you can omit <inputGlobs> entirely unless you want to override them -->
<inputGlob>src/main/java/**/*.java</inputGlob>
<inputGlob>src/test/java/**/*.java</inputGlob>
</inputGlobs>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>${plugin.prettier.goal}</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>github_action</id>
<activation>
<property>
<name>env.GITHUB_ACTIONS</name>
</property>
</activation>
<properties>
<!-- But in our CI environment we want to validate that code is formatted -->
<plugin.prettier.goal>check</plugin.prettier.goal>
</properties>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package com.workup.payments;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class PaymentsApplication {

public static void main(String[] args) {
SpringApplication.run(PaymentsApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(PaymentsApplication.class, args);
}

@Bean
public ApplicationRunner runner(AmqpTemplate template) {
return args -> {
// TODO: Test each command
};
}

@Bean
public Queue myQueue() {
return new Queue("paymentsqueue");
}

@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.workup.payments;

import com.workup.payments.commands.PaymentCommandMap;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RabbitListener(queues = "paymentsqueue")
public class RabbitMQListener {

@Autowired
public PaymentCommandMap commandMap;
// TODO: Implement receive method for each command

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.workup.payments.commands;

import com.workup.payments.repositories.PaymentRequestRepository;
import com.workup.payments.repositories.PaymentTransactionRepository;
import com.workup.payments.repositories.WalletRepository;
import com.workup.payments.repositories.WalletTransactionRepository;
import com.workup.shared.commands.Command;
import com.workup.shared.commands.CommandRequest;
import com.workup.shared.commands.CommandResponse;
import lombok.Data;

@Data
public abstract class PaymentCommand<T extends CommandRequest, Q extends CommandResponse>
implements Command<T, Q> {

private PaymentRequestRepository paymentRequestRepository;
private PaymentTransactionRepository paymentTransactionRepository;
private WalletRepository walletRepository;
private WalletTransactionRepository walletTransactionRepository;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.workup.payments.commands;

import com.workup.payments.commands.paymentrequest.*;
import com.workup.payments.commands.paymenttransaction.GetClientPaymentTransactionsCommand;
import com.workup.payments.commands.paymenttransaction.GetFreelancerPaymentTransactionsCommand;
import com.workup.payments.commands.wallet.CreateWalletCommand;
import com.workup.payments.commands.wallet.GetWalletCommand;
import com.workup.payments.commands.wallettransaction.CreateWalletTransactionCommand;
import com.workup.payments.commands.wallettransaction.GetWalletTransactionCommand;
import com.workup.payments.commands.wallettransaction.GetWalletTransactionsCommand;
import com.workup.payments.commands.wallettransaction.WithdrawFromWalletCommand;
import com.workup.payments.repositories.PaymentRequestRepository;
import com.workup.payments.repositories.PaymentTransactionRepository;
import com.workup.payments.repositories.WalletRepository;
import com.workup.payments.repositories.WalletTransactionRepository;
import com.workup.shared.commands.CommandMap;
import com.workup.shared.commands.CommandRequest;
import com.workup.shared.commands.CommandResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PaymentCommandMap
extends CommandMap<PaymentCommand<? extends CommandRequest, ? extends CommandResponse>> {

@Autowired
private PaymentRequestRepository paymentRequestRepository;

@Autowired
private PaymentTransactionRepository paymentTransactionRepository;

@Autowired
private WalletRepository walletRepository;

@Autowired
private WalletTransactionRepository walletTransactionRepository;

public void registerCommands() {
/* PaymentRequest commands */
commands.put("CreatePaymentRequest", CreatePaymentRequestCommand.class);
commands.put("GetClientPaymentRequests", GetClientPaymentRequestsCommand.class);
commands.put(
"GetFreelancerPaymentRequests",
GetFreelancerPaymentRequestsCommand.class
);
commands.put("GetPaymentRequest", GetPaymentRequestCommand.class);
commands.put("PayPaymentRequest", PayPaymentRequestCommand.class);

/* PaymentTransaction commands */
commands.put(
"GetClientPaymentTransactions",
GetClientPaymentTransactionsCommand.class
);
commands.put(
"GetFreelancerPaymentTransactions",
GetFreelancerPaymentTransactionsCommand.class
);

/* Wallet commands */
commands.put("CreateWallet", CreateWalletCommand.class);
commands.put("GetWallet", GetWalletCommand.class);

/* WalletTransaction commands */
commands.put("CreateWalletTransaction", CreateWalletTransactionCommand.class);
commands.put("GetWalletTransaction", GetWalletTransactionCommand.class);
commands.put("GetWalletTransactions", GetWalletTransactionsCommand.class);
commands.put("WithdrawFromWallet", WithdrawFromWalletCommand.class);
}

@Override
public void setupCommand(
PaymentCommand<? extends CommandRequest, ? extends CommandResponse> command
) {
command.setPaymentRequestRepository(paymentRequestRepository);
command.setPaymentTransactionRepository(paymentTransactionRepository);
command.setWalletRepository(walletRepository);
command.setWalletTransactionRepository(walletTransactionRepository);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.workup.payments.commands.paymentrequest;

import com.workup.payments.commands.PaymentCommand;
import com.workup.payments.models.PaymentRequest;
import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
import com.workup.shared.commands.payments.paymentrequest.responses.CreatePaymentRequestResponse;
import com.workup.shared.enums.HttpStatusCode;

public class CreatePaymentRequestCommand
extends PaymentCommand<CreatePaymentRequestRequest, CreatePaymentRequestResponse> {

@Override
public CreatePaymentRequestResponse Run(CreatePaymentRequestRequest request) {
PaymentRequest paymentRequest = PaymentRequest
.builder()
.withClientId(request.getClientId())
.withFreelancerId(request.getFreelancerId())
.withAmount(request.getAmount())
.withDescription(request.getDescription())
.build();
try {
PaymentRequest savedPaymentRequest = getPaymentRequestRepository()
.save(paymentRequest);

System.out.println("[x] Payment request created : " + savedPaymentRequest);

return CreatePaymentRequestResponse
.builder()
.withStatusCode(HttpStatusCode.CREATED)
.withPaymentRequestId(savedPaymentRequest.getId())
.build();
} catch (Exception e) {
System.out.println("[x] Payment request creation failed : " + e.getMessage());

return CreatePaymentRequestResponse
.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while creating payment request")
.withPaymentRequestId(null)
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.workup.payments.commands.paymentrequest;

import com.workup.payments.commands.PaymentCommand;
import com.workup.payments.mapper.PaymentRequestMapper;
import com.workup.payments.models.PaymentRequest;
import com.workup.shared.commands.payments.dto.PaymentRequestDTO;
import com.workup.shared.commands.payments.paymentrequest.requests.GetClientPaymentRequestsRequest;
import com.workup.shared.commands.payments.paymentrequest.responses.GetClientPaymentRequestsResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.util.List;

public class GetClientPaymentRequestsCommand
extends PaymentCommand<GetClientPaymentRequestsRequest, GetClientPaymentRequestsResponse> {

@Override
public GetClientPaymentRequestsResponse Run(GetClientPaymentRequestsRequest request) {
List<PaymentRequest> savedRequests = getPaymentRequestRepository()
.findAllByClientId(request.getClientId());

List<PaymentRequestDTO> paymentRequestDTOS = PaymentRequestMapper.mapToPaymentRequestDTOs(
savedRequests
);

System.out.println("[x] Payment requests fetched : " + paymentRequestDTOS);

return GetClientPaymentRequestsResponse
.builder()
.withStatusCode(HttpStatusCode.OK)
.withRequests(paymentRequestDTOS)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.workup.payments.commands.paymentrequest;

import com.workup.payments.commands.PaymentCommand;
import com.workup.payments.mapper.PaymentRequestMapper;
import com.workup.payments.models.PaymentRequest;
import com.workup.shared.commands.payments.dto.PaymentRequestDTO;
import com.workup.shared.commands.payments.paymentrequest.requests.GetFreelancerPaymentRequestsRequest;
import com.workup.shared.commands.payments.paymentrequest.responses.GetFreelancerPaymentRequestsResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.util.List;

public class GetFreelancerPaymentRequestsCommand
extends PaymentCommand<GetFreelancerPaymentRequestsRequest, GetFreelancerPaymentRequestsResponse> {

@Override
public GetFreelancerPaymentRequestsResponse Run(
GetFreelancerPaymentRequestsRequest request
) {
List<PaymentRequest> savedRequests = getPaymentRequestRepository()
.findAllByFreelancerId(request.getFreelancerId());

List<PaymentRequestDTO> paymentRequestDTOS = PaymentRequestMapper.mapToPaymentRequestDTOs(
savedRequests
);

System.out.println("[x] Payment requests fetched : " + paymentRequestDTOS);

return GetFreelancerPaymentRequestsResponse
.builder()
.withStatusCode(HttpStatusCode.OK)
.withRequests(paymentRequestDTOS)
.build();
}
}
Loading
Loading