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
3 changes: 3 additions & 0 deletions .idea/FullStack.MicroWebApplication-Server.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 14 additions & 11 deletions src/main/java/runner/controllers/AccountController.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package runner.controllers;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -8,8 +9,11 @@
import runner.entities.Transaction;
import runner.services.AccountServices;
import runner.services.CustomerServices;
import runner.views.Views;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@RequestMapping("/myaccount")
@RestController
Expand All @@ -18,8 +22,6 @@ public class AccountController {
@Autowired
private AccountServices accountServices;

@Autowired
private CustomerServices customerServices;
/**
* This controller is used only for JWT testing purposes
* */
Expand All @@ -29,16 +31,17 @@ public String testJWT() {
}

//get accounts for the authenticated user only, THIS is the homepage once user has logged in
@JsonView(Views.AllAccounts.class)
@GetMapping
public ResponseEntity<Set<Account>> readAllAccount() {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
return new ResponseEntity<>(customerServices.getAllAccounts(currentPrincipalName), HttpStatus.OK);
return new ResponseEntity<>(accountServices.getAllAccounts(currentPrincipalName), HttpStatus.OK);
}

//REMOVE if not needed
@GetMapping(value = "/{id}")
public ResponseEntity<Account> readAccount(@PathVariable Long id) throws Exception {
return new ResponseEntity<>(accountServices.readAccount(id), HttpStatus.OK);
@JsonView(Views.AccountSpecific.class)
@GetMapping(value = "/{accountEncryptedUrl}")
public ResponseEntity<Account> readAccountById(@PathVariable String accountEncryptedUrl){
return new ResponseEntity<>(accountServices.findAccountByEncryptedUrl(accountEncryptedUrl), HttpStatus.OK);
}

//REMOVE if not needed
Expand All @@ -53,10 +56,10 @@ public ResponseEntity<Optional<Account>> update(@RequestBody Account account, @P
return new ResponseEntity<>(accountServices.updateAccount(id,account), HttpStatus.OK);
}

//This needs to be rewritten with "encryptedUrl/delete"
@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<Boolean> deleteById(@PathVariable Long id) throws Exception {
return new ResponseEntity<>(accountServices.removeAccount(id), 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);
}

@PutMapping(value = "/{encryptedUrl}/deposit")
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/runner/controllers/CustomerController.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package runner.controllers;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand All @@ -9,6 +10,7 @@
import runner.entities.Address;
import runner.entities.Customer;
import runner.services.CustomerServices;
import runner.views.Views;


import java.net.URI;
Expand All @@ -23,9 +25,9 @@ public class CustomerController {

private final static Logger logger = Logger.getLogger(CustomerController.class.getName());


@JsonView(Views.Profile.class)
@GetMapping
public ResponseEntity<?> readById() {
public ResponseEntity<?> getCustomer() {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Customer customer =customerServices.readCustomerByLogin(currentPrincipalName);
if( customer == null)
Expand Down Expand Up @@ -62,6 +64,7 @@ public ResponseEntity<Customer> update(@RequestBody Customer customer) throws Ex
return new ResponseEntity<>(customerServices.updateCustomer(id,customer), HttpStatus.OK);
}

@JsonView(Views.PhoneNumber.class)
@PutMapping(value = "/update/phone")
public ResponseEntity<?> updatePhone(@RequestBody String phoneNumber) throws Exception {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Expand All @@ -77,6 +80,7 @@ else if(response == 1 )

}

@JsonView(Views.Email.class)
@PutMapping(value = "/update/email")
public ResponseEntity<?> updateEmail(@RequestBody String email) throws Exception {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Expand All @@ -92,6 +96,7 @@ else if(response == 1 )

}

@JsonView(Views.Address.class)
@PutMapping(value = "/update/address")
public ResponseEntity<?> updateEmail(@RequestBody Address address) throws Exception {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Expand All @@ -103,6 +108,7 @@ public ResponseEntity<?> updateEmail(@RequestBody Address address) throws Except
else
return new ResponseEntity<>(customerServices.readCustomer(id), HttpStatus.OK);
}

@DeleteMapping(value = "/delete")
public ResponseEntity<?> deleteById() {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Expand All @@ -117,14 +123,4 @@ else if(flag==2)
return new ResponseEntity<>("No accounts/user found", HttpStatus.NOT_FOUND);
}

@GetMapping(value = "/accounts")
public ResponseEntity<?> getAllAccounts(){
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Customer customerReturned =customerServices.readCustomerByLogin(currentPrincipalName);
Long id = customerReturned.getId();
if(customerServices.getAllAccounts(id) == null)
return new ResponseEntity<>("Customer not found", HttpStatus.NOT_FOUND);
else
return new ResponseEntity<>(customerServices.getAllAccounts(id), HttpStatus.OK);
}
}
9 changes: 5 additions & 4 deletions src/main/java/runner/controllers/TestHomePageDeleteLater.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Set;
import java.util.stream.Collectors;

//@RequestMapping(value = "/myaccount")
@RestController
public class TestHomePageDeleteLater {
@Autowired
Expand All @@ -37,13 +36,15 @@ public ResponseEntity<Set<Account>> readAllAccount(){
return new ResponseEntity<>(customerServices.getAllAccounts(currentPrincipalName), HttpStatus.OK);
}*/

@GetMapping(value = "/{accountEncryptedUrl}")

//get account for specific encrypted URL: THIS METHOD HAS BEEN MOVED TO ACCOUNTCONTROLLER
/* @GetMapping(value = "/{accountEncryptedUrl}")
public ResponseEntity<Account> readAccountById(@PathVariable String accountEncryptedUrl) throws Exception {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Long accountId = customerServices.getAllAccounts(currentPrincipalName).stream()
Long accountId = accountServices.getAllAccounts(currentPrincipalName).stream()
.filter(a->a.getEncryptedUrl().equals(accountEncryptedUrl))
.collect(Collectors.toList()).get(0).getId();
return new ResponseEntity<>(accountServices.readAccount(accountId), HttpStatus.OK);
}
}*/

}
18 changes: 18 additions & 0 deletions src/main/java/runner/entities/Account.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package runner.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonView;
import runner.enums.AccountType;
import runner.views.Views;

import javax.persistence.*;
import java.time.LocalDate;

Expand All @@ -15,15 +18,30 @@ public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@JsonView(Views.AccountNumber.class)
private String accountNumber;

@JsonView(Views.AccountDetails.class)
private String routingNumber;

@JsonView(Views.AccountType.class)
@Enumerated(EnumType.STRING)
private AccountType accountType; //enum

@JsonView(Views.AccountActions.class)
private Double balance;

@JsonView(Views.AccountDetails.class)
private LocalDate dateOfOpening;

@JsonView(Views.AccountDetails.class)
private Double interestRate;

@JsonView(Views.AllAccounts.class) //delete this later in production
private String encryptedUrl;

@JsonView(Views.AccountSpecific.class)
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "account_transaction",
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/runner/entities/Customer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package runner.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonView;
import runner.views.Views;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.Set;
Expand All @@ -12,20 +15,31 @@ public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(nullable = false)
private String firstName;

@JsonView(Views.Profile.class)
private String middleName;

@Column(nullable = false)
private String lastName;

@Column(nullable = false)
private LocalDate dateOfBirth;

@Column(nullable = false)
private String socialSecurity;

@Column(nullable = false)
@JsonView(Views.Email.class)
private String email;

@JsonView(Views.PhoneNumber.class)
@Column(nullable = false)
private String phoneNumber;

@JsonView(Views.Address.class)
@OneToOne(cascade = ALL, fetch = FetchType.EAGER)
private Address address;
@JsonBackReference(value = "login")
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/runner/entities/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import runner.views.Views;

import javax.persistence.*;
import java.util.Collection;
Expand All @@ -15,8 +17,11 @@ public class Login implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@JsonView(Views.Profile.class)
@Column(nullable = false)
private String username;

@Column(nullable = false)
private String password;

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/runner/repositories/AccountRepo.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package runner.repositories;

import org.springframework.data.jpa.repository.Query;
import runner.entities.Account;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;


@Repository
public interface AccountRepo extends CrudRepository<Account,Long> {

Account findAccountById(Long Id);
Account findAccountByEncryptedUrl(String encryptedUrl);
Account findAccountByAccountNumber(String accountNumber);

Set<Account> findAccountsByCustomer_LoginUsername (String login);
Account deleteAccountByEncryptedUrl(String encryptedUrl);
}
4 changes: 2 additions & 2 deletions src/main/java/runner/security/config/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.ArrayList;
import java.util.List;

@Configuration //allows Spring to find and automatically apply the class to the global Web Security.
@EnableWebSecurity
@Configuration
@EnableWebSecurity //allows Spring to find and automatically apply the class to the global Web Security.
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import runner.entities.Login;
import runner.security.models.AuthenticationResponse;
import runner.security.utilities.JwtUtil;
import runner.services.AccountServices;
import runner.services.CustomerServices;
import runner.services.LoginServices;

Expand All @@ -29,7 +30,7 @@ public class AuthenticationController {
@Autowired
private LoginServices loginServices;
@Autowired
private CustomerServices customerServices;
private AccountServices accountServices;

//jwt authentication
@PostMapping(value = "/authenticate")
Expand All @@ -51,7 +52,7 @@ public ResponseEntity<?> generateAuthenticationToken(@RequestBody Login login) t

//adding the random URL to the accounts
public void addRandomUrlToAccounts(Login login){
Set<Account> accountSet = customerServices.getAllAccounts(login.getUsername());
Set<Account> accountSet = accountServices.getAllAccounts(login.getUsername());
accountSet.stream().forEach(a->a.setEncryptedUrl(generateRandomUrl()));
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/runner/services/AccountServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class AccountServices {
@Autowired
private TransactionServices transactionServices;

public Set<Account> getAllAccounts(String username){
return accountRepo.findAccountsByCustomer_LoginUsername(username);
}

public Account createAccount(Account account) {
loggerService.log(Level.INFO, "The customer's new account is being saved");
return accountRepo.save(account);
Expand Down Expand Up @@ -51,6 +55,14 @@ public Boolean removeAccount(Long id) throws Exception{
}
}

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

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) {
Expand Down
Loading