Skip to content

Commit

Permalink
Update v1.0.2
Browse files Browse the repository at this point in the history
-Captura de excepciones
  • Loading branch information
Marc0Franc0 committed Aug 13, 2023
1 parent d33b978 commit e2e39ff
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 21 deletions.
15 changes: 11 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
</parent>
<groupId>com.api</groupId>
<artifactId>Shoool-Management</artifactId>
<version>1.0.1</version>
<name>note-management-api</name>
<description>Demo project for Spring Boot</description>
<version>1.0.2</version>
<name>School-Management</name>
<description>El proyecto tiene la posibilidad de gestionar una institución educativa, con el objetivo " +
"de facilitar la administración de estudiantes, profesores, materias y notas.</description>
<properties>
<java.version>17</java.version>
</properties>
Expand Down Expand Up @@ -74,7 +75,13 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>6.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.api.notemanagementapi.springdoc;
package com.api.notemanagementapi.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.api.notemanagementapi.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class SQLException {
//Validaciones al registrarse (username duplicado en la base de datos)
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public ResponseEntity captureSQLIntegrityConstraintViolationException
(SQLIntegrityConstraintViolationException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Message: "
.concat("Uno o más de los datos enviados ya existen, pruebe con otros."));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.api.notemanagementapi.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
import java.util.stream.Collectors;

@RestControllerAdvice
public class ValidationDtoException{
//Validaciones al registrarse-> entrada de datos vacios o nulos
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity captureMethodArgumentNotValidException
(MethodArgumentNotValidException e){
List<String> errors = e
.getBindingResult()
.getFieldErrors()
.stream().map( FieldError ::getDefaultMessage)
.collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Message: ".concat(errors.toString()));
}

//Formato JSON recibido
@ExceptionHandler( HttpMessageNotReadableException.class)
public ResponseEntity captureHttpMessageNotReadableException
(HttpMessageNotReadableException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body("Message: ".concat(e.getMessage()));
}
//Parametro de json no permitido
@ExceptionHandler( IllegalArgumentException.class)
public ResponseEntity captureHttpMessageNotReadableException
(IllegalArgumentException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body("Message: ".concat("Uno de los parámetros enviados no es permitido"));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package com.api.notemanagementapi.security.model;

import java.util.Set;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -29,6 +21,7 @@ public class UserEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Column(unique = true)
private String username;
@NotBlank
private String password;
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Database configuration
profile.active=prod
spring.jpa.hibernate.ddl-auto=${SPRING_JPA_HIBERNATE_DDL_AUTO}
spring.datasource.url= ${SPRING_DATASOURCE_URL}
spring.datasource.username= ${SPRING_DATASOURCE_USERNAME}
spring.datasource.password= ${SPRING_DATASOURCE_PASSWORD}
#JWT
jwt.time.expiration = ${JWT_TIME_EXPIRATION}
jwt.secret.key = ${JWT_SECRET_KEY}
12 changes: 6 additions & 6 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#Database configuration
spring.jpa.hibernate.ddl-auto=${SPRING_JPA_HIBERNATE_DDL_AUTO}
spring.datasource.url= ${SPRING_DATASOURCE_URL}
spring.datasource.username= ${SPRING_DATASOURCE_USERNAME}
spring.datasource.password= ${SPRING_DATASOURCE_PASSWORD}
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url= jdbc:mysql://localhost:3306/school
spring.datasource.username= root
spring.datasource.password=
#JWT
jwt.secret.key = ${JWT_SECRET_KEY}
jwt.time.expiration = ${JWT_TIME_EXPIRATION}
jwt.time.expiration = 86400000
jwt.secret.key = 76397924423F4528482B4D6251655468576D5A7134743777217A25432A46294A

0 comments on commit e2e39ff

Please sign in to comment.