Skip to content

Commit cee6820

Browse files
Merge pull request #14 from codeplusofc/implementando-camada-config
Implementando camada config
2 parents c915751 + a95daf3 commit cee6820

File tree

8 files changed

+130
-5
lines changed

8 files changed

+130
-5
lines changed

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.4.2</version>
8+
<version>3.3.2</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.code</groupId>
@@ -45,6 +45,16 @@
4545
<artifactId>h2</artifactId>
4646
<scope>runtime</scope>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.springdoc</groupId>
50+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
51+
<version>2.2.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.swagger.core.v3</groupId>
55+
<artifactId>swagger-models-jakarta</artifactId>
56+
<version>2.2.9</version>
57+
</dependency>
4858
<dependency>
4959
<groupId>org.springframework.boot</groupId>
5060
<artifactId>spring-boot-starter-test</artifactId>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.code.food.config;
2+
3+
import io.swagger.v3.oas.models.Components;
4+
import io.swagger.v3.oas.models.OpenAPI;
5+
import io.swagger.v3.oas.models.info.Info;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class SwaggerConfiguration {
11+
12+
@Bean
13+
public OpenAPI customOpenAPI() {
14+
return new OpenAPI().components(new Components())
15+
.info(new Info().title("Swagger").description("Documentação Assembleia"));
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.code.food.exception;
2+
3+
import java.time.LocalDateTime;
4+
5+
public class ApiError {
6+
7+
private int statusCode;
8+
private String message;
9+
private LocalDateTime date;
10+
11+
public ApiError(int statusCode, String message, LocalDateTime date) {
12+
this.statusCode = statusCode;
13+
this.message = message;
14+
this.date = date;
15+
}
16+
17+
public int getStatusCode() {
18+
return statusCode;
19+
}
20+
21+
public void setStatusCode(int statusCode) {
22+
this.statusCode = statusCode;
23+
}
24+
25+
public String getMessage() {
26+
return message;
27+
}
28+
29+
public void setMessage(String message) {
30+
this.message = message;
31+
}
32+
33+
public LocalDateTime getDate() {
34+
return date;
35+
}
36+
37+
public void setDate(LocalDateTime date) {
38+
this.date = date;
39+
}
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.code.food.exception;
2+
3+
public class BadRequestException extends RuntimeException{
4+
5+
public BadRequestException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.code.food.exception;
2+
3+
public class ForbiddenException extends RuntimeException{
4+
5+
public ForbiddenException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.code.food.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.ControllerAdvice;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
8+
import java.time.LocalDateTime;
9+
10+
@ControllerAdvice
11+
public class HttpErrorExceptionHandler {
12+
13+
private ResponseEntity<ApiError> buildErrorResponse(HttpStatus statusCode, String message) {
14+
var error = new ApiError(statusCode.value(), message, LocalDateTime.now());
15+
return ResponseEntity.status(statusCode).body(error);
16+
}
17+
18+
@ExceptionHandler(BadRequestException.class)
19+
public ResponseEntity<ApiError> badRequest(BadRequestException badRequestException) {
20+
return buildErrorResponse(HttpStatus.BAD_REQUEST, badRequestException.getMessage());
21+
}
22+
23+
//TODO: Criar tratamento de erro para usuários não encontrados, esse tratamento vai ser para o 404 NotFoundException
24+
@ExceptionHandler(NotFoundException.class)
25+
public ResponseEntity<ApiError> notFound(NotFoundException notFoundException) {
26+
return buildErrorResponse(HttpStatus.NOT_FOUND, notFoundException.getMessage());
27+
}
28+
29+
@ExceptionHandler(ForbiddenException.class)
30+
public ResponseEntity<ApiError> forbidden(ForbiddenException forbiddenException) {
31+
return buildErrorResponse(HttpStatus.FORBIDDEN, forbiddenException.getMessage());
32+
}
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.code.food.exception;
2+
3+
public class NotFoundException extends RuntimeException{
4+
5+
public NotFoundException(String message) {
6+
super(message);
7+
}
8+
}

src/main/java/com/code/food/validation/UserValidator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.code.food.validation;
22

33
import com.code.food.entity.UserEntity;
4+
import com.code.food.exception.BadRequestException;
45

56
public class UserValidator {
67

@@ -13,28 +14,28 @@ public static void validateFields(UserEntity userEntity) {
1314

1415
public static String validateName(String name) {
1516
if (name.isEmpty()) {
16-
throw new RuntimeException("O nome não pode ser vazio");
17+
throw new BadRequestException("O nome não pode ser vazio");
1718
}
1819
return name;
1920
}
2021

2122
public static String validateEmail(String email) {
2223
if (email.isEmpty()) {
23-
throw new RuntimeException("O email não pode ser vazio");
24+
throw new BadRequestException("O e-mail não pode ser vazio");
2425
}
2526
return email;
2627
}
2728

2829
public static String validatePassword(String password) {
2930
if (password.isEmpty()) {
30-
throw new RuntimeException("A senha não pode ser vazia");
31+
throw new BadRequestException("A senha não pode ser vazia");
3132
}
3233
return password;
3334
}
3435

3536
public static String validatePhone(String phone) {
3637
if (phone.isEmpty()) {
37-
throw new RuntimeException("O telefone não pode ser vazio");
38+
throw new BadRequestException("O telefone não pode ser vazio");
3839
}
3940
return phone;
4041
}

0 commit comments

Comments
 (0)