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
10 changes: 6 additions & 4 deletions src/main/java/runner/controllers/AccountController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import runner.entities.Transaction;
import runner.services.AccountServices;
import runner.views.Views;

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

Expand Down Expand Up @@ -56,10 +58,10 @@ public ResponseEntity<Account> create(@RequestBody Account account) throws Excep
// }

//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
14 changes: 8 additions & 6 deletions src/main/java/runner/controllers/CustomerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
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
26 changes: 18 additions & 8 deletions src/main/java/runner/services/AccountServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +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 @@ -82,14 +86,20 @@ public Account getAccountByAccountNumber(String accountNumber){
return accountRepo.findAccountByAccountNumber(accountNumber);
}


public Boolean removeAccount(String encryptedUrl){
if(accountRepo.findAccountByEncryptedUrl(encryptedUrl).getBalance()==0) {
accountRepo.deleteAccountByEncryptedUrl(encryptedUrl);
return true;
}
return false;
}
// 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{
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/runner/services/CustomerServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import runner.repositories.CustomerRepo;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.*;
import java.util.logging.Level;
Expand Down Expand Up @@ -153,4 +155,20 @@ public Set<Account> getAllAccounts(Long id) {
return null;
}

@Transactional
public Customer removeAccount(String username, String encryptedUrl) throws Exception{
Customer customer = customerRepo.findCustomerByLoginUsername(username);
List<Account> testAcct= customer.getAccounts().stream()
.filter(account -> account.getEncryptedUrl().equals(encryptedUrl)).collect(Collectors.toList());
if(testAcct.get(0).getBalance()==0) {

Set<Account> myAccts = customer.getAccounts().stream()
.filter(account -> !account.getEncryptedUrl().equals(encryptedUrl)).collect(Collectors.toSet());
customer.getAccounts().clear();
customer.getAccounts().addAll(myAccts);
return customerRepo.save(customer);
}
throw new Exception("Balance is not 0");
}

}
2 changes: 1 addition & 1 deletion src/main/resources/db/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
--

INSERT INTO `account` VALUES (1,'1234567890','CHECKING',999999999999,'2004-01-22',NULL,.2,'091000022',1);
# INSERT INTO `account` VALUES (3,'1111111111','CHECKING',10000,'2004-01-22',NULL,.2,'091000022',2),(4,'4444444444','SAVINGS',25000,'2004-01-22',NULL,.85,'091000022',2);
INSERT INTO `account` VALUES (2,'1111111111','CHECKING',0,'2004-01-22',NULL,.2,'091000022',1),(3,'4444444444','SAVINGS',25000,'2004-01-22',NULL,.85,'091000022',1);
# INSERT INTO `account` VALUES (5,'2222222222','CHECKING',0,'2004-01-22',NULL,.2,'091000022',3);
--
-- Dumping data for table `login`
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/runner/controllers/AccountControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ public void readAccountByUrlTest() throws Exception {
public void createAccountTest(){
}

@WithMockUser
@Test
public void deleteAccountTest() throws Exception {
Mockito.when(accountServices.removeAccount(any())).thenReturn(true);

mockMvc.perform(delete("/myaccount/{encryptedUrl}/delete","12345"))
.andExpect(status().isOk());
}
// @WithMockUser
// @Test
// public void deleteAccountTest() throws Exception {
// Mockito.when(accountServices.removeAccount(any())).thenReturn(true);
//
// mockMvc.perform(delete("/myaccount/{encryptedUrl}/delete","12345"))
// .andExpect(status().isOk());
// }

@WithMockUser
@Test
Expand Down
Loading