Skip to content

Commit 456c91b

Browse files
authored
feat: added first common files (#1)
2 parents d288ac0 + cea21a8 commit 456c91b

File tree

17 files changed

+390
-17
lines changed

17 files changed

+390
-17
lines changed

.gitignore

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,55 @@
1-
# Compiled class file
1+
target/
2+
release/
3+
dependency-reduced-pom.xml
4+
.mvn/wrapper/
5+
6+
*.class
7+
*.jar
8+
*.war
9+
*.ear
10+
*.log
11+
12+
.classpath
13+
.project
14+
.settings/
15+
16+
.idea/
17+
18+
*.iml
19+
*.ipr
20+
*.iws
21+
.idea/
222
*.class
323

4-
# Log file
24+
logs/
525
*.log
626

7-
# BlueJ files
8-
*.ctxt
27+
log/
28+
target/
929

10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
30+
*.idea/
31+
*.iml
32+
*.ipr
33+
*.iws
1234

13-
# Package Files #
14-
*.jar
15-
*.war
16-
*.nar
17-
*.ear
18-
*.zip
19-
*.tar.gz
20-
*.rar
35+
.gradle/
36+
build/
2137

22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
24-
replay_pid*
38+
nbproject/private/
39+
build/
40+
41+
.vscode/
42+
43+
nb-configuration.xml
44+
45+
.cache/
46+
.cproject
47+
.settings/
48+
.tmproj
49+
*.log
50+
*.tmp
51+
*.bak
52+
*.swp
53+
Thumbs.db
54+
Desktop.ini
55+
.DS_Store

pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>br.com.grupo63.techchallenge</groupId>
6+
<artifactId>service-common</artifactId>
7+
<packaging>jar</packaging>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<groupId>org.apache.maven.plugins</groupId>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<configuration>
14+
<source>17</source>
15+
<target>17</target>
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
20+
<version>1.0</version>
21+
<url>http://maven.apache.org</url>
22+
<name>service-common</name>
23+
<description>FIAP SOAT1 2023 - Group 63 - Common files</description>
24+
<properties>
25+
<java.version>17</java.version>
26+
</properties>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.projectlombok</groupId>
30+
<artifactId>lombok</artifactId>
31+
<version>1.18.24</version>
32+
<optional>true</optional>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<version>3.2.1</version>
37+
<artifactId>spring-boot-starter-web</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.session</groupId>
41+
<version>3.2.1</version>
42+
<artifactId>spring-session-core</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<version>3.2.1</version>
47+
<artifactId>spring-boot-starter-validation</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<version>3.2.1</version>
52+
<artifactId>spring-boot-starter-data-jpa</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-devtools</artifactId>
57+
<version>3.2.1</version>
58+
<scope>runtime</scope>
59+
<optional>true</optional>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<version>3.2.1</version>
64+
<artifactId>spring-boot-starter-actuator</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springdoc</groupId>
68+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
69+
<version>2.1.0</version>
70+
</dependency>
71+
</dependencies>
72+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package br.com.grupo63.techchallenge.common.api.controller;
2+
3+
import br.com.grupo63.techchallenge.common.api.controller.dto.DefaultResponseDTO;
4+
import br.com.grupo63.techchallenge.common.exception.GenericException;
5+
import br.com.grupo63.techchallenge.common.exception.NotFoundException;
6+
import br.com.grupo63.techchallenge.common.exception.ValidationException;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.MessageSource;
9+
import org.springframework.context.i18n.LocaleContextHolder;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.validation.ObjectError;
13+
import org.springframework.web.bind.MethodArgumentNotValidException;
14+
import org.springframework.web.bind.annotation.ExceptionHandler;
15+
16+
import java.util.Objects;
17+
import java.util.stream.Collectors;
18+
19+
public abstract class AbstractAPIController {
20+
21+
@Autowired
22+
private MessageSource messageSource;
23+
24+
@ExceptionHandler
25+
public ResponseEntity<DefaultResponseDTO> handleException(Exception exception) {
26+
exception.printStackTrace();
27+
DefaultResponseDTO responseDTO = new DefaultResponseDTO(
28+
messageSource.getMessage("default.title.unknownError", null, LocaleContextHolder.getLocale()),
29+
messageSource.getMessage("default.title.unknownError.description", null, LocaleContextHolder.getLocale()));
30+
31+
if (exception instanceof ValidationException validationException) {
32+
responseDTO.setTitle(messageSource.getMessage(validationException.getName(), null, LocaleContextHolder.getLocale()));
33+
responseDTO.setDescription(messageSource.getMessage(validationException.getDescription(), null, LocaleContextHolder.getLocale()));
34+
35+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseDTO);
36+
} else if (exception instanceof MethodArgumentNotValidException methodArgumentNotValidException) {
37+
responseDTO.setTitle(messageSource.getMessage("default.title.validationError", null, LocaleContextHolder.getLocale()));
38+
responseDTO.setDescription(
39+
methodArgumentNotValidException
40+
.getBindingResult().getAllErrors()
41+
.stream().map(ObjectError::getDefaultMessage)
42+
.filter(Objects::nonNull)
43+
.map(message -> messageSource.getMessage(message, null, LocaleContextHolder.getLocale()))
44+
.collect(Collectors.joining("; ")));
45+
46+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseDTO);
47+
} else if (exception instanceof NotFoundException) {
48+
responseDTO.setTitle(messageSource.getMessage("default.title.notFoundError", null, LocaleContextHolder.getLocale()));
49+
responseDTO.setDescription(messageSource.getMessage("default.title.notFoundError.description", null, LocaleContextHolder.getLocale()));
50+
51+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseDTO);
52+
} else if (exception instanceof GenericException genericException) {
53+
responseDTO.setTitle(genericException.getName());
54+
responseDTO.setDescription(genericException.getDescription());
55+
56+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseDTO);
57+
}
58+
59+
return ResponseEntity.internalServerError().body(responseDTO);
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package br.com.grupo63.techchallenge.common.api.controller.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
@Getter
9+
@Setter
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
13+
public class DefaultResponseDTO {
14+
private String title;
15+
private String description;
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package br.com.grupo63.techchallenge.common.controller.dto;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
public abstract class AbstractControllerDTO {
10+
11+
@Schema(defaultValue = "1")
12+
protected Long id;
13+
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package br.com.grupo63.techchallenge.common.domain;
2+
3+
import br.com.grupo63.techchallenge.common.domain.validation.group.Update;
4+
import jakarta.validation.constraints.Min;
5+
import jakarta.validation.constraints.NotNull;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
import lombok.Setter;
10+
11+
import java.io.Serializable;
12+
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
@Getter
16+
@Setter
17+
public abstract class Entity implements Serializable {
18+
19+
@NotNull(message = "order.create.idNotNull", groups = {Update.class})
20+
@Min(value = 1, message = "order.create.idNotNull", groups = {Update.class})
21+
protected Long id;
22+
23+
protected boolean deleted = false;
24+
25+
public void delete() {
26+
this.deleted = true;
27+
}
28+
29+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package br.com.grupo63.techchallenge.common.domain.validation.group;
2+
3+
public interface Create {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package br.com.grupo63.techchallenge.common.domain.validation.group;
2+
3+
public interface Delete {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package br.com.grupo63.techchallenge.common.domain.validation.group;
2+
3+
public interface Read {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package br.com.grupo63.techchallenge.common.domain.validation.group;
2+
3+
public interface Update {
4+
}

0 commit comments

Comments
 (0)