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

divido los features para q funcionen los tests #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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);
}

}
35 changes: 34 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,28 @@ 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;
}

public Double getPromo() {
return promo;
}

public void setPromo(Double promo) {
this.promo = promo;
}

}
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,
}
55 changes: 55 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,52 @@ 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));

if (sum >= 2000 && account.getPromo() > 0){
Double promoApplied = Math.min(sum * 0.1, account.getPromo());
account.setBalance(account.getBalance() + promoApplied);
account.setPromo(account.getPromo() - promoApplied);
account.addTransaction(new Transaction(promoApplied, TransactionType.PROMO));
}

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");
}
}

}
20 changes: 1 addition & 19 deletions src/test/resources/cucumber/bank_account_operations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,4 @@ Feature: Bank account operations
Given Account with a balance of 200
When Trying to deposit -100
Then Operation should be denied due to negative sum
And Account balance should remain 200


Feature: Bank account promo, get 10% extra in your $2000+ deposits, up to $500

Scenario: Successfully promo applied, cap not reached.
Given Account with a balance of 0
When Trying to deposit 2000
Then Account balance should be 2200

Scenario: Successfully promo applied, cap reached.
Given Account with a balance of 0
When Trying to deposit 6000
Then Account balance should be 6500

Scenario: Promo not applied
Given Account with a balance of 0
When Trying to deposit 1500
Then Account balance should be 1500
And Account balance should remain 200
16 changes: 16 additions & 0 deletions src/test/resources/cucumber/bank_account_promos.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: Bank account promo, get 10% extra in your $2000+ deposits, up to $500

Scenario: Successfully promo applied, cap not reached.
Given Account with a balance of 0
When Trying to deposit 2000
Then Account balance should be 2200

Scenario: Successfully promo applied, cap reached.
Given Account with a balance of 0
When Trying to deposit 6000
Then Account balance should be 6500

Scenario: Promo not applied
Given Account with a balance of 0
When Trying to deposit 1500
Then Account balance should be 1500