Skip to content

Commit

Permalink
arreglo las transacciones
Browse files Browse the repository at this point in the history
  • Loading branch information
ignaciano3 committed Oct 22, 2023
1 parent 1870a88 commit e50204d
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# memo1-bank-app
Memo1 - Backend API

Por favor para la proxima metan algo en el readme
22 changes: 21 additions & 1 deletion src/main/java/com/aninfo/Memo1BankApp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aninfo;

import com.aninfo.model.Account;
import com.aninfo.model.Transaction;
import com.aninfo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
Expand All @@ -10,6 +11,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import springfox.documentation.builders.PathSelectors;
Expand All @@ -33,7 +35,8 @@ public static void main(String[] args) {

@PostMapping("/accounts")
@ResponseStatus(HttpStatus.CREATED)
public Account createAccount(@RequestBody Account account) {
public Account createAccount(@RequestParam double balance) {
Account account = new Account(balance);
return accountService.createAccount(account);
}

Expand Down Expand Up @@ -65,6 +68,7 @@ public void deleteAccount(@PathVariable Long cbu) {
accountService.deleteById(cbu);
}

/* NO van a haber deposit ni withdraw de por si, solo transactions
@PutMapping("/accounts/{cbu}/withdraw")
public Account withdraw(@PathVariable Long cbu, @RequestParam Double sum) {
return accountService.withdraw(cbu, sum);
Expand All @@ -74,6 +78,22 @@ public Account withdraw(@PathVariable Long cbu, @RequestParam Double sum) {
public Account deposit(@PathVariable Long cbu, @RequestParam Double sum) {
return accountService.deposit(cbu, sum);
}
*/

@GetMapping("/transactions/{cbu}")
public ArrayList<Transaction> getTransactions(@PathVariable Long cbu) {
return accountService.getTransactions(cbu);
}

@GetMapping("/transactions/{cbu}/{transactionIndex}")
public Transaction getTransaction(@PathVariable Long cbu, @PathVariable int transactionIndex) {
return accountService.getTransaction(cbu, transactionIndex);
}

@PutMapping("/transactions/{cbu}")
public Account makeTransaction(@PathVariable Long cbu, @RequestBody Transaction transaction){
return accountService.makeTransaction(cbu, transaction);
}

@Bean
public Docket apiDocket() {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/aninfo/exceptions/AccountNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.aninfo.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class AccountNotFoundException extends RuntimeException {

public AccountNotFoundException(String message) {
super(message);
}

}
27 changes: 26 additions & 1 deletion src/main/java/com/aninfo/model/Account.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.aninfo.model;

import java.util.ArrayList;

import javax.persistence.*;

@Entity
Expand All @@ -9,7 +11,14 @@ public class Account {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long cbu;

private Double balance;
private Double balance; //Es un error grave usar doubles para balances

@Lob
private ArrayList<Transaction> transactions = new ArrayList<Transaction>();
// Si tuviera mas tiempo lo haria en otra tabla

private Double promo = 500.0;


public Account(){
}
Expand All @@ -34,4 +43,20 @@ public void setBalance(Double balance) {
this.balance = balance;
}

public void addTransaction(Transaction t) {
this.transactions.add(t);
}

public void deleteTransaction(int index) {
transactions.remove(index);
}

public Transaction getTransaction(int index) {
return transactions.get(index);
}

public ArrayList<Transaction> getTransactions() {
return transactions;
}

}
22 changes: 22 additions & 0 deletions src/main/java/com/aninfo/model/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.aninfo.model;

import java.io.Serializable;

public class Transaction implements Serializable {
private final TransactionType type;
private final Double amount;

public Transaction(Double amount, TransactionType type) {
this.amount = amount;
this.type = type;
}

public Double getAmount() {
return amount;
}

public TransactionType getType() {
return type;
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/aninfo/model/TransactionType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.aninfo.model;

public enum TransactionType {
DEPOSIT,
WITHDRAW,
PROMO,
}
47 changes: 47 additions & 0 deletions src/main/java/com/aninfo/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.aninfo.exceptions.DepositNegativeSumException;
import com.aninfo.exceptions.InsufficientFundsException;
import com.aninfo.exceptions.InvalidTransactionTypeException;
import com.aninfo.exceptions.AccountNotFoundException;
import com.aninfo.model.Account;
import com.aninfo.model.Transaction;
import com.aninfo.model.TransactionType;
import com.aninfo.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;

Expand All @@ -18,6 +24,9 @@ public class AccountService {
private AccountRepository accountRepository;

public Account createAccount(Account account) {
if (account.getBalance() > 0) {
account.addTransaction(new Transaction(account.getBalance(), TransactionType.DEPOSIT));
}
return accountRepository.save(account);
}

Expand All @@ -40,12 +49,16 @@ public void deleteById(Long cbu) {
@Transactional
public Account withdraw(Long cbu, Double sum) {
Account account = accountRepository.findAccountByCbu(cbu);
if (account == null) {
throw new AccountNotFoundException("Account not found");
}

if (account.getBalance() < sum) {
throw new InsufficientFundsException("Insufficient funds");
}

account.setBalance(account.getBalance() - sum);
account.addTransaction(new Transaction(sum, TransactionType.WITHDRAW));
accountRepository.save(account);

return account;
Expand All @@ -59,10 +72,44 @@ public Account deposit(Long cbu, Double sum) {
}

Account account = accountRepository.findAccountByCbu(cbu);
if (account == null) {
throw new AccountNotFoundException("Account not found");
}

account.setBalance(account.getBalance() + sum);
account.addTransaction(new Transaction(sum, TransactionType.DEPOSIT));
accountRepository.save(account);

return account;
}

public ArrayList<Transaction> getTransactions(Long cbu) {
Account account = accountRepository.findAccountByCbu(cbu);
if (account == null) {
throw new AccountNotFoundException("Account not found");
}

return account.getTransactions();
}

public Transaction getTransaction(Long cbu, int index) {
Account account = accountRepository.findAccountByCbu(cbu);
if (account == null) {
throw new AccountNotFoundException("Account not found");
}

return account.getTransaction(index);
}

@Transactional
public Account makeTransaction(Long cbu, Transaction t) {
if (t.getType() == TransactionType.DEPOSIT) {
return deposit(cbu, t.getAmount());
} else if (t.getType() == TransactionType.WITHDRAW) {
return withdraw(cbu, t.getAmount());
} else {
throw new InvalidTransactionTypeException("Invalid Transaction");
}
}

}

0 comments on commit e50204d

Please sign in to comment.