diff --git a/src/main/java/com/aninfo/Memo1BankApp.java b/src/main/java/com/aninfo/Memo1BankApp.java index 6a85624b..64811d63 100644 --- a/src/main/java/com/aninfo/Memo1BankApp.java +++ b/src/main/java/com/aninfo/Memo1BankApp.java @@ -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; @@ -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); @@ -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 getAccounts() { return accountService.getAccounts(); } + @GetMapping("/accounts/{cbu}/Transactions") + public Collection getTransactions(@PathVariable Long cbu) { + return transactionService.getTransactions(cbu); + } + + @GetMapping("/accounts/Transactions/{id}") + public Optional getTransaction(@PathVariable Long id) { + return transactionService.findById(id); + } + @GetMapping("/accounts/{cbu}") public ResponseEntity getAccount(@PathVariable Long cbu) { Optional accountOptional = accountService.findById(cbu); @@ -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); @@ -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(); } -} +} \ No newline at end of file diff --git a/src/main/java/com/aninfo/model/Transaction.java b/src/main/java/com/aninfo/model/Transaction.java new file mode 100644 index 00000000..1f923d05 --- /dev/null +++ b/src/main/java/com/aninfo/model/Transaction.java @@ -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; + } +} diff --git a/src/main/java/com/aninfo/repository/TransactionRepository.java b/src/main/java/com/aninfo/repository/TransactionRepository.java new file mode 100644 index 00000000..2de2bf70 --- /dev/null +++ b/src/main/java/com/aninfo/repository/TransactionRepository.java @@ -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 findTransactionById(Long id); + + @Override + List findAll(); +} diff --git a/src/main/java/com/aninfo/service/AccountService.java b/src/main/java/com/aninfo/service/AccountService.java index 0a5f1411..e75d5831 100644 --- a/src/main/java/com/aninfo/service/AccountService.java +++ b/src/main/java/com/aninfo/service/AccountService.java @@ -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); diff --git a/src/main/java/com/aninfo/service/TransactionService.java b/src/main/java/com/aninfo/service/TransactionService.java new file mode 100644 index 00000000..dcdd8ea7 --- /dev/null +++ b/src/main/java/com/aninfo/service/TransactionService.java @@ -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 getTransactions(Long cbu) { + return transactionRepository.findAll().stream() + .filter(x -> x.getCBU() == cbu) + .collect(Collectors.toList()); + } + + public Optional findById(Long id) { + return transactionRepository.findById(id); + } + + public void deleteById(Long transactionId) { + transactionRepository.deleteById(transactionId); + } + +} diff --git a/src/test/java/com/aninfo/integration/cucumber/AccountIntegrationServiceTest.java b/src/test/java/com/aninfo/integration/cucumber/AccountIntegrationServiceTest.java index 2583adc1..31b12cc6 100644 --- a/src/test/java/com/aninfo/integration/cucumber/AccountIntegrationServiceTest.java +++ b/src/test/java/com/aninfo/integration/cucumber/AccountIntegrationServiceTest.java @@ -23,6 +23,7 @@ Account withdraw(Account account, Double sum) { } Account deposit(Account account, Double sum) { + return accountService.deposit(account.getCbu(), sum); } diff --git a/src/test/resources/cucumber/bank_account_operations.feature b/src/test/resources/cucumber/bank_account_operations.feature index c71060ce..98a856be 100644 --- a/src/test/resources/cucumber/bank_account_operations.feature +++ b/src/test/resources/cucumber/bank_account_operations.feature @@ -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 @@ -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 +