Skip to content

Commit

Permalink
feat: added first common files (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueZaim authored Jan 17, 2024
2 parents d288ac0 + cea21a8 commit 456c91b
Show file tree
Hide file tree
Showing 17 changed files with 390 additions and 17 deletions.
65 changes: 48 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
# Compiled class file
target/
release/
dependency-reduced-pom.xml
.mvn/wrapper/

*.class
*.jar
*.war
*.ear
*.log

.classpath
.project
.settings/

.idea/

*.iml
*.ipr
*.iws
.idea/
*.class

# Log file
logs/
*.log

# BlueJ files
*.ctxt
log/
target/

# Mobile Tools for Java (J2ME)
.mtj.tmp/
*.idea/
*.iml
*.ipr
*.iws

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
.gradle/
build/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
nbproject/private/
build/

.vscode/

nb-configuration.xml

.cache/
.cproject
.settings/
.tmproj
*.log
*.tmp
*.bak
*.swp
Thumbs.db
Desktop.ini
.DS_Store
72 changes: 72 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.grupo63.techchallenge</groupId>
<artifactId>service-common</artifactId>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<version>1.0</version>
<url>http://maven.apache.org</url>
<name>service-common</name>
<description>FIAP SOAT1 2023 - Group 63 - Common files</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<version>3.2.1</version>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<version>3.2.1</version>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<version>3.2.1</version>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<version>3.2.1</version>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>3.2.1</version>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<version>3.2.1</version>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package br.com.grupo63.techchallenge.common.api.controller;

import br.com.grupo63.techchallenge.common.api.controller.dto.DefaultResponseDTO;
import br.com.grupo63.techchallenge.common.exception.GenericException;
import br.com.grupo63.techchallenge.common.exception.NotFoundException;
import br.com.grupo63.techchallenge.common.exception.ValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Objects;
import java.util.stream.Collectors;

public abstract class AbstractAPIController {

@Autowired
private MessageSource messageSource;

@ExceptionHandler
public ResponseEntity<DefaultResponseDTO> handleException(Exception exception) {
exception.printStackTrace();
DefaultResponseDTO responseDTO = new DefaultResponseDTO(
messageSource.getMessage("default.title.unknownError", null, LocaleContextHolder.getLocale()),
messageSource.getMessage("default.title.unknownError.description", null, LocaleContextHolder.getLocale()));

if (exception instanceof ValidationException validationException) {
responseDTO.setTitle(messageSource.getMessage(validationException.getName(), null, LocaleContextHolder.getLocale()));
responseDTO.setDescription(messageSource.getMessage(validationException.getDescription(), null, LocaleContextHolder.getLocale()));

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseDTO);
} else if (exception instanceof MethodArgumentNotValidException methodArgumentNotValidException) {
responseDTO.setTitle(messageSource.getMessage("default.title.validationError", null, LocaleContextHolder.getLocale()));
responseDTO.setDescription(
methodArgumentNotValidException
.getBindingResult().getAllErrors()
.stream().map(ObjectError::getDefaultMessage)
.filter(Objects::nonNull)
.map(message -> messageSource.getMessage(message, null, LocaleContextHolder.getLocale()))
.collect(Collectors.joining("; ")));

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseDTO);
} else if (exception instanceof NotFoundException) {
responseDTO.setTitle(messageSource.getMessage("default.title.notFoundError", null, LocaleContextHolder.getLocale()));
responseDTO.setDescription(messageSource.getMessage("default.title.notFoundError.description", null, LocaleContextHolder.getLocale()));

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseDTO);
} else if (exception instanceof GenericException genericException) {
responseDTO.setTitle(genericException.getName());
responseDTO.setDescription(genericException.getDescription());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseDTO);
}

return ResponseEntity.internalServerError().body(responseDTO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.grupo63.techchallenge.common.api.controller.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor

public class DefaultResponseDTO {
private String title;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package br.com.grupo63.techchallenge.common.controller.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public abstract class AbstractControllerDTO {

@Schema(defaultValue = "1")
protected Long id;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package br.com.grupo63.techchallenge.common.domain;

import br.com.grupo63.techchallenge.common.domain.validation.group.Update;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public abstract class Entity implements Serializable {

@NotNull(message = "order.create.idNotNull", groups = {Update.class})
@Min(value = 1, message = "order.create.idNotNull", groups = {Update.class})
protected Long id;

protected boolean deleted = false;

public void delete() {
this.deleted = true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package br.com.grupo63.techchallenge.common.domain.validation.group;

public interface Create {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package br.com.grupo63.techchallenge.common.domain.validation.group;

public interface Delete {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package br.com.grupo63.techchallenge.common.domain.validation.group;

public interface Read {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package br.com.grupo63.techchallenge.common.domain.validation.group;

public interface Update {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package br.com.grupo63.techchallenge.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class GenericException extends Exception {
private String name;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package br.com.grupo63.techchallenge.common.exception;

public class NotFoundException extends GenericException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package br.com.grupo63.techchallenge.common.exception;


public class ValidationException extends GenericException {

public ValidationException(String name, String description) {
super(name, description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package br.com.grupo63.techchallenge.common.gateway;

import java.util.List;
import java.util.Optional;

public interface IPersistenceEntityGateway<T> {

List<T> findByDeletedFalse();

T saveAndFlush(T entity);

Optional<T> findByIdAndDeletedFalse(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.com.grupo63.techchallenge.common.gateway.repository;

import java.util.List;
import java.util.Optional;

public interface IJpaRepository<E> {

Optional<E> findByIdAndDeletedFalse(Long id);

List<E> findByDeletedFalse();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package br.com.grupo63.techchallenge.common.gateway.repository.entity;

import br.com.grupo63.techchallenge.common.domain.Entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter

@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public abstract class PersistenceEntity implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

@Id
@Access(AccessType.FIELD)
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;

@Basic
@Column(name = "deleted", nullable = false)
protected boolean deleted = false;

@Column(name = "creation_date", nullable = false)
@LastModifiedDate
private LocalDateTime creationDate;

@Column(name = "last_update_date", nullable = false)
@LastModifiedDate
private LocalDateTime lastUpdateDate;

public PersistenceEntity(Entity entity) {
this.id = entity.getId();
this.deleted = entity.isDeleted();
}
}
Loading

0 comments on commit 456c91b

Please sign in to comment.