Skip to content
Merged
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
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ Use Spring Initializr to create an starting point for you application
### Money Management Application

#### User Stories to Fulfill
* As a client,
* can create new user account (Customer)
* can create new account for a customer
* As a logged in client,
* can create new client
* can create new account for a client
* deposit money to each account
* withdraw money from each account
* transfer money to and from any 2 accounts
* delete the client if accounts with Balance = 0
* display all account belonging to client
* display account details and transaction details of loaded account


### Money Management Application

#### User Stories to Fulfill
* As a client, (not logged in) I
* can create new accounts
* deposit money to each account
* withdraw money from each account
* transfer money to and from any 2 accounts
* can create new customer profile
* login

23 changes: 12 additions & 11 deletions src/main/java/runner/controllers/AccountController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import runner.services.AccountServices;
import runner.views.Views;

import javax.transaction.Transactional;
import java.util.Optional;
import java.util.Set;


@CrossOrigin(origins = "*", allowedHeaders = "*")
@RequestMapping("/myaccount")
@RestController
public class AccountController {
Expand Down Expand Up @@ -49,18 +50,18 @@ public ResponseEntity<Account> create(@RequestBody Account account) throws Excep
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
return new ResponseEntity<>(accountServices.createAccount(account, currentPrincipalName), HttpStatus.CREATED);
}

//REMOVE if not needed
@PutMapping(value = "/update/{id}")
public ResponseEntity<Optional<Account>> update(@RequestBody Account account, @PathVariable Long id) throws Exception {
return new ResponseEntity<>(accountServices.updateAccount(id,account), HttpStatus.OK);
}
//
// //REMOVE if not needed
// @PutMapping(value = "/update/{id}")
// public ResponseEntity<Optional<Account>> update(@RequestBody Account account, @PathVariable Long id) throws Exception {
// return new ResponseEntity<>(accountServices.updateAccount(id,account), HttpStatus.OK);
// }

//This needs to be rewritten with "encryptedUrl/delete", need to doublecheck if deleting account deletes User due to cascade.ALL
@DeleteMapping(value = "/{encryptedUrl}/delete")
public ResponseEntity<Boolean> deleteById(@PathVariable String encryptedUrl){
return new ResponseEntity<>(accountServices.removeAccount(encryptedUrl), HttpStatus.OK);
}
// @DeleteMapping(value = "/{encryptedUrl}/delete")
// public ResponseEntity<Boolean> deleteAccount(@PathVariable String encryptedUrl){
// return new ResponseEntity<>(accountServices.removeAccount(encryptedUrl), HttpStatus.OK);
// }

@JsonView(Views.AccountSpecific.class)
@PutMapping(value = "/{encryptedUrl}/deposit")
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/runner/controllers/CustomerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import runner.services.CustomerServices;
import runner.views.Views;


@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
public class CustomerController {

Expand All @@ -20,13 +20,9 @@ public class CustomerController {

@JsonView(Views.Profile.class)
@GetMapping(value = "/myaccount/profile")
public ResponseEntity<?> getCustomer() {
public ResponseEntity<Customer> getCustomer() {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName(); //needs JWT token in header
Customer customer =customerServices.readCustomerByLogin(currentPrincipalName); //<< for testing on angular, need to change back to currentPrincipalName
if( customer == null)
return new ResponseEntity<>("Customer not found", HttpStatus.NOT_FOUND);
else
return new ResponseEntity<>(customer, HttpStatus.OK);
return new ResponseEntity<>(customerServices.readCustomerByLogin(currentPrincipalName), HttpStatus.OK);
}

@PostMapping(value = "/openaccount",consumes = MediaType.APPLICATION_JSON_VALUE)
Expand Down Expand Up @@ -83,4 +79,10 @@ else if(flag==2)
return new ResponseEntity<>("No accounts/user found", HttpStatus.NOT_FOUND);
}

@DeleteMapping(value = "myaccount/{encryptedUrl}/delete")
public ResponseEntity<Customer> deleteAccount(@PathVariable String encryptedUrl) throws Exception{
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
return new ResponseEntity<>(customerServices.removeAccount(currentPrincipalName,encryptedUrl), HttpStatus.OK);
}

}
29 changes: 14 additions & 15 deletions src/main/java/runner/controllers/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ public class LoginController {
private LoginServices loginServices;

//Remove if not needed
@GetMapping(value = "/read/{id}")
public ResponseEntity<Login> readById(@PathVariable Long id) throws Exception {
if(new ResponseEntity<>(loginServices.readLogin(id), HttpStatus.OK) == null) throw new Exception("Error , the user id is null") ;
else
return new ResponseEntity<>(loginServices.readLogin(id), HttpStatus.OK);
}
// @GetMapping(value = "/read/{id}")
// public ResponseEntity<Login> readById(@PathVariable Long id) throws Exception {
// if(new ResponseEntity<>(loginServices.readLogin(id), HttpStatus.OK) == null) throw new Exception("Error , the user id is null") ;
// else
// return new ResponseEntity<>(loginServices.readLogin(id), HttpStatus.OK);
// }

//Remove if not needed
@PostMapping(value = "/create")
public ResponseEntity<Login> create(@RequestBody Login login) {
Login loginResult =loginServices.createLogin(login);
if( loginResult == null)
return new ResponseEntity<>(null, HttpStatus.FORBIDDEN);
else
return new ResponseEntity<>(loginResult, HttpStatus.OK);
}
// @PostMapping(value = "/create")
// public ResponseEntity<Login> create(@RequestBody Login login) {
// Login loginResult =loginServices.createLogin(login);
// if( loginResult == null)
// return new ResponseEntity<>(null, HttpStatus.FORBIDDEN);
// else
// return new ResponseEntity<>(loginResult, HttpStatus.OK);
// }

//

}
3 changes: 1 addition & 2 deletions src/main/java/runner/entities/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Set;
import static javax.persistence.CascadeType.ALL;


@Entity
public class Customer {
@Id
Expand Down Expand Up @@ -53,7 +52,7 @@ public class Customer {
private Login login;

@JsonView(Views.Profile.class) //delete in production
@OneToMany(mappedBy = "customer", cascade = ALL,fetch = FetchType.EAGER)
@OneToMany(mappedBy = "customer", cascade = ALL,fetch = FetchType.EAGER, orphanRemoval = true)
@OrderBy
@JsonBackReference
private Set<Account> accounts;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/runner/entities/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ public boolean isEnabled() {
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}

}
3 changes: 2 additions & 1 deletion src/main/java/runner/repositories/AccountRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public interface AccountRepo extends CrudRepository<Account,Long> {
Account findAccountByEncryptedUrl(String encryptedUrl);
Account findAccountByAccountNumber(String accountNumber);
Set<Account> findAccountsByCustomer_LoginUsername (String login);
Account deleteAccountByEncryptedUrl(String encryptedUrl);
Integer deleteAccountByEncryptedUrl(String encryptedUrl);
Integer deleteByEncryptedUrl(String encryptedUrl);
}
3 changes: 1 addition & 2 deletions src/main/java/runner/security/config/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET","POST","PUT","DELETE")
.allowedHeaders("*")
//.allowedOrigins("http://localhost:4200"); //angular default port
.allowedOrigins("http://zip-bank.herokuapp.com"); //angular default port
}
};
Expand All @@ -106,8 +107,6 @@ public void addCorsMappings(CorsRegistry registry) {





/* @Override //creating own form for login
protected void configure(HttpSecurity http) throws Exception{
http
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/runner/security/utilities/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public String generateToken(UserDetails userDetails){

private String createToken(Map<String, Object> claims, String subject){
return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis()+1000*60*60*100))
.setExpiration(new Date(System.currentTimeMillis()+1000*60*30))
.signWith(SignatureAlgorithm.HS256, SECRET_KEY).compact();
}

Expand Down
100 changes: 36 additions & 64 deletions src/main/java/runner/services/AccountServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import runner.entities.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import runner.entities.Customer;
import runner.entities.Transaction;
import runner.repositories.AccountRepo;

import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Transactional
@Service
public class AccountServices {
private final static Logger loggerService = Logger.getLogger(AccountServices.class.getName());
Expand Down Expand Up @@ -83,57 +86,38 @@ public Account getAccountByAccountNumber(String accountNumber){
return accountRepo.findAccountByAccountNumber(accountNumber);
}

//Remove if not needed
/* public Account readAccount(Long id) throws Exception{
loggerService.log(Level.INFO, "Attempting to read customer's account # " + id);
if (accountRepo.existsById(id) == true) {
loggerService.log(Level.INFO, "The customer's account #" + id + "is being read'");
return accountRepo.findAccountById(id);
}
loggerService.log(Level.WARNING, "The customer is trying to read account # " + id + "that doe not exist");
throw new Exception("Account does not exist");
}*/

//Remove if not needed
/* public Boolean removeAccount(Long id) throws Exception{
loggerService.log(Level.INFO, "Attempting to remove customer's account # " + id);
if (accountRepo.findAccountById(id).getBalance() == 0) {
loggerService.log(Level.INFO, "The customer is removing the account # " + id);
Account accountFromDB = accountRepo.findAccountById(id);
accountRepo.delete(accountFromDB);
return accountRepo.existsById(id);
} else {

loggerService.log(Level.WARNING, "The customer had a balance greater than 0 and could not remove the account # " + id);
throw new Exception("Balance not 0 cannot be closed");
}
}*/

public Boolean removeAccount(String encryptedUrl){
if(accountRepo.findAccountByEncryptedUrl(encryptedUrl).getBalance()==0) {
accountRepo.deleteAccountByEncryptedUrl(encryptedUrl);
return true;
}
return false;
}

//REMOVE if not used
public Optional<Account> updateAccount(Long id, Account account) throws Exception{
loggerService.log(Level.INFO, "Attempting to update customer's account # " + id);
if (accountRepo.existsById(id) == true) {
loggerService.log(Level.INFO, "The customer is updating their account # " + id);
Account accountFromDB = accountRepo.findAccountById(id);
accountFromDB.setAccountType(account.getAccountType());
accountFromDB.setAccountNumber(account.getAccountNumber());
accountFromDB.setInterestRate(account.getInterestRate());
accountFromDB.setDateOfOpening(account.getDateOfOpening());
accountFromDB.setRoutingNumber(account.getRoutingNumber());
accountFromDB.setBalance(account.getBalance());
return Optional.of(accountFromDB);
}
loggerService.log(Level.WARNING, "The account # " + id + "does not exist to be updated");
throw new Exception("Account does not exist");
}
// public Boolean removeAccount(String encryptedUrl){
// Account testAcct= accountRepo.findAccountByEncryptedUrl(encryptedUrl);
// Double testBalance = testAcct.getBalance();
// Customer customer = testAcct.getCustomer();
// if(accountRepo.findAccountByEncryptedUrl(encryptedUrl).getBalance()==0) {
// Integer testRemoveInt = accountRepo.deleteAccountByEncryptedUrl(encryptedUrl);
// //Integer myAcct = accountRepo.deleteByEncryptedUrl(encryptedUrl);
// Set<Account> myAccts = customer.getAccounts();
// myAccts.remove(testAcct);
//
// return true;
// }
// return false;
// }

// //REMOVE if not used
// public Optional<Account> updateAccount(Long id, Account account) throws Exception{
// loggerService.log(Level.INFO, "Attempting to update customer's account # " + id);
// if (accountRepo.existsById(id) == true) {
// loggerService.log(Level.INFO, "The customer is updating their account # " + id);
// Account accountFromDB = accountRepo.findAccountById(id);
// accountFromDB.setAccountType(account.getAccountType());
// accountFromDB.setAccountNumber(account.getAccountNumber());
// accountFromDB.setInterestRate(account.getInterestRate());
// accountFromDB.setDateOfOpening(account.getDateOfOpening());
// accountFromDB.setRoutingNumber(account.getRoutingNumber());
// accountFromDB.setBalance(account.getBalance());
// return Optional.of(accountFromDB);
// }
// loggerService.log(Level.WARNING, "The account # " + id + "does not exist to be updated");
// throw new Exception("Account does not exist");
// }

//iterate through set to get accounts but should only be one at any time
public Account iteratorReturn(Iterator<Account> iterator){
Expand Down Expand Up @@ -211,18 +195,6 @@ public String generateRandomUrl() {
return randomString;
}

// Not needed, transfer and withdraw have same JSON payload; so use withdraw method
/* public Account transfer(Double amount, Long fromId, Long toId) throws Exception {
if (accountRepo.findAccountById(fromId).getBalance() > amount) {
loggerService.log(Level.INFO, "The customer is making a transfer");
accountRepo.findAccountById(fromId).setBalance(accountRepo.findAccountById(fromId).getBalance() - amount);
accountRepo.findAccountById(toId).setBalance(accountRepo.findAccountById(toId).getBalance() + amount);
accountRepo.save(readAccount(toId));
return accountRepo.save(readAccount(fromId));
} else {
loggerService.log(Level.WARNING, "The customer did not have sufficient funds to make the transfer");
throw new Exception("Insufficient funds");
}
}*/


}
Loading