Skip to content

Commit 9475ccd

Browse files
committed
Added new enum as FUNDED
1 parent 04561a4 commit 9475ccd

10 files changed

Lines changed: 139 additions & 2 deletions

File tree

investmentservice/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@
9898
<artifactId>spring-security-test</artifactId>
9999
<scope>test</scope>
100100
</dependency>
101+
102+
<dependency>
103+
<groupId>org.springframework.boot</groupId>
104+
<artifactId>spring-boot-starter-webflux</artifactId>
105+
</dependency>
106+
101107
</dependencies>
102108

103109
<build>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.p2plending.investmentservice.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.reactive.function.client.WebClient;
6+
7+
@Configuration
8+
public class WebClientConfig {
9+
10+
@Bean
11+
public WebClient webClient() {
12+
return WebClient.builder()
13+
.baseUrl("http://p2p-lending-loan-service:8082") // Update if needed
14+
.build();
15+
}
16+
}

investmentservice/src/main/java/com/p2plending/investmentservice/controller/InvestmentController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class InvestmentController {
1818
private final InvestmentService investmentService;
1919

20-
@PostMapping
20+
@PostMapping("/create")
2121
public InvestmentResponse create(@RequestBody InvestmentRequest request) {
2222
Investment investment = investmentService.createInvestment(request);
2323
return toResponse(investment);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.p2plending.investmentservice.dto;
2+
3+
import com.p2plending.investmentservice.dto.LoanStatus;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
7+
import java.math.BigDecimal;
8+
import java.time.LocalDateTime;
9+
import java.util.UUID;
10+
11+
@Data
12+
@Builder
13+
public class LoanResponse {
14+
private UUID id;
15+
private UUID userId;
16+
private BigDecimal amount;
17+
private Integer tenure;
18+
private Double interestRate;
19+
private LoanStatus status;
20+
private LocalDateTime createdAt;
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.p2plending.investmentservice.dto;
2+
3+
public enum LoanStatus {
4+
PENDING,
5+
APPROVED,
6+
FUNDED,
7+
REJECTED,
8+
PAID
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.p2plending.investmentservice.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class UpdateLoanStatusRequest {
11+
private LoanStatus status;
12+
}

investmentservice/src/main/java/com/p2plending/investmentservice/service/serviceimpl/InvestmentServiceImpl.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.p2plending.investmentservice.service.serviceimpl;
22

33
import com.p2plending.investmentservice.dto.InvestmentRequest;
4+
import com.p2plending.investmentservice.dto.LoanResponse;
5+
import com.p2plending.investmentservice.dto.LoanStatus;
6+
import com.p2plending.investmentservice.dto.UpdateLoanStatusRequest;
47
import com.p2plending.investmentservice.model.Investment;
58
import com.p2plending.investmentservice.repository.InvestmentRepository;
69
import com.p2plending.investmentservice.service.InvestmentService;
710
import lombok.RequiredArgsConstructor;
11+
import org.springframework.http.MediaType;
812
import org.springframework.stereotype.Service;
13+
import org.springframework.web.reactive.function.client.WebClient;
914

15+
import java.math.BigDecimal;
1016
import java.util.List;
1117
import java.util.UUID;
1218

@@ -18,7 +24,33 @@ public class InvestmentServiceImpl implements InvestmentService {
1824

1925
@Override
2026
public Investment createInvestment(InvestmentRequest request) {
21-
return null;
27+
if (request.getAmount() <= 0) {
28+
throw new IllegalArgumentException("Investment amount must be greater than zero.");
29+
}
30+
31+
LoanResponse loan = fetchLoanDetails(request.getLoanId());
32+
33+
if (loan == null || loan.getStatus() != LoanStatus.APPROVED) {
34+
throw new IllegalArgumentException("Loan not available for investment.");
35+
}
36+
37+
if (BigDecimal.valueOf(request.getAmount()).compareTo(loan.getAmount()) != 0) {
38+
throw new IllegalArgumentException("Investment must match loan amount exactly.");
39+
}
40+
// Create and save investment
41+
Investment investment = Investment.builder()
42+
.userId(request.getUserId())
43+
.loanId(request.getLoanId())
44+
.amount(request.getAmount())
45+
.investedAt(java.time.LocalDateTime.now())
46+
.build();
47+
48+
Investment savedInvestment = investmentRepository.save(investment);
49+
50+
// TODO: Update loan's funded amount in Loan Service
51+
updateLoanStatusToFunded(request.getLoanId());
52+
53+
return savedInvestment;
2254
}
2355

2456
@Override
@@ -30,4 +62,35 @@ public List<Investment> getInvestmentsByUserId(UUID userId) {
3062
public List<Investment> getInvestmentsByLoanId(UUID loanId) {
3163
return investmentRepository.findByLoanId(loanId);
3264
}
65+
66+
private final WebClient webClient = WebClient.create("http://loanservice-service:8082");
67+
68+
private LoanResponse fetchLoanDetails(UUID loanId) {
69+
try {
70+
return webClient.get()
71+
.uri("/api/v1/loans/" + loanId)
72+
.retrieve()
73+
.bodyToMono(LoanResponse.class)
74+
.block();
75+
} catch (Exception e) {
76+
throw new RuntimeException("Failed to fetch loan details from Loan Service.", e);
77+
}
78+
}
79+
80+
private void updateLoanStatusToFunded(UUID loanId) {
81+
try {
82+
83+
UpdateLoanStatusRequest request = new UpdateLoanStatusRequest(LoanStatus.FUNDED);
84+
85+
webClient.put()
86+
.uri("/api/v1/loans/" + loanId + "/status")
87+
.contentType(MediaType.APPLICATION_JSON)
88+
.bodyValue(request)
89+
.retrieve()
90+
.bodyToMono(Void.class)
91+
.block();
92+
} catch (Exception e) {
93+
throw new RuntimeException("Failed to update loan status to FUNDED in Loan Service.", e);
94+
}
95+
}
3396
}

loanservice/src/main/java/com/p2plending/loanservice/controller/LoanController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public LoanResponse rejectLoan(@PathVariable("id") UUID id) {
5252
return mapToResponse(loan);
5353
}
5454

55+
@PostMapping("/{id}/fund")
56+
public LoanResponse fundLoan(@PathVariable("id") UUID id) {
57+
Loan loan = loanService.fundLoan(id);
58+
return mapToResponse(loan);
59+
}
60+
61+
5562
@PostMapping("/{id}/pay")
5663
public LoanResponse payLoan(@PathVariable("id") UUID id) {
5764
Loan loan = loanService.payLoan(id);

loanservice/src/main/java/com/p2plending/loanservice/model/LoanStatus.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public enum LoanStatus {
44
PENDING,
55
APPROVED,
6+
FUNDED,
67
REJECTED,
78
PAID
89
}

loanservice/src/main/java/com/p2plending/loanservice/service/LoanService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ public interface LoanService {
1919

2020
Loan rejectLoan(UUID loanId);
2121

22+
Loan fundLoan(UUID loanId);
23+
2224
Loan payLoan(UUID loanId);
2325
}

0 commit comments

Comments
 (0)