Skip to content
Snippets Groups Projects
Commit 798459b1 authored by Filip Fabry's avatar Filip Fabry
Browse files

Change types for currencies to BigDecimal

parent 5bcb258e
No related branches found
No related tags found
1 merge request!8Change types for currencies to BigDecimal
Showing with 57 additions and 47 deletions
package cz.muni.fi.obs.api;
import java.math.BigDecimal;
import lombok.Builder;
@Builder
public record CurrencyExchangeResult(
String symbolFrom,
String symbolTo,
long exchangedRate,
long sourceAmount,
long exchangedAmount) {
BigDecimal exchangedRate,
BigDecimal sourceAmount,
BigDecimal exchangedAmount) {
}
package cz.muni.fi.obs.api;
import java.math.BigDecimal;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import lombok.Builder;
......@@ -10,8 +12,8 @@ public record TransactionCreateDto(
String withdrawsFrom,
@NotBlank
String depositsTo,
@Min(0)long withdrawAmount,
@Min(0)long depositAmount,
@Min(0) BigDecimal withdrawAmount,
@Min(0) BigDecimal depositAmount,
String note,
String variableSymbol) {
}
......@@ -2,6 +2,8 @@ package cz.muni.fi.obs.controller;
import static cz.muni.fi.obs.controller.TransactionController.TRANSACTION_PATH;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
......@@ -47,7 +49,7 @@ public class TransactionController {
}
@GetMapping("/account/{accountId}/balance")
public ResponseEntity<Long> checkAccountBalance(@PathVariable("accountId") String accountId) {
public ResponseEntity<BigDecimal> checkAccountBalance(@PathVariable("accountId") String accountId) {
log.info("Checking account balance for account: {}", accountId);
return ResponseEntity.ok(facade.checkAccountBalance(accountId));
}
......
package cz.muni.fi.obs.data.dbo;
import java.math.BigDecimal;
import org.springframework.data.annotation.Id;
import lombok.Builder;
......@@ -7,12 +9,11 @@ import lombok.Builder;
@Builder
public record TransactionDbo(
@Id String id,
long conversionRate,
BigDecimal conversionRate,
String withdrawsFrom,
String depositsTo,
long withdrawAmount,
long depositAmount,
BigDecimal withdrawAmount,
BigDecimal depositAmount,
String note,
String variableSymbol,
String state) {
String variableSymbol) {
}
package cz.muni.fi.obs.data.repository;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.data.domain.Page;
......@@ -16,13 +17,12 @@ public class TransactionRepository {
return TransactionDbo.builder()
.id("1")
.depositsTo("1")
.conversionRate(1)
.conversionRate(BigDecimal.valueOf(1))
.note("note")
.withdrawAmount(1)
.withdrawAmount(BigDecimal.valueOf(1))
.withdrawsFrom("1")
.depositAmount(1)
.depositAmount(BigDecimal.valueOf(1))
.variableSymbol("123")
.state("state")
.build();
}
......@@ -31,24 +31,22 @@ public class TransactionRepository {
TransactionDbo.builder()
.id("1")
.depositsTo("1")
.conversionRate(1)
.conversionRate(BigDecimal.valueOf(1))
.note("note")
.withdrawAmount(1)
.withdrawAmount(BigDecimal.valueOf(1))
.withdrawsFrom("1")
.depositAmount(1)
.depositAmount(BigDecimal.valueOf(1))
.variableSymbol("123")
.state("state")
.build(),
TransactionDbo.builder()
.id("2")
.depositsTo("1")
.conversionRate(2)
.conversionRate(BigDecimal.valueOf(2))
.note("note")
.withdrawAmount(2)
.withdrawAmount(BigDecimal.valueOf(2))
.withdrawsFrom("2")
.depositAmount(2)
.depositAmount(BigDecimal.valueOf(2))
.variableSymbol("123")
.state("state")
.build()
);
}
......@@ -58,24 +56,22 @@ public class TransactionRepository {
TransactionDbo.builder()
.id("1")
.depositsTo("1")
.conversionRate(1)
.conversionRate(BigDecimal.valueOf(1))
.note("note")
.withdrawAmount(1)
.withdrawAmount(BigDecimal.valueOf(1))
.withdrawsFrom("1")
.depositAmount(1)
.depositAmount(BigDecimal.valueOf(1))
.variableSymbol("123")
.state("state")
.build(),
TransactionDbo.builder()
.id("2")
.depositsTo("1")
.conversionRate(2)
.conversionRate(BigDecimal.valueOf(2))
.note("note")
.withdrawAmount(2)
.withdrawAmount(BigDecimal.valueOf(2))
.withdrawsFrom("1")
.depositAmount(2)
.depositAmount(BigDecimal.valueOf(2))
.variableSymbol("123")
.state("state")
.build()
);
}
......@@ -84,24 +80,22 @@ public class TransactionRepository {
var transactions = List.of(
TransactionDbo.builder()
.id("1")
.conversionRate(1)
.conversionRate(BigDecimal.valueOf(1))
.note("note")
.withdrawAmount(1)
.withdrawAmount(BigDecimal.valueOf(1))
.withdrawsFrom("1")
.depositAmount(1)
.depositAmount(BigDecimal.valueOf(1))
.variableSymbol("123")
.state("state")
.build(),
TransactionDbo.builder()
.id("2")
.depositsTo("2")
.conversionRate(2)
.conversionRate(BigDecimal.valueOf(2))
.note("note")
.withdrawAmount(2)
.withdrawAmount(BigDecimal.valueOf(2))
.withdrawsFrom("2")
.depositAmount(2)
.depositAmount(BigDecimal.valueOf(2))
.variableSymbol("123")
.state("state")
.build()
);
......
package cz.muni.fi.obs.facade;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
......@@ -35,7 +37,7 @@ public class TransactionManagementFacade {
return transactionService.viewTransactionHistory(accountId, pageNumber, pageSize);
}
public long checkAccountBalance(String accountId) {
public BigDecimal checkAccountBalance(String accountId) {
return transactionService.checkAccountBalance(accountId);
}
......
package cz.muni.fi.obs.service;
import java.math.BigDecimal;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -23,14 +24,18 @@ public class TransactionService {
this.client = client;
}
public long checkAccountBalance(String accountId) {
public BigDecimal checkAccountBalance(String accountId) {
var withdraws = repository.getTransactionsByWithdrawId(accountId);
var deposits = repository.getTransactionsByDepositId(accountId);
long withdrawSum = withdraws.stream().mapToLong(TransactionDbo::withdrawAmount).sum();
long depositSum = deposits.stream().mapToLong(TransactionDbo::depositAmount).sum();
BigDecimal withdrawSum = withdraws.stream()
.map(TransactionDbo::withdrawAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal depositSum = deposits.stream()
.map(TransactionDbo::depositAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
return depositSum - withdrawSum;
return depositSum.subtract(withdrawSum);
}
public Page<TransactionDbo> viewTransactionHistory(String accountId, int pageNumber, int pageSize) {
......
package cz.muni.fi.obs.web;
import java.math.BigDecimal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -11,9 +13,9 @@ public class CurrencyServiceClient {
@Bean
public CurrencyExchangeResult getConversionRate() {
return CurrencyExchangeResult.builder()
.sourceAmount(100)
.exchangedAmount(4)
.exchangedRate(25)
.sourceAmount(BigDecimal.valueOf(100))
.exchangedAmount(BigDecimal.valueOf(4))
.exchangedRate(BigDecimal.valueOf(4))
.symbolFrom("CZK")
.symbolTo("EUR").build();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment