Skip to content

Commit 6b991d7

Browse files
authored
Merge pull request #31 from felipemelozx/develop
Develop
2 parents 6d8760d + 7035f2a commit 6b991d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1926
-34
lines changed

.github/workflows/codeQL.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ jobs:
2525
- name: Checkout code
2626
uses: actions/checkout@v4
2727

28-
- name: Initialize CodeQL
28+
- name: Perform CodeQL Analysis
2929
uses: github/codeql-action/init@v3
3030
with:
3131
languages: ${{ matrix.language }}
3232

33+
- name: Start Docker Compose
34+
run: docker compose up -d
35+
3336
- name: Build with Maven
34-
run: mvn clean install -Dspring.profiles.active=test
37+
run: mvn clean install -Dspring.profiles.active=dev
3538

3639
- name: Perform CodeQL Analysis
3740
uses: github/codeql-action/analyze@v3
41+
42+
- name: Stop Docker Compose
43+
if: always()
44+
run: docker compose down

.github/workflows/java-ci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ jobs:
2525
java-version: '21'
2626
distribution: 'temurin'
2727

28+
- name: Start Docker Compose
29+
run: docker compose up -d
30+
2831
- name: Build with Maven
2932
run: mvn clean install -DskipTests
3033

3134
- name: Run tests
32-
run: mvn test -Dspring.profiles.active=test
35+
run: mvn test -Dspring.profiles.active=dev
3336

3437
- name: Run Checkstyle
3538
run: mvn checkstyle:check
3639

3740
- name: Build JAR file
3841
run: mvn package
42+
43+
44+
- name: Stop Docker Compose
45+
if: always()
46+
run: docker compose down

.github/workflows/sonarCloud.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,19 @@ jobs:
3838
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
3939
restore-keys: ${{ runner.os }}-maven
4040

41+
- name: Start Docker Compose
42+
run: docker compose up -d
43+
4144
- name: SonarCloud Scan
4245
run: |
43-
mvn verify sonar:sonar -Dspring.profiles.active=test \
46+
mvn verify sonar:sonar -Dspring.profiles.active=dev \
4447
-Dsonar.projectKey=felipemelozx_TrackMoney \
4548
-Dsonar.organization=felipemelozx-trackmoney \
4649
-Dsonar.host.url=https://sonarcloud.io \
4750
-Dsonar.login=${{ secrets.SONAR_TOKEN }} \
48-
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
51+
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml \
52+
-Dsonar.java.checkstyle.reportPaths=target/checkstyle-result.xml
53+
54+
- name: Stop Docker Compose
55+
if: always()
56+
run: docker compose down

custom_check.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="Checker">
6+
<module name="LineLength">
7+
<property name="max" value="120"/>
8+
</module>
9+
<module name="TreeWalker">
10+
<module name="MemberName"/>
11+
<module name="MethodName"/>
12+
<module name="AvoidStarImport"/>
13+
<module name="PackageName"/>
14+
<module name="TypeName"/>
15+
<module name="ParameterName"/>
16+
<module name="LocalVariableName"/>
17+
<module name="StaticVariableName"/>
18+
<module name="ConstantName"/>
19+
<module name="RedundantImport"/>
20+
<module name="UnusedImports"/>
21+
<module name="EmptyLineSeparator">
22+
<property name="tokens" value="CLASS_DEF, INTERFACE_DEF, METHOD_DEF, CTOR_DEF"/>
23+
</module>
24+
<module name="NeedBraces"/>
25+
<module name="LeftCurly"/>
26+
<module name="RightCurly"/>
27+
<module name="FinalClass"/>
28+
<module name="EmptyCatchBlock"/>
29+
<module name="EqualsHashCode"/>
30+
<module name="HiddenField">
31+
<property name="ignoreConstructorParameter" value="true"/>
32+
<property name="ignoreSetter" value="true"/>
33+
</module>
34+
<module name="CyclomaticComplexity">
35+
<property name="max" value="10"/>
36+
</module>
37+
<module name="NestedIfDepth">
38+
<property name="max" value="3"/>
39+
</module>
40+
<module name="MethodLength">
41+
<property name="max" value="50"/>
42+
</module>
43+
</module>
44+
</module>

pom.xml

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@
5050
<groupId>org.springframework.boot</groupId>
5151
<artifactId>spring-boot-starter-web</artifactId>
5252
</dependency>
53-
<dependency>
54-
<groupId>com.h2database</groupId>
55-
<artifactId>h2</artifactId>
56-
<version>2.2.224</version>
57-
<scope>test</scope>
58-
</dependency>
5953
<dependency>
6054
<groupId>org.postgresql</groupId>
6155
<artifactId>postgresql</artifactId>
@@ -76,6 +70,21 @@
7670
<artifactId>spring-security-test</artifactId>
7771
<scope>test</scope>
7872
</dependency>
73+
<dependency>
74+
<groupId>org.mapstruct</groupId>
75+
<artifactId>mapstruct</artifactId>
76+
<version>1.5.5.Final</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.auth0</groupId>
80+
<artifactId>java-jwt</artifactId>
81+
<version>4.4.0</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.springdoc</groupId>
85+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
86+
<version>2.8.6</version>
87+
</dependency>
7988
</dependencies>
8089

8190
<build>
@@ -112,15 +121,14 @@
112121
<plugin>
113122
<groupId>org.apache.maven.plugins</groupId>
114123
<artifactId>maven-checkstyle-plugin</artifactId>
115-
<version>3.1.2</version>
124+
<version>3.2.0</version>
116125
<configuration>
117-
<encoding>UTF-8</encoding>
118126
<consoleOutput>true</consoleOutput>
119127
<failsOnError>false</failsOnError>
120128
<failOnViolation>true</failOnViolation>
121129
<violationSeverity>warning</violationSeverity>
122130
<outputFile>target/checkstyle-result.xml</outputFile>
123-
<configLocation>google_checks.xml</configLocation>
131+
<configLocation>custom_check.xml</configLocation>
124132
</configuration>
125133
<executions>
126134
<execution>
@@ -135,10 +143,26 @@
135143
<dependency>
136144
<groupId>com.puppycrawl.tools</groupId>
137145
<artifactId>checkstyle</artifactId>
138-
<version>8.30</version>
146+
<version>10.14.1</version>
139147
</dependency>
140148
</dependencies>
141149
</plugin>
150+
<plugin>
151+
<groupId>org.apache.maven.plugins</groupId>
152+
<artifactId>maven-compiler-plugin</artifactId>
153+
<version>3.11.0</version>
154+
<configuration>
155+
<source>${java.version}</source>
156+
<target>${java.version}</target>
157+
<annotationProcessorPaths>
158+
<path>
159+
<groupId>org.mapstruct</groupId>
160+
<artifactId>mapstruct-processor</artifactId>
161+
<version>1.5.5.Final</version>
162+
</path>
163+
</annotationProcessorPaths>
164+
</configuration>
165+
</plugin>
142166
</plugins>
143167
</build>
144168
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package fun.trackmoney.auth.controller;
2+
3+
import fun.trackmoney.auth.dto.LoginRequestDTO;
4+
import fun.trackmoney.auth.dto.LoginResponseDTO;
5+
import fun.trackmoney.auth.service.AuthService;
6+
import fun.trackmoney.user.dtos.UserRequestDTO;
7+
import fun.trackmoney.user.dtos.UserResponseDTO;
8+
import fun.trackmoney.utils.response.ApiResponse;
9+
import jakarta.validation.Valid;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
/**
17+
* Controller class to handle authentication-related requests.
18+
* Provides endpoints for user registration.
19+
*/
20+
@RestController
21+
@RequestMapping("auth/")
22+
public class AuthController {
23+
24+
private final AuthService authService;
25+
26+
/**
27+
* Constructor to initialize the AuthController with an AuthService instance.
28+
*
29+
* @param authService The service used for handling authentication operations.
30+
*/
31+
public AuthController(AuthService authService) {
32+
this.authService = authService;
33+
}
34+
35+
/**
36+
* Endpoint to register a new user.
37+
*
38+
* @param userDto The user data transfer object containing the user's registration information.
39+
* @return A ResponseEntity containing an ApiResponse
40+
* with the result of the registration operation.
41+
*/
42+
@PostMapping("register")
43+
public ResponseEntity<ApiResponse<UserResponseDTO>> register(
44+
@RequestBody @Valid UserRequestDTO userDto) {
45+
return ResponseEntity.ok().body(
46+
new ApiResponse<>(
47+
true,
48+
"User register!",
49+
authService.register(userDto),
50+
null
51+
)
52+
);
53+
}
54+
55+
@PostMapping("login")
56+
public ResponseEntity<ApiResponse<LoginResponseDTO>> login(@RequestBody LoginRequestDTO loginDto) {
57+
return ResponseEntity.ok().body(
58+
new ApiResponse<>(
59+
true,
60+
"Login successful!",
61+
authService.login(loginDto),
62+
null
63+
)
64+
);
65+
}
66+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package fun.trackmoney.auth.dto;
2+
3+
public record LoginRequestDTO(String email, String password){
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package fun.trackmoney.auth.dto;
2+
3+
public record LoginResponseDTO (String token){
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package fun.trackmoney.auth.exception;
2+
3+
import fun.trackmoney.utils.CustomFieldError;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class LoginException extends RuntimeException {
9+
private final List<CustomFieldError> errors;
10+
11+
public LoginException(String message) {
12+
super(message);
13+
this.errors = new ArrayList<>();
14+
this.errors.add(new CustomFieldError("Email: ", message));
15+
}
16+
17+
public LoginException(List<CustomFieldError> errors) {
18+
super("Email not found!");
19+
this.errors = errors;
20+
}
21+
22+
public List<CustomFieldError> getErrors() {
23+
return errors;
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fun.trackmoney.auth.infra.config;
2+
3+
import fun.trackmoney.user.entity.UserEntity;
4+
import fun.trackmoney.user.repository.UserRepository;
5+
import java.util.Collections;
6+
import org.springframework.security.core.userdetails.User;
7+
import org.springframework.security.core.userdetails.UserDetails;
8+
import org.springframework.security.core.userdetails.UserDetailsService;
9+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
10+
import org.springframework.stereotype.Component;
11+
12+
@Component
13+
public class CustomUserDetailsService implements UserDetailsService {
14+
15+
private final UserRepository repository;
16+
17+
public CustomUserDetailsService(UserRepository repository) {
18+
this.repository = repository;
19+
}
20+
21+
@Override
22+
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
23+
UserEntity user = repository.findByEmail(email)
24+
.orElseThrow(() -> new UsernameNotFoundException("User not found with email: " + email));
25+
return new User(user.getEmail(), user.getPassword(), Collections.emptyList());
26+
}
27+
}

0 commit comments

Comments
 (0)