Skip to content

Commit c8eafe0

Browse files
committed
refactoring on be
1 parent 3fda257 commit c8eafe0

File tree

11 files changed

+77
-69
lines changed

11 files changed

+77
-69
lines changed

BankApplicationBackend/src/main/java/com/demo/bankapp/BankApplication.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ public class BankApplication {
1212
public static void main(String[] args) {
1313
SpringApplication.run(BankApplication.class, args);
1414
}
15+
16+
@Bean
17+
public PasswordEncoder passwordEncoder() {
18+
return new BCryptPasswordEncoder();
19+
}
20+
1521
}

BankApplicationBackend/src/main/java/com/demo/bankapp/configuration/security/SecurityConfig.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.context.annotation.Bean;
5-
import org.springframework.http.HttpMethod;
65
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
76
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
87
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
98
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
109
import org.springframework.security.config.http.SessionCreationPolicy;
11-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
10+
import org.springframework.security.core.userdetails.UserDetailsService;
1211
import org.springframework.security.crypto.password.PasswordEncoder;
1312
import org.springframework.web.cors.CorsConfiguration;
1413
import org.springframework.web.cors.CorsConfigurationSource;
1514
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
1615

17-
import com.demo.bankapp.service.concretions.UserService;
18-
1916
@EnableWebSecurity
2017
public class SecurityConfig extends WebSecurityConfigurerAdapter {
2118

22-
@Autowired
23-
private UserService userService;
19+
private UserDetailsService userService;
20+
private PasswordEncoder passwordEncoder;
2421

2522
@Autowired
26-
private PasswordEncoder passwordEncoder;
23+
public SecurityConfig(UserDetailsService userService, PasswordEncoder passwordEncoder) {
24+
this.userService = userService;
25+
this.passwordEncoder = passwordEncoder;
26+
}
2727

2828
@Override
2929
protected void configure(HttpSecurity http) throws Exception {
@@ -48,9 +48,4 @@ CorsConfigurationSource corsConfigurationSource() {
4848
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
4949
return source;
5050
}
51-
52-
@Bean
53-
public PasswordEncoder passwordEncoder() {
54-
return new BCryptPasswordEncoder();
55-
}
5651
}

BankApplicationBackend/src/main/java/com/demo/bankapp/controller/TransactionController.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.hateoas.Resource;
88
import org.springframework.http.MediaType;
9-
import org.springframework.web.bind.annotation.CrossOrigin;
109
import org.springframework.web.bind.annotation.PostMapping;
1110
import org.springframework.web.bind.annotation.RequestBody;
1211
import org.springframework.web.bind.annotation.RequestMapping;
@@ -19,25 +18,27 @@
1918
import com.demo.bankapp.model.User;
2019
import com.demo.bankapp.request.FindAllByUserRequest;
2120
import com.demo.bankapp.request.MakeTransactionRequest;
22-
import com.demo.bankapp.service.concretions.TransactionService;
23-
import com.demo.bankapp.service.concretions.UserService;
24-
import com.demo.bankapp.service.concretions.WealthService;
21+
import com.demo.bankapp.service.abstractions.ITransactionService;
22+
import com.demo.bankapp.service.abstractions.IUserService;
23+
import com.demo.bankapp.service.abstractions.IWealthService;
2524

2625
@RestController
2726
@RequestMapping(value = "/transaction", produces = { MediaType.APPLICATION_JSON_VALUE })
2827
public class TransactionController {
28+
29+
private ITransactionService transactionService;
30+
private IUserService userService;
31+
private IWealthService wealthService;
32+
33+
private TransactionResourceAssembler assembler;
2934

3035
@Autowired
31-
TransactionResourceAssembler assembler;
32-
33-
@Autowired
34-
TransactionService transactionService;
35-
36-
@Autowired
37-
UserService userService;
38-
39-
@Autowired
40-
WealthService userWealthService;
36+
public TransactionController(ITransactionService transactionService, IUserService userService, IWealthService wealthService, TransactionResourceAssembler assembler) {
37+
this.transactionService = transactionService;
38+
this.userService = userService;
39+
this.wealthService = wealthService;
40+
this.assembler = assembler;
41+
}
4142

4243
@PostMapping("/create")
4344
public Resource<Transaction> createTransaction(@RequestBody MakeTransactionRequest request) {
@@ -69,7 +70,7 @@ public Resource<Transaction> createTransaction(@RequestBody MakeTransactionReque
6970
throw new DailyOperationLimitReachedException();
7071
}
7172

72-
userWealthService.makeWealthExchange(user.getId(), request.getCurrency(), request.getAmount(), request.isBuying());
73+
wealthService.makeWealthExchange(user.getId(), request.getCurrency(), request.getAmount(), request.isBuying());
7374
Transaction transaction = transactionService.createNewTransaction(user.getId(), request.isBuying(), request.getCurrency(), request.getAmount());
7475

7576
return assembler.toResource(transaction);

BankApplicationBackend/src/main/java/com/demo/bankapp/controller/TransferController.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.hateoas.Resource;
1010
import org.springframework.http.MediaType;
11-
import org.springframework.web.bind.annotation.CrossOrigin;
1211
import org.springframework.web.bind.annotation.PostMapping;
1312
import org.springframework.web.bind.annotation.RequestBody;
1413
import org.springframework.web.bind.annotation.RequestMapping;
@@ -20,25 +19,27 @@
2019
import com.demo.bankapp.model.Transfer;
2120
import com.demo.bankapp.model.User;
2221
import com.demo.bankapp.request.MakeTransferRequest;
22+
import com.demo.bankapp.service.abstractions.ITransferService;
2323
import com.demo.bankapp.service.abstractions.IUserService;
2424
import com.demo.bankapp.service.abstractions.IWealthService;
25-
import com.demo.bankapp.service.concretions.TransferService;
2625

2726
@RestController
2827
@RequestMapping(value = "/transfer", produces = { MediaType.APPLICATION_JSON_VALUE })
2928
public class TransferController {
3029

31-
@Autowired
32-
IUserService userService;
33-
34-
@Autowired
35-
IWealthService wealthService;
30+
private IUserService userService;
31+
private IWealthService wealthService;
32+
private ITransferService transferService;
3633

37-
@Autowired
38-
TransferService transferService;
34+
private TransferResourceAssembler assembler;
3935

4036
@Autowired
41-
TransferResourceAssembler assembler;
37+
public TransferController(IUserService userService, IWealthService wealthService, ITransferService transferService, TransferResourceAssembler assembler) {
38+
this.userService = userService;
39+
this.wealthService = wealthService;
40+
this.transferService = transferService;
41+
this.assembler = assembler;
42+
}
4243

4344
@PostMapping("/create")
4445
public Resource<Transfer> createTransfer(@RequestBody MakeTransferRequest request) {
@@ -91,7 +92,6 @@ private BigDecimal getTryEquivalent(Map<String, Double> currencyRates, String cu
9192
}
9293

9394
private void checkDailyTransferLimitExceedition(Map<String, Double> currencyRates, List<Transfer> last24HourTransfers, BigDecimal transferTryEquivalent) {
94-
9595
BigDecimal dailyTransferLimit = new BigDecimal(100000);
9696

9797
BigDecimal rate;

BankApplicationBackend/src/main/java/com/demo/bankapp/controller/UserController.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.hateoas.Resource;
99
import org.springframework.http.MediaType;
10-
import org.springframework.web.bind.annotation.CrossOrigin;
1110
import org.springframework.web.bind.annotation.GetMapping;
1211
import org.springframework.web.bind.annotation.PostMapping;
1312
import org.springframework.web.bind.annotation.RequestBody;
@@ -18,22 +17,24 @@
1817
import com.demo.bankapp.exception.BadRequestException;
1918
import com.demo.bankapp.model.User;
2019
import com.demo.bankapp.request.CreateNewUserRequest;
21-
import com.demo.bankapp.request.LoginRequest;
2220
import com.demo.bankapp.service.abstractions.IUserService;
2321
import com.demo.bankapp.service.abstractions.IWealthService;
2422

2523
@RestController
2624
@RequestMapping(value = "/user", produces = { MediaType.APPLICATION_JSON_VALUE })
2725
public class UserController {
2826

29-
@Autowired
3027
private IUserService userService;
31-
32-
@Autowired
3328
private IWealthService wealthService;
3429

35-
@Autowired
3630
private UserResourceAssembler assembler;
31+
32+
@Autowired
33+
public UserController(IUserService userService, IWealthService wealthService, UserResourceAssembler assembler) {
34+
this.userService = userService;
35+
this.wealthService = wealthService;
36+
this.assembler = assembler;
37+
}
3738

3839
@GetMapping("/find/all")
3940
public List<Resource<User>> findAll() {

BankApplicationBackend/src/main/java/com/demo/bankapp/controller/WealthController.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.hateoas.Resource;
55
import org.springframework.http.MediaType;
6-
import org.springframework.web.bind.annotation.CrossOrigin;
76
import org.springframework.web.bind.annotation.PostMapping;
87
import org.springframework.web.bind.annotation.RequestBody;
98
import org.springframework.web.bind.annotation.RequestMapping;
@@ -21,15 +20,18 @@
2120
@RequestMapping(value = "/wealth", produces = { MediaType.APPLICATION_JSON_VALUE })
2221
public class WealthController {
2322

24-
@Autowired
2523
private IWealthService wealthService;
26-
27-
@Autowired
2824
private IUserService userService;
2925

30-
@Autowired
3126
private WealthResourceAssembler assembler;
3227

28+
@Autowired
29+
public WealthController(IWealthService wealthService, IUserService userService, WealthResourceAssembler assembler) {
30+
this.wealthService = wealthService;
31+
this.userService = userService;
32+
this.assembler = assembler;
33+
}
34+
3335
@PostMapping("/retrieve")
3436
public Resource<Wealth> retrieveWealth(@RequestBody RetrieveWealthRequest request) {
3537

BankApplicationBackend/src/main/java/com/demo/bankapp/repository/TransferRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public interface TransferRepository extends JpaRepository<Transfer, Long> {
1414

1515
@Query(value = "SELECT t FROM Transfer t WHERE t.fromUserId = :userId and t.transferTime >= DATEADD(day, -1, GETDATE())")
16+
1617
List<Transfer> findAllTransfersFrom24Hours(@Param("userId") Long userId);
1718

1819
}

BankApplicationBackend/src/main/java/com/demo/bankapp/service/concretions/TransactionService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
@Service
1414
public class TransactionService implements ITransactionService {
1515

16+
private TransactionRepository repository;
17+
1618
@Autowired
17-
TransactionRepository repository;
19+
public TransactionService(TransactionRepository repository) {
20+
this.repository = repository;
21+
}
1822

1923
@Override
2024
public Transaction createNewTransaction(Long userId, boolean isBuying, String currency, BigDecimal amount) {

BankApplicationBackend/src/main/java/com/demo/bankapp/service/concretions/TransferService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
@Service
1313
public class TransferService implements ITransferService {
1414

15+
private TransferRepository repository;
16+
1517
@Autowired
16-
TransferRepository repository;
18+
public TransferService(TransferRepository repository) {
19+
this.repository = repository;
20+
}
1721

1822
@Override
1923
public Transfer createNewTransfer(Transfer transfer) {
2024
return repository.save(transfer);
2125
}
22-
26+
2327
@Override
2428
public List<Transfer> findAllTransfersFrom24Hours(Long userId) {
2529
return repository.findAllTransfersFrom24Hours(userId);

BankApplicationBackend/src/main/java/com/demo/bankapp/service/concretions/UserService.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
@Service
2020
public class UserService implements IUserService, UserDetailsService {
2121

22-
@Autowired
2322
private UserRepository repository;
23+
private PasswordEncoder passwordEncoder;
2424

2525
@Autowired
26-
private PasswordEncoder passwordEncoder;
26+
public UserService(UserRepository repository, PasswordEncoder passwordEncoder) {
27+
this.repository = repository;
28+
this.passwordEncoder = passwordEncoder;
29+
}
2730

2831
@Override
2932
public List<User> findAll() {
@@ -55,7 +58,7 @@ public User findByTcno(String tcno) {
5558
else
5659
return user;
5760
}
58-
61+
5962
@Override
6063
public UserDetails loadUserByUsername(String username) {
6164
User user = repository.findByUsername(username);
@@ -67,17 +70,4 @@ public UserDetails loadUserByUsername(String username) {
6770
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), emptyList());
6871
}
6972

70-
// Avoid timing attacks?
71-
// private boolean isEqual(byte[] a, byte[] b) {
72-
// if (a.length != b.length) {
73-
// return false;
74-
// }
75-
//
76-
// int result = 0;
77-
// for (int i = 0; i < a.length; i++) {
78-
// result |= a[i] ^ b[i];
79-
// }
80-
// return result == 0;
81-
// }
82-
8373
}

BankApplicationBackend/src/main/java/com/demo/bankapp/service/concretions/WealthService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
@Service
2020
public class WealthService implements IWealthService {
2121

22-
@Autowired
2322
private WealthRepository repository;
2423

24+
@Autowired
25+
public WealthService(WealthRepository repository) {
26+
this.repository = repository;
27+
}
28+
2529
@Override
2630
public void newWealthRecord(Long userId) {
2731

0 commit comments

Comments
 (0)