diff --git a/README.md b/README.md index b83999c..cc5317c 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +- 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 +``` \ No newline at end of file diff --git a/VideoLocadora/.dockerignore b/VideoLocadora/.dockerignore new file mode 100644 index 0000000..8620276 --- /dev/null +++ b/VideoLocadora/.dockerignore @@ -0,0 +1,5 @@ +.idea +.gitignore +HELP.md +mvnw* +.mvn \ No newline at end of file diff --git a/VideoLocadora/.gitignore b/VideoLocadora/.gitignore index 549e00a..08c9d6d 100644 --- a/VideoLocadora/.gitignore +++ b/VideoLocadora/.gitignore @@ -2,7 +2,8 @@ HELP.md target/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ -!**/src/test/**/target/ +!**/src/test/**/target +*~ ### STS ### .apt_generated diff --git a/VideoLocadora/Dockerfile b/VideoLocadora/Dockerfile new file mode 100644 index 0000000..d35454d --- /dev/null +++ b/VideoLocadora/Dockerfile @@ -0,0 +1,4 @@ +FROM openjdk:21 +EXPOSE 8080 +COPY target/*.jar app.jar +ENTRYPOINT ["java","-jar","/app.jar"] diff --git a/VideoLocadora/docker-compose.yml b/VideoLocadora/docker-compose.yml new file mode 100644 index 0000000..ab0d9c6 --- /dev/null +++ b/VideoLocadora/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/VideoLocadora/pom.xml b/VideoLocadora/pom.xml index 53eae6d..18526cf 100644 --- a/VideoLocadora/pom.xml +++ b/VideoLocadora/pom.xml @@ -5,8 +5,8 @@ org.springframework.boot spring-boot-starter-parent - 3.3.4 - + 3.3.6 + io.github.gabryel @@ -38,6 +38,17 @@ spring-boot-starter-validation + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-docker-compose + 3.1.1 + + org.springframework.boot spring-boot-devtools @@ -89,6 +100,11 @@ 2.6.0 + + org.mapstruct + mapstruct + 1.6.3 + @@ -97,9 +113,11 @@ true development + jar compile + true @@ -109,16 +127,36 @@ org.springframework.boot spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 - - + 11 + 11 + + org.projectlombok lombok - - + 1.18.36 + + + org.mapstruct + mapstruct-processor + 1.6.3 + + + org.projectlombok + lombok-mapstruct-binding + 0.2.0 + + + + diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/configuration/Messages.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/configuration/Messages.java index ab77ff8..9faa060 100644 --- a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/configuration/Messages.java +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/configuration/Messages.java @@ -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; @@ -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() { @@ -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 ""; } diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/controller/CustomerController.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/controller/CustomerController.java index fc66ce8..93f188e 100644 --- a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/controller/CustomerController.java +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/controller/CustomerController.java @@ -1,10 +1,12 @@ 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; @@ -12,13 +14,16 @@ 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"; @@ -26,7 +31,7 @@ public String index() { @Operation(summary = "Buscar cliente por ID") @GetMapping("/{id}") - public ResponseEntity getById(@PathVariable Long id) throws BusinessException { + public ResponseEntity getById(@PathVariable Long id) throws BusinessException { return ResponseEntity.ok(customerService.findById(id)); } diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/dto/customer/CustomerSaveDTO.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/dto/customer/CustomerSaveDTO.java index a6b1772..1d70b85 100644 --- a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/dto/customer/CustomerSaveDTO.java +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/dto/customer/CustomerSaveDTO.java @@ -13,9 +13,6 @@ public record CustomerSaveDTO( @NotBlank String cpf, - @NotNull - boolean delayDevolution, - @Email String email ) { diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java index aeaba34..8661462 100644 --- a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java @@ -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; diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/mapper/customer/CustomerMapper.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/mapper/customer/CustomerMapper.java new file mode 100644 index 0000000..4112a36 --- /dev/null +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/mapper/customer/CustomerMapper.java @@ -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); + +} diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/service/customer/CustomerService.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/service/customer/CustomerService.java index 34bc348..7b06e72 100644 --- a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/service/customer/CustomerService.java +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/service/customer/CustomerService.java @@ -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); @@ -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 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); } diff --git a/VideoLocadora/src/main/resources/application.yaml b/VideoLocadora/src/main/resources/application.yaml index 5f92e36..0652d83 100644 --- a/VideoLocadora/src/main/resources/application.yaml +++ b/VideoLocadora/src/main/resources/application.yaml @@ -31,7 +31,11 @@ springdoc: api-docs: path: /api-docs - + docker: + compose: + enabled: true + file: docker-compose.yml + profiles: development --- spring: config: @@ -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: diff --git a/VideoLocadora/src/main/resources/data.sql b/VideoLocadora/src/main/resources/data.sql index 4161235..b652f1e 100644 --- a/VideoLocadora/src/main/resources/data.sql +++ b/VideoLocadora/src/main/resources/data.sql @@ -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');