forked from ZipCodeCore/FinalGroupProjects-Java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAccountController.java
More file actions
79 lines (66 loc) · 3.44 KB
/
AccountController.java
File metadata and controls
79 lines (66 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import runner.entities.Account;
import runner.entities.Transaction;
import runner.services.AccountServices;
import runner.views.Views;
import java.util.Optional;
import java.util.Set;
@RequestMapping("/myaccount")
@RestController
public class AccountController {
@Autowired
private AccountServices accountServices;
/**
* This controller is used only for JWT testing purposes
* */
@GetMapping(value = "/test")
public String testJWT() {
return "Hello World";
}
//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<>(accountServices.getAllAccounts(currentPrincipalName), 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
@PostMapping(value = "/create")
public ResponseEntity<Account> create(@RequestBody Account account) {
return new ResponseEntity<>(accountServices.createAccount(account), 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);
}
//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")
public ResponseEntity<Account> updateAccountDeposit(@RequestBody Transaction transaction, @PathVariable String encryptedUrl) throws Exception {
return new ResponseEntity<>(accountServices.deposit(transaction,encryptedUrl), HttpStatus.OK);
}
@PutMapping(value = "/{encryptedUrl}/withdraw")
public ResponseEntity<Account> updateAccountWithdraw(@RequestBody Transaction transaction, @PathVariable String encryptedUrl) throws Exception {
return new ResponseEntity<>(accountServices.withdraw(transaction,encryptedUrl), HttpStatus.OK);
}
//same method as withdraw since JSON payload is same, only front end is different
@PutMapping(value = "/<encryptedUrl}/transfer")
public ResponseEntity<Account> updateAccountTransfer(@RequestBody Transaction transaction, @PathVariable String encryptedUrl) throws Exception {
return new ResponseEntity<>(accountServices.withdraw(transaction,encryptedUrl), HttpStatus.OK);
}
}