Skip to content

Commit

Permalink
feat: use **Project Lombok** and add support for login using email or…
Browse files Browse the repository at this point in the history
… account number (#26)
  • Loading branch information
AmrElsayyad authored Jul 14, 2024
1 parent e6b40b4 commit 48dc66d
Show file tree
Hide file tree
Showing 55 changed files with 937 additions and 2,233 deletions.
17 changes: 8 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
</properties>
<dependencies>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail-junit5</artifactId>
<version>2.0.0-alpha-2</version>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail-junit5</artifactId>
<version>2.0.0-alpha-2</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
Expand All @@ -47,7 +52,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
Expand Down Expand Up @@ -92,7 +96,6 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand Down Expand Up @@ -120,7 +123,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down Expand Up @@ -160,7 +162,4 @@
</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

import com.github.benmanes.caffeine.cache.Caffeine;


import lombok.val;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
val cacheManager = new CaffeineCacheManager();
cacheManager.setCacheNames(List.of("otpAttempts")); // Define the cache name
cacheManager.setCaffeine(caffeineConfig());
return cacheManager;
Expand All @@ -31,4 +31,5 @@ public Caffeine<Object, Object> caffeineConfig() {
.maximumSize(100) // Maximum of 100 entries in the cache
.recordStats(); // For monitoring cache statistics (optional)
}

}
12 changes: 7 additions & 5 deletions src/main/java/com/webapp/bankingportal/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;

import lombok.val;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -15,7 +18,7 @@ public class SwaggerConfig {

@Bean
public OpenAPI customOpenAPI() {
final String securitySchemeName = "Bearer";
val securitySchemeName = "Bearer";
return new OpenAPI()
.info(new Info().title("Banking Portal API")
.description("This is auth service use for validate the user.")
Expand All @@ -32,8 +35,7 @@ public OpenAPI customOpenAPI() {
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
);
.bearerFormat("JWT")));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

import jakarta.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class WebSecurityConfig {

private static final String[] PUBLIC_URLS = {
Expand All @@ -41,34 +44,29 @@ public class WebSecurityConfig {
"/actuator/**"
};

@Autowired
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;

@Autowired
private TokenService tokenService;
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final TokenService tokenService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(tokenService).passwordEncoder(passwordEncoder());
}

@Bean
public PasswordEncoder passwordEncoder() {
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public AuthenticationManager authenticationManager(
AuthenticationManager authenticationManager(
AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(requests -> requests
.requestMatchers(PUBLIC_URLS).permitAll()
Expand All @@ -90,4 +88,5 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti

return http.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.webapp.bankingportal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -17,116 +11,99 @@
import com.webapp.bankingportal.dto.FundTransferRequest;
import com.webapp.bankingportal.dto.PinRequest;
import com.webapp.bankingportal.dto.PinUpdateRequest;
import com.webapp.bankingportal.dto.TransactionDTO;
import com.webapp.bankingportal.service.AccountService;
import com.webapp.bankingportal.service.TransactionService;
import com.webapp.bankingportal.util.JsonUtil;
import com.webapp.bankingportal.util.LoggedinUser;

import lombok.RequiredArgsConstructor;
import lombok.val;

@RestController
@RequestMapping("/api/account")
@RequiredArgsConstructor
public class AccountController {

@Autowired
private AccountService accountService;

@Autowired
private TransactionService transactionService;
private final AccountService accountService;
private final TransactionService transactionService;

@GetMapping("/pin/check")
public ResponseEntity<?> checkAccountPIN() {
boolean isPINValid = accountService.isPinCreated(LoggedinUser.getAccountNumber());

Map<String, Object> result = new HashMap<>();
result.put("hasPIN", isPINValid);
public ResponseEntity<String> checkAccountPIN() {
val isPINValid = accountService.isPinCreated(LoggedinUser.getAccountNumber());

String response;
if (isPINValid) {
result.put("msg", "PIN Created");

response = "{\"hasPIN\": true, \"msg\": \"PIN Created\"}";
} else {
result.put("msg", "PIN Not Created");
response = "{\"hasPIN\": false, \"msg\": \"PIN Not Created\"}";
}

return new ResponseEntity<>(result, HttpStatus.OK);
return ResponseEntity.ok(response);
}

@PostMapping("/pin/create")
public ResponseEntity<?> createPIN(@RequestBody PinRequest pinRequest) {
public ResponseEntity<String> createPIN(@RequestBody PinRequest pinRequest) {
accountService.createPin(
LoggedinUser.getAccountNumber(),
pinRequest.getPassword(),
pinRequest.getPin());

Map<String, String> response = new HashMap<>();
response.put("msg", "PIN created successfully");
pinRequest.password(),
pinRequest.pin());

return new ResponseEntity<>(response, HttpStatus.OK);
return ResponseEntity.ok("{\"msg\": \"PIN created successfully\"}");
}

@PostMapping("/pin/update")
public ResponseEntity<?> updatePIN(@RequestBody PinUpdateRequest pinUpdateRequest) {
public ResponseEntity<String> updatePIN(@RequestBody PinUpdateRequest pinUpdateRequest) {
accountService.updatePin(
LoggedinUser.getAccountNumber(),
pinUpdateRequest.getOldPin(),
pinUpdateRequest.getPassword(),
pinUpdateRequest.getNewPin());
pinUpdateRequest.oldPin(),
pinUpdateRequest.password(),
pinUpdateRequest.newPin());

Map<String, String> response = new HashMap<>();
response.put("msg", "PIN updated successfully");

return new ResponseEntity<>(response, HttpStatus.OK);
return ResponseEntity.ok("{\"msg\": \"PIN updated successfully\"}");
}

@PostMapping("/deposit")
public ResponseEntity<?> cashDeposit(@RequestBody AmountRequest amountRequest) {
public ResponseEntity<String> cashDeposit(@RequestBody AmountRequest amountRequest) {
accountService.cashDeposit(
LoggedinUser.getAccountNumber(),
amountRequest.getPin(),
amountRequest.getAmount());

Map<String, String> response = new HashMap<>();
response.put("msg", "Cash deposited successfully");
amountRequest.pin(),
amountRequest.amount());

return new ResponseEntity<>(response, HttpStatus.OK);
return ResponseEntity.ok("{\"msg\": \"Cash deposited successfully\"}");
}

@PostMapping("/withdraw")
public ResponseEntity<?> cashWithdrawal(@RequestBody AmountRequest amountRequest) {
public ResponseEntity<String> cashWithdrawal(@RequestBody AmountRequest amountRequest) {
accountService.cashWithdrawal(
LoggedinUser.getAccountNumber(),
amountRequest.getPin(),
amountRequest.getAmount());

Map<String, String> response = new HashMap<>();
response.put("msg", "Cash withdrawn successfully");
amountRequest.pin(),
amountRequest.amount());

return new ResponseEntity<>(response, HttpStatus.OK);
return ResponseEntity.ok("{\"msg\": \"Cash withdrawn successfully\"}");
}

@PostMapping("/fund-transfer")
public ResponseEntity<?> fundTransfer(@RequestBody FundTransferRequest fundTransferRequest) {
public ResponseEntity<String> fundTransfer(@RequestBody FundTransferRequest fundTransferRequest) {
if (LoggedinUser.getAccountNumber()
.equals(fundTransferRequest.getTargetAccountNumber())) {
return new ResponseEntity<>(
"Source and target account cannot be the same",
HttpStatus.BAD_REQUEST);
.equals(fundTransferRequest.targetAccountNumber())) {
return ResponseEntity.badRequest()
.body("Source and target account cannot be the same");
}

accountService.fundTransfer(
LoggedinUser.getAccountNumber(),
fundTransferRequest.getTargetAccountNumber(),
fundTransferRequest.getPin(),
fundTransferRequest.getAmount());
fundTransferRequest.targetAccountNumber(),
fundTransferRequest.pin(),
fundTransferRequest.amount());

Map<String, String> response = new HashMap<>();
response.put("msg", "Fund transferred successfully");

return new ResponseEntity<>(response, HttpStatus.OK);
return ResponseEntity.ok("{\"msg\": \"Fund transferred successfully\"}");
}

@GetMapping("/transactions")
public ResponseEntity<List<TransactionDTO>> getAllTransactionsByAccountNumber() {
List<TransactionDTO> transactions = transactionService
public ResponseEntity<String> getAllTransactionsByAccountNumber() {
val transactions = transactionService
.getAllTransactionsByAccountNumber(LoggedinUser.getAccountNumber());
return ResponseEntity.ok(transactions);
return ResponseEntity.ok(JsonUtil.toJson(transactions));
}

}
Loading

0 comments on commit 48dc66d

Please sign in to comment.