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

Actividad 4.8 #11

Open
wants to merge 4 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
35 changes: 30 additions & 5 deletions src/main/java/com/aninfo/Memo1BankApp.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.aninfo;

import com.aninfo.model.Account;
import com.aninfo.model.Transaction;
import com.aninfo.service.AccountService;
import com.aninfo.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -26,6 +28,8 @@ public class Memo1BankApp {

@Autowired
private AccountService accountService;
@Autowired
private TransactionService transactionService;

public static void main(String[] args) {
SpringApplication.run(Memo1BankApp.class, args);
Expand All @@ -37,11 +41,27 @@ public Account createAccount(@RequestBody Account account) {
return accountService.createAccount(account);
}

@PostMapping("/accounts/{cbu}/Transactions")
@ResponseStatus(HttpStatus.CREATED)
public Transaction createTransaction(@RequestBody Transaction transaction) {
return transactionService.createTransaction(transaction);
}

@GetMapping("/accounts")
public Collection<Account> getAccounts() {
return accountService.getAccounts();
}

@GetMapping("/accounts/{cbu}/Transactions")
public Collection<Transaction> getTransactions(@PathVariable Long cbu) {
return transactionService.getTransactions(cbu);
}

@GetMapping("/accounts/Transactions/{id}")
public Optional<Transaction> getTransaction(@PathVariable Long id) {
return transactionService.findById(id);
}

@GetMapping("/accounts/{cbu}")
public ResponseEntity<Account> getAccount(@PathVariable Long cbu) {
Optional<Account> accountOptional = accountService.findById(cbu);
Expand All @@ -65,6 +85,11 @@ public void deleteAccount(@PathVariable Long cbu) {
accountService.deleteById(cbu);
}

@DeleteMapping("/accounts/{cbu}/Transactions/{id}")
public void deleteTransacion(@PathVariable Long id) {
transactionService.deleteById(id);
}

@PutMapping("/accounts/{cbu}/withdraw")
public Account withdraw(@PathVariable Long cbu, @RequestParam Double sum) {
return accountService.withdraw(cbu, sum);
Expand All @@ -78,9 +103,9 @@ public Account deposit(@PathVariable Long cbu, @RequestParam Double sum) {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/aninfo/model/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.aninfo.model;

import javax.persistence.*;

@Entity
public class Transaction {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private Long cbu;

private Double amount;

public Transaction() {

}

public Transaction(Double amount, Long cbu, Long id) {
this.amount = amount;
this.cbu = cbu;
this.id = id;
}

public Double getAmount() { return this.amount; }

public Long getCBU() { return this.cbu; }

public Long getId() {
return this.id;
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/aninfo/repository/TransactionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.aninfo.repository;

import com.aninfo.model.Transaction;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource
public interface TransactionRepository extends CrudRepository<Transaction, Long> {

Transaction findTransactionById(Long id);

@Override
List<Transaction> findAll();
}
11 changes: 11 additions & 0 deletions src/main/java/com/aninfo/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public Account deposit(Long cbu, Double sum) {
throw new DepositNegativeSumException("Cannot deposit negative sums");
}

if(sum >= 2000){
double extraSum = (10 * sum) / 100;

if(extraSum >= 500){
sum += 500;
}
else{
sum += extraSum;
}
}

Account account = accountRepository.findAccountByCbu(cbu);
account.setBalance(account.getBalance() + sum);
accountRepository.save(account);
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/aninfo/service/TransactionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.aninfo.service;

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

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class TransactionService {

@Autowired
private TransactionRepository transactionRepository;
@Autowired
private AccountRepository accountRepository;

@Transactional
public Transaction createTransaction(Transaction transaction) {

Account account = accountRepository.findAccountByCbu(transaction.getCBU());

if ((transaction.getAmount() < 0) && (account.getBalance() - transaction.getAmount() < 0)) {
throw new InsufficientFundsException("Insufficient funds");
}

if (transaction.getAmount() == 0) {
throw new DepositNegativeSumException("Cannot deposit anything");
}

Double promo = Double.valueOf(0);

if (transaction.getAmount() >= 2000) {
promo = (10 * transaction.getAmount() / 100);

if (promo >= 500) {
promo = Double.valueOf(500);
}
}

Double newBalance = account.getBalance() + transaction.getAmount() + promo;

account.setBalance(newBalance);
accountRepository.save(account);

return transactionRepository.save(transaction);
}

public Collection<Transaction> getTransactions(Long cbu) {
return transactionRepository.findAll().stream()
.filter(x -> x.getCBU() == cbu)
.collect(Collectors.toList());
}

public Optional<Transaction> findById(Long id) {
return transactionRepository.findById(id);
}

public void deleteById(Long transactionId) {
transactionRepository.deleteById(transactionId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Account withdraw(Account account, Double sum) {
}

Account deposit(Account account, Double sum) {

return accountService.deposit(account.getCbu(), sum);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/cucumber/bank_account_operations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ Feature: Bank account operations
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
#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
Expand All @@ -40,3 +39,4 @@ Feature: Bank account promo, get 10% extra in your $2000+ deposits, up to $500
Given Account with a balance of 0
When Trying to deposit 1500
Then Account balance should be 1500