Skip to content

Commit

Permalink
implementando o docker no projeto
Browse files Browse the repository at this point in the history
  • Loading branch information
GabryelBoeira committed Nov 28, 2024
1 parent 86c4f78 commit 7b40418
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 35 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@
- IDE: IntelliJ
- Documentação da API:
- Banco de Dados: MySQL 8
- AI utilizada para auxilio documentação do Projeto [Codeium](https://codeium.com/profile/gabryelboeira)
- Docker
- AI utilizada para auxilio documentação do Projeto [Codeium](https://codeium.com/profile/gabryelboeira)


## Informações para a execução do projeto:

- Acessar a raiz do projeto em VideoLocadora/, execurta o seguinde comando para executar o projeto
```
docker-compose up --build -d
```
5 changes: 5 additions & 0 deletions VideoLocadora/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
.gitignore
HELP.md
mvnw*
.mvn
3 changes: 2 additions & 1 deletion VideoLocadora/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
!**/src/test/**/target
*~

### STS ###
.apt_generated
Expand Down
4 changes: 4 additions & 0 deletions VideoLocadora/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:21
EXPOSE 8080
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
22 changes: 22 additions & 0 deletions VideoLocadora/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.8'

services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: videolocadora
ports:
- "3306:3306"
restart: always

app:
build: .
ports:
- "8080:8080"
depends_on:
- db
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/videolocadora
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
50 changes: 44 additions & 6 deletions VideoLocadora/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
<version>3.3.6</version>
<relativePath/>
</parent>

<groupId>io.github.gabryel</groupId>
Expand Down Expand Up @@ -38,6 +38,17 @@
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<version>3.1.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down Expand Up @@ -89,6 +100,11 @@
<version>2.6.0</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>

<profiles>
Expand All @@ -97,9 +113,11 @@
<activeByDefault>true</activeByDefault>
</activation>
<id>development</id>

<properties>
<packaging>jar</packaging>
<tomcat.scope>compile</tomcat.scope>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>
Expand All @@ -109,16 +127,36 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<excludes>
<exclude>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<version>1.18.36</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.6.3</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

Expand All @@ -17,13 +15,10 @@
@Configuration
public class Messages {

@Value("${spring.messages.default-locale}")
private String defaultLocale;
public static final String DEFAULT_LOCALE = "pt-BR";

@Bean
public LocaleResolver localeResolver() {
return new SessionLocaleResolver();
}
@Value("${spring.messages.default-locale}")
private String localeSelected;

@Bean(name="messageSource")
public ResourceBundleMessageSource messageSource() {
Expand Down Expand Up @@ -58,10 +53,10 @@ public String getMessage(String message) {
* @return the retrieved message, or an empty string if the message code is blank or the message source is not found
*/
public String getMessage(String message, Object... args) {
if (StringUtils.isNotBlank(defaultLocale)) defaultLocale = "pt-BR";
if (StringUtils.isNotBlank(localeSelected)) localeSelected = DEFAULT_LOCALE;

if (StringUtils.isNotBlank(message))
return messageSource().getMessage(message, args, Locale.forLanguageTag(defaultLocale));
return messageSource().getMessage(message, args, Locale.forLanguageTag(localeSelected));

return "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
package io.github.gabryel.videolocadora.controller;

import io.github.gabryel.videolocadora.dto.customer.CustomerDetailDTO;
import io.github.gabryel.videolocadora.exception.BusinessException;
import io.github.gabryel.videolocadora.service.customer.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/customer")
@Tag(name = "Cliente", description = "Gerenciar/Manipular dados de clientes")
public class CustomerController {

private final CustomerService customerService;

public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}

@GetMapping("/")
public String index() {
return "Hello World";
}

@Operation(summary = "Buscar cliente por ID")
@GetMapping("/{id}")
public ResponseEntity<Object> getById(@PathVariable Long id) throws BusinessException {
public ResponseEntity<CustomerDetailDTO> getById(@PathVariable Long id) throws BusinessException {
return ResponseEntity.ok(customerService.findById(id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public record CustomerSaveDTO(
@NotBlank
String cpf,

@NotNull
boolean delayDevolution,

@Email
String email
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CustomerEntity implements Serializable {
private String cpf;

@Column(name = "delay_devolution")
private boolean delayDevolution;
private boolean delayDevolution = false;

private String email;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.gabryel.videolocadora.mapper.customer;

import io.github.gabryel.videolocadora.dto.customer.CustomerDetailDTO;
import io.github.gabryel.videolocadora.entity.customer.CustomerEntity;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;


@Mapper
public interface CustomerMapper {

CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);

CustomerDetailDTO toDetailDTO(CustomerEntity customerEntity);

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package io.github.gabryel.videolocadora.service.customer;

import io.github.gabryel.videolocadora.dto.customer.CustomerDetailDTO;
import io.github.gabryel.videolocadora.entity.customer.CustomerEntity;
import io.github.gabryel.videolocadora.exception.BusinessException;
import io.github.gabryel.videolocadora.mapper.customer.CustomerMapper;
import io.github.gabryel.videolocadora.repository.customer.CustomerRepository;
import lombok.AllArgsConstructor;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class CustomerService {

private final CustomerRepository customerRepository;
private final CustomerMapper customerMapper = Mappers.getMapper(CustomerMapper.class);

public CustomerService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

public void save(CustomerEntity customer) {
customerRepository.save(customer);
Expand All @@ -25,14 +31,25 @@ public void save(CustomerEntity customer) {
* @return the customer found
* @throws BusinessException if the customer is not found
*/
public CustomerEntity findById(Long id) throws BusinessException {
return customerRepository.findById(id).orElseThrow(() -> new BusinessException("Customer not found"));
public CustomerDetailDTO findById(Long id) throws BusinessException {
var entity = customerRepository.findById(id).orElseThrow(() -> new BusinessException("Customer not found"));
return customerMapper.toDetailDTO(entity);
}

/**
* Retrieves all customers.
*
* @return a list of all customers
*/
public List<CustomerEntity> findAll() {
return customerRepository.findAll();
}

/**
* Deletes a customer by ID.
*
* @param id the ID of the customer to be deleted
*/
public void delete(Long id) {
customerRepository.deleteById(id);
}
Expand Down
8 changes: 6 additions & 2 deletions VideoLocadora/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ springdoc:
api-docs:
path: /api-docs


docker:
compose:
enabled: true
file: docker-compose.yml
profiles: development
---
spring:
config:
Expand All @@ -41,7 +45,7 @@ spring:
sql.init.mode: always
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/videolocadora?useSSL=false
url: ${SPRING_DATASOURCE_URL}
username: root
password: root
jpa:
Expand Down
13 changes: 8 additions & 5 deletions VideoLocadora/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#-- ADD CLIENTES, CPFs gerados automaticamente https://www.4devs.com.br/gerador_de_cpf

create table CUSTOMER (
ID bigint not null,
NAME varchar(255),
CPF varchar(255),
DELAY_DEVOLUTION boolean,
EMAIL varchar(255),
primary key (ID)
);

insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-2, 'João' , '37772382014', false, 'j@j.com');
insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-3, 'Jose' , '06355528091', false, 'j@j.com');
insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-4, 'Carlos', '80071319069', false, 'j@j.com');
insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-5, 'Pedro' , '81454522011', false, 'j@j.com');
insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-6, 'Will' , '14915748014', false, 'j@j.com');

0 comments on commit 7b40418

Please sign in to comment.