diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..5b81eda --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,21 @@ +name: Google Java Format +on: + push: + branches: ["**"] + pull_request: + branches: [master, main, develop] +jobs: + format: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: "17" + distribution: "adopt" + + - name: Run Google Java Format + run: mvn spotless:apply diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..353d256 --- /dev/null +++ b/README.md @@ -0,0 +1,117 @@ +# Granja de Patos + +Este projeto é um serviço Spring Boot para o desafio "Granja de Patos" proposto pela empresa Preço Justo. + +## Índice + +- [Introdução](#granja-de-patos) +- [Tecnologias Utilizadas](#tecnologias-utilizadas) +- [MER - Modelo Entidade Relacionamento](#mer---modelo-entidade-relacionamento) +- [Cronograma](#cronograma) +- [Pré-requisitos](#pré-requisitos) +- [Executando o Projeto](#executando-o-projeto) + - [Sem Docker](#sem-docker) + - [Com Docker](#com-docker) + +## Tecnologias Utilizadas + +- Spring Boot +- Java 17 +- Swagger para documentação da API +- GitHub Actions para CI/CD + +## MER - Modelo Entidade Relacionamento + +

+ Modelo Entidade Relacionamento +

+ +## Cronograma + +| Atividade | Tempo | Descrição | +| -------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| Configuração inicial | 1 hora | Configuração do ambiente de desenvolvimento, incluindo dependências, Swagger, tratamento global de exceções, e integração contínua com GitHub Actions. | +| Adição das entidades e relacionamentos | 1 hora | Análise e implementação das entidades do domínio e seus relacionamentos, com foco na integridade e validação dos dados. | +| Adição dos controladores e services | 5 horas | Desenvolvimento dos componentes de lógica de negócios (serviços) e interfaces de comunicação (controladores) para as entidades Cliente, Pato e Venda, incluindo tratamento adequado de exceções. | +| Gerar relatório em Planilha | 4 horas | Implementação da lógica para geração de relatórios em formato de planilha, seguindo critérios específicos de hierarquia do relacionamento dos Patos. | +| Gerar relatório em PDF | 4 horas | Configuração do layout e desenvolvimento da funcionalidade para exportação de relatórios no formato PDF, utilizando o template. | +| Refatoração e testes | 4 horas | Revisão e melhoria do código para garantir legibilidade e conformidade com boas práticas, além da execução de testes para validar as funcionalidades implementadas. | + +Para acessar o relatório completo em formato Excel, [clique aqui](docs/cronograma.xlsx). + +## Pré-requisitos + +- Java 17 ou superior +- Maven 3.6.3 ou superior +- Docker (opcional, para rodar com Docker) + +## Executando o Projeto + +### Sem Docker + +1. Clone o repositório: + + ```sh + git clone https://github.com/brenogonzaga/desafio-preco-justo.git + cd desafio-preco-justo + ``` + +2. Compile o projeto e baixe as dependências: + + ```sh + mvn clean install + ``` + +3. Configura as variáveis de ambiente: + + Crie um arquivo chamado `application.properties` na pasta `src/main/resources` com as seguintes configurações: + + ```sh + spring.datasource.url=jdbc:postgresql://localhost:5432/seu_banco_de_dados + spring.datasource.username=seu_usuario + spring.datasource.password=sua_senha + spring.datasource.driverClassName=org.postgresql.Driver + ``` + +4. Execute o projeto: + + ```sh + mvn spring-boot:run + ``` + +5. O serviço estará disponível em `http://localhost:8080`. + +### Com Docker + +1. Clone o repositório: + + ```sh + git clone https://github.com/brenogonzaga/desafio-preco-justo.git + cd desafio-preco-justo + ``` + +2. Construa a imagem Docker: + + ```sh + docker build -t relatorio-patos . + ``` + +3. Execute o container Docker: + + ```sh + docker run -p 8080:8080 relatorio-patos + ``` + +4. Compile o projeto e baixe as dependências: + + ```sh + mvn clean install + ``` + +5. Execute o projeto: + + ```sh + mvn spring-boot:run + ``` + +6. O serviço estará disponível em `http://localhost:8080`. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..199fd6c --- /dev/null +++ b/compose.yaml @@ -0,0 +1,9 @@ +services: + postgres: + image: "postgres:latest" + environment: + - "POSTGRES_DB=mydatabase" + - "POSTGRES_PASSWORD=secret" + - "POSTGRES_USER=myuser" + ports: + - "5432:5432" diff --git a/docs/api-docs.json b/docs/api-docs.json new file mode 100644 index 0000000..263b693 --- /dev/null +++ b/docs/api-docs.json @@ -0,0 +1,952 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "Granja de Patos", + "description": "API Granja de Patos", + "version": "1.0" + }, + "servers": [ + { + "url": "http://localhost:8080", + "description": "Generated server url" + } + ], + "paths": { + "/patos/{id}": { + "get": { + "tags": [ + "Patos" + ], + "operationId": "getPatoById", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/PatoResponseDTO" + } + } + } + } + } + }, + "put": { + "tags": [ + "Patos" + ], + "operationId": "updatePato", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PatoRequestDTO" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/PatoResponseDTO" + } + } + } + } + } + }, + "delete": { + "tags": [ + "Patos" + ], + "operationId": "deletePato", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/clientes/{id}": { + "get": { + "tags": [ + "Clientes" + ], + "operationId": "getClienteById", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/ClienteResponseDTO" + } + } + } + } + } + }, + "put": { + "tags": [ + "Clientes" + ], + "operationId": "updateCliente", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ClienteRequestDTO" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/ClienteResponseDTO" + } + } + } + } + } + }, + "delete": { + "tags": [ + "Clientes" + ], + "operationId": "deleteCliente", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/vendas": { + "get": { + "tags": [ + "Vendas" + ], + "operationId": "getAllVendas", + "parameters": [ + { + "name": "page", + "in": "query", + "required": false, + "schema": { + "type": "integer", + "format": "int32", + "default": 0 + } + }, + { + "name": "size", + "in": "query", + "required": false, + "schema": { + "type": "integer", + "format": "int32", + "default": 10 + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/PageVendaResponseDTO" + } + } + } + } + } + }, + "post": { + "tags": [ + "Vendas" + ], + "operationId": "createVenda", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/VendaRequestDTO" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/VendaResponseDTO" + } + } + } + } + } + } + }, + "/patos": { + "get": { + "tags": [ + "Patos" + ], + "operationId": "getAllPatos", + "parameters": [ + { + "name": "page", + "in": "query", + "required": false, + "schema": { + "type": "integer", + "format": "int32", + "default": 0 + } + }, + { + "name": "size", + "in": "query", + "required": false, + "schema": { + "type": "integer", + "format": "int32", + "default": 10 + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/PagePatoResponseDTO" + } + } + } + } + } + }, + "post": { + "tags": [ + "Patos" + ], + "operationId": "createPato", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PatoRequestDTO" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/PatoResponseDTO" + } + } + } + } + } + } + }, + "/clientes": { + "get": { + "tags": [ + "Clientes" + ], + "operationId": "getAllClientes", + "parameters": [ + { + "name": "page", + "in": "query", + "required": false, + "schema": { + "type": "integer", + "format": "int32", + "default": 0 + } + }, + { + "name": "size", + "in": "query", + "required": false, + "schema": { + "type": "integer", + "format": "int32", + "default": 10 + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/PageClienteResponseDTO" + } + } + } + } + } + }, + "post": { + "tags": [ + "Clientes" + ], + "operationId": "createCliente", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ClienteRequestDTO" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/ClienteResponseDTO" + } + } + } + } + } + } + }, + "/auth/login": { + "post": { + "tags": [ + "Auth" + ], + "operationId": "login", + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "type": "string" + } + } + } + } + } + } + }, + "/vendas/{id}": { + "get": { + "tags": [ + "Vendas" + ], + "operationId": "getVendaById", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/VendaResponseDTO" + } + } + } + } + } + } + }, + "/relatorio": { + "get": { + "tags": [ + "Relatórios" + ], + "operationId": "obterPatosOrganizados", + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/RelatorioResponseDTO" + } + } + } + } + } + } + } + }, + "/relatorio/pdf": { + "get": { + "tags": [ + "Relatórios" + ], + "operationId": "relatorioPdf", + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "type": "array", + "items": { + "type": "string", + "format": "byte" + } + } + } + } + } + } + } + }, + "/relatorio/gerar": { + "get": { + "tags": [ + "Relatórios" + ], + "operationId": "gerarRelatorio", + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "type": "array", + "items": { + "type": "string", + "format": "byte" + } + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "PatoRequestDTO": { + "type": "object", + "properties": { + "nome": { + "type": "string" + }, + "maeId": { + "type": "integer", + "format": "int64" + } + } + }, + "PatoResponseDTO": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "mae": { + "type": "string" + }, + "nome": { + "type": "string" + }, + "preco": { + "type": "number", + "format": "double" + } + } + }, + "ClienteRequestDTO": { + "required": [ + "cpf", + "email", + "nome", + "password" + ], + "type": "object", + "properties": { + "nome": { + "type": "string" + }, + "cpf": { + "type": "string" + }, + "email": { + "type": "string" + }, + "elegivelDesconto": { + "type": "boolean" + }, + "password": { + "type": "string" + }, + "enderecos": { + "type": "array", + "items": { + "$ref": "#/components/schemas/EnderecoDTO" + } + }, + "telefones": { + "type": "array", + "items": { + "$ref": "#/components/schemas/TelefoneDTO" + } + } + } + }, + "EnderecoDTO": { + "type": "object", + "properties": { + "logradouro": { + "type": "string" + }, + "numero": { + "type": "string" + }, + "complemento": { + "type": "string" + }, + "bairro": { + "type": "string" + }, + "cidade": { + "type": "string" + }, + "estado": { + "type": "string" + }, + "cep": { + "type": "string" + }, + "pais": { + "type": "string" + } + } + }, + "TelefoneDTO": { + "type": "object", + "properties": { + "tipo": { + "type": "string", + "enum": [ + "RESIDENCIAL", + "COMERCIAL", + "CELULAR" + ] + }, + "ddd": { + "type": "integer", + "format": "int32" + }, + "numero": { + "type": "string" + } + } + }, + "ClienteResponseDTO": { + "required": [ + "cpf", + "email", + "nome" + ], + "type": "object", + "properties": { + "nome": { + "type": "string" + }, + "cpf": { + "type": "string" + }, + "email": { + "type": "string" + }, + "id": { + "type": "integer", + "format": "int64" + }, + "ativo": { + "type": "boolean" + }, + "elegivelDesconto": { + "type": "boolean" + } + } + }, + "VendaRequestDTO": { + "type": "object", + "properties": { + "clienteId": { + "type": "integer", + "format": "int64" + }, + "patosId": { + "type": "array", + "items": { + "type": "integer", + "format": "int64" + } + } + } + }, + "PatoVendidosDTO": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "nome": { + "type": "string" + }, + "preco": { + "type": "number", + "format": "double" + } + } + }, + "VendaResponseDTO": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "cliente": { + "$ref": "#/components/schemas/ClienteResponseDTO" + }, + "pato": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PatoVendidosDTO" + } + }, + "dataVenda": { + "type": "string", + "format": "date-time" + }, + "valorTotal": { + "type": "number", + "format": "double" + }, + "quantidadePatos": { + "type": "integer", + "format": "int32" + } + } + }, + "PageVendaResponseDTO": { + "type": "object", + "properties": { + "totalPages": { + "type": "integer", + "format": "int32" + }, + "totalElements": { + "type": "integer", + "format": "int64" + }, + "size": { + "type": "integer", + "format": "int32" + }, + "content": { + "type": "array", + "items": { + "$ref": "#/components/schemas/VendaResponseDTO" + } + }, + "number": { + "type": "integer", + "format": "int32" + }, + "sort": { + "$ref": "#/components/schemas/SortObject" + }, + "first": { + "type": "boolean" + }, + "last": { + "type": "boolean" + }, + "numberOfElements": { + "type": "integer", + "format": "int32" + }, + "pageable": { + "$ref": "#/components/schemas/PageableObject" + }, + "empty": { + "type": "boolean" + } + } + }, + "PageableObject": { + "type": "object", + "properties": { + "offset": { + "type": "integer", + "format": "int64" + }, + "sort": { + "$ref": "#/components/schemas/SortObject" + }, + "pageSize": { + "type": "integer", + "format": "int32" + }, + "paged": { + "type": "boolean" + }, + "pageNumber": { + "type": "integer", + "format": "int32" + }, + "unpaged": { + "type": "boolean" + } + } + }, + "SortObject": { + "type": "object", + "properties": { + "empty": { + "type": "boolean" + }, + "sorted": { + "type": "boolean" + }, + "unsorted": { + "type": "boolean" + } + } + }, + "RelatorioResponseDTO": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "nome": { + "type": "string" + }, + "status": { + "type": "string" + }, + "cliente": { + "type": "string" + }, + "tipoCliente": { + "type": "string" + }, + "valor": { + "type": "number", + "format": "double" + }, + "nivel": { + "type": "integer", + "format": "int32" + } + } + }, + "PagePatoResponseDTO": { + "type": "object", + "properties": { + "totalPages": { + "type": "integer", + "format": "int32" + }, + "totalElements": { + "type": "integer", + "format": "int64" + }, + "size": { + "type": "integer", + "format": "int32" + }, + "content": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PatoResponseDTO" + } + }, + "number": { + "type": "integer", + "format": "int32" + }, + "sort": { + "$ref": "#/components/schemas/SortObject" + }, + "first": { + "type": "boolean" + }, + "last": { + "type": "boolean" + }, + "numberOfElements": { + "type": "integer", + "format": "int32" + }, + "pageable": { + "$ref": "#/components/schemas/PageableObject" + }, + "empty": { + "type": "boolean" + } + } + }, + "PageClienteResponseDTO": { + "type": "object", + "properties": { + "totalPages": { + "type": "integer", + "format": "int32" + }, + "totalElements": { + "type": "integer", + "format": "int64" + }, + "size": { + "type": "integer", + "format": "int32" + }, + "content": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ClienteResponseDTO" + } + }, + "number": { + "type": "integer", + "format": "int32" + }, + "sort": { + "$ref": "#/components/schemas/SortObject" + }, + "first": { + "type": "boolean" + }, + "last": { + "type": "boolean" + }, + "numberOfElements": { + "type": "integer", + "format": "int32" + }, + "pageable": { + "$ref": "#/components/schemas/PageableObject" + }, + "empty": { + "type": "boolean" + } + } + } + } + } +} \ No newline at end of file diff --git a/docs/cronograma.xlsx b/docs/cronograma.xlsx new file mode 100644 index 0000000..1bba29c Binary files /dev/null and b/docs/cronograma.xlsx differ diff --git a/docs/diagrama.drawio.png b/docs/diagrama.drawio.png new file mode 100644 index 0000000..a5253e7 Binary files /dev/null and b/docs/diagrama.drawio.png differ diff --git a/mvnw b/mvnw new file mode 100644 index 0000000..d7c358e --- /dev/null +++ b/mvnw @@ -0,0 +1,259 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.2 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/mvnw.cmd b/mvnw.cmd new file mode 100644 index 0000000..6f779cf --- /dev/null +++ b/mvnw.cmd @@ -0,0 +1,149 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.2 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' +$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" +if ($env:MAVEN_USER_HOME) { + $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" +} +$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f6ea963 --- /dev/null +++ b/pom.xml @@ -0,0 +1,173 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.1 + + + br.app.precojusto + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-hateoas + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-websocket + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + + org.postgresql + postgresql + runtime + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + pom + + + org.springframework.boot + spring-boot-starter-test + 2.4.0 + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.springframework.security + spring-security-test + test + + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.3.0 + + + + + org.apache.poi + poi-ooxml + 5.2.2 + + + + + net.sf.jasperreports + jasperreports + 6.17.0 + + + + org.hibernate.validator + hibernate-validator + 6.2.0.Final + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + com.diffplug.spotless + spotless-maven-plugin + 2.43.0 + + + + + + src/*/java/**/*.java + + + + + 3.2.5 + 2.6.0 + + + 4 + 110 + java + prettier-plugin-java + + + + + + + + + + diff --git a/src/main/java/br/app/precojusto/BackendApplication.java b/src/main/java/br/app/precojusto/BackendApplication.java new file mode 100644 index 0000000..1273a8f --- /dev/null +++ b/src/main/java/br/app/precojusto/BackendApplication.java @@ -0,0 +1,15 @@ +package br.app.precojusto; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.info.Info; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@OpenAPIDefinition(info = @Info(title = "Granja de Patos", version = "1.0", description = "API Granja de Patos")) +public class BackendApplication { + + public static void main(String[] args) { + SpringApplication.run(BackendApplication.class, args); + } +} diff --git a/src/main/java/br/app/precojusto/Seeds.java b/src/main/java/br/app/precojusto/Seeds.java new file mode 100644 index 0000000..3a9c9b0 --- /dev/null +++ b/src/main/java/br/app/precojusto/Seeds.java @@ -0,0 +1,109 @@ +package br.app.precojusto; + +import br.app.precojusto.models.*; +import br.app.precojusto.models.Telefone.TipoTelefone; +import br.app.precojusto.repositories.*; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +@Component +public class Seeds { + + @Autowired + private ClienteRepository clienteRepository; + + @Autowired + private EnderecoRepository enderecoRepository; + + @Autowired + private TelefoneRepository telefoneRepository; + + @Autowired + private PatoRepository patoRepository; + + @EventListener + public void onApplicationReady(ApplicationReadyEvent event) { + seedClientes(); + seedPatos(); + } + + private void seedClientes() { + for (int i = 0; i < 5; i++) { + Cliente cliente = Cliente.builder(). + nome("Cliente " + i). + cpf("99846249519"). + email("cliente" + i + "@example.com"). + hashedPassword("123"). + elegivelDesconto(i % 2 == 0). // Alternando entre elegível ou não para desconto + ativo(true). + build(); + clienteRepository.save(cliente); + + // Adicionando endereço para o cliente + Endereco endereco = Endereco.builder(). + logradouro("Rua Exemplo " + i). + numero("" + (100 + i)). + complemento("Apto " + i). + bairro("Bairro " + i). + cidade("Cidade " + i). + estado("Estado " + i). + cep("12345-67" + i). + pais("Brasil"). + cliente(cliente). + build(); + + + + enderecoRepository.save(endereco); + + // Adicionando telefone para o cliente + Telefone telefone = Telefone.builder(). + tipo(TipoTelefone.CELULAR). + ddd(11). + numero("9876543" + i). + cliente(cliente). + build(); + telefoneRepository.save(telefone); + } + } + + private int getLineageDepth(Pato pato) { + int depth = 0; + Pato current = pato; + while (current.getMae() != null) { + depth++; + current = current.getMae(); + if (depth >= 2) break; + } + return depth; + } + + private void seedPatos() { + Random random = new Random(); + List possiveisMaes = patoRepository + .findAll() + .stream() + .filter(pato -> getLineageDepth(pato) < 2) + .collect(Collectors.toList()); + + for (int i = 0; i < 10; i++) { + Pato pato = new Pato(); + pato.setNome("Pato Extra " + i); + if (!possiveisMaes.isEmpty()) { + int indiceAleatorio = random.nextInt(possiveisMaes.size()); + Pato maeAleatoria = possiveisMaes.get(indiceAleatorio); + pato.setMae(maeAleatoria); + } + + patoRepository.save(pato); + if (getLineageDepth(pato) < 2) { + possiveisMaes.add(pato); + } + } + } +} diff --git a/src/main/java/br/app/precojusto/config/SecurityConfig.java b/src/main/java/br/app/precojusto/config/SecurityConfig.java new file mode 100644 index 0000000..fdbae92 --- /dev/null +++ b/src/main/java/br/app/precojusto/config/SecurityConfig.java @@ -0,0 +1,34 @@ +package br.app.precojusto.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.web.cors.CorsConfiguration; + +@Configuration +@EnableWebSecurity +public class SecurityConfig { + + @Bean + SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { + // TODO: estabelecer configurações de segurança + CorsConfiguration corsConfiguration = new CorsConfiguration(); + corsConfiguration.addAllowedOriginPattern("*"); + corsConfiguration.addAllowedHeader("*"); + corsConfiguration.addAllowedMethod("*"); + corsConfiguration.addExposedHeader("*"); + httpSecurity + .cors(cors -> cors.configurationSource(request -> corsConfiguration)) + .csrf(csrf -> csrf.disable()); + return httpSecurity.build(); + } + // @Bean + // AuthenticationManager authenticationManager( + // AuthenticationConfiguration authConfig + // ) + // throws Exception { + // return authConfig.getAuthenticationManager(); + // } +} diff --git a/src/main/java/br/app/precojusto/config/SwaggerConfig.java b/src/main/java/br/app/precojusto/config/SwaggerConfig.java new file mode 100644 index 0000000..deb207f --- /dev/null +++ b/src/main/java/br/app/precojusto/config/SwaggerConfig.java @@ -0,0 +1,35 @@ +package br.app.precojusto.config; + +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.security.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SwaggerConfig { + + @Bean + public OpenAPI customOpenAPI() { + return new OpenAPI() + .components( + new Components() + // .addSecuritySchemes( + // "bearerAuth", + // new SecurityScheme() + // .type(SecurityScheme.Type.HTTP) + // .scheme("bearer") + // .bearerFormat("JWT") + // ) + ) + .info( + new Info() + .title("API Granja de Patos") + .version("1.0") + .description("Api para o desafio Granja de Patos") + ); + // .addSecurityItem(new SecurityRequirement().addList("bearerAuth")); + } +} diff --git a/src/main/java/br/app/precojusto/controllers/AuthController.java b/src/main/java/br/app/precojusto/controllers/AuthController.java new file mode 100644 index 0000000..dbd7ca4 --- /dev/null +++ b/src/main/java/br/app/precojusto/controllers/AuthController.java @@ -0,0 +1,18 @@ +package br.app.precojusto.controllers; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.v3.oas.annotations.tags.Tag; + +@RestController +@RequestMapping("/auth") +@Tag(name = "Auth") +public class AuthController { + + @PostMapping("/login") + public String login() { + return "Login realizado com sucesso!"; + } +} diff --git a/src/main/java/br/app/precojusto/controllers/ClienteController.java b/src/main/java/br/app/precojusto/controllers/ClienteController.java new file mode 100644 index 0000000..ddbd818 --- /dev/null +++ b/src/main/java/br/app/precojusto/controllers/ClienteController.java @@ -0,0 +1,62 @@ +package br.app.precojusto.controllers; + +import br.app.precojusto.exceptions.BadRequest; +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Cliente; +import br.app.precojusto.models.dto.request.ClienteRequestDTO; +import br.app.precojusto.models.dto.response.ClienteResponseDTO; +import br.app.precojusto.services.ClienteService; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/clientes") +@Tag(name = "Clientes") +public class ClienteController { + + @Autowired + private ClienteService clienteService; + + @PostMapping + public ResponseEntity createCliente(@Valid @RequestBody ClienteRequestDTO clienteDTO) + throws BadRequest, NotFound { + ClienteResponseDTO responseClienteDTO = clienteService.save(clienteDTO); + return ResponseEntity.created(null).body(responseClienteDTO); + } + + @GetMapping + public ResponseEntity> getAllClientes( + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size + ) { + Page responseClienteDTO = clienteService.findAll(page, size); + return ResponseEntity.ok(responseClienteDTO); + } + + @GetMapping("/{id}") + public ResponseEntity getClienteById(@PathVariable Long id) throws NotFound { + Cliente cliente = clienteService.findById(id); + ClienteResponseDTO responseClienteDTO = ClienteResponseDTO.fromCliente(cliente); + return ResponseEntity.ok(responseClienteDTO); + } + + @PutMapping("/{id}") + public ResponseEntity updateCliente( + @PathVariable Long id, + @Valid @RequestBody ClienteRequestDTO clienteDTO + ) throws NotFound, BadRequest { + Cliente cliente = clienteService.update(id, clienteDTO); + ClienteResponseDTO responseClienteDTO = ClienteResponseDTO.fromCliente(cliente); + return ResponseEntity.ok(responseClienteDTO); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteCliente(@PathVariable Long id) throws NotFound { + clienteService.delete(id); + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/br/app/precojusto/controllers/PatoController.java b/src/main/java/br/app/precojusto/controllers/PatoController.java new file mode 100644 index 0000000..e3046b8 --- /dev/null +++ b/src/main/java/br/app/precojusto/controllers/PatoController.java @@ -0,0 +1,59 @@ +package br.app.precojusto.controllers; + +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Pato; +import br.app.precojusto.models.dto.request.PatoRequestDTO; +import br.app.precojusto.models.dto.response.PatoResponseDTO; +import br.app.precojusto.services.PatoService; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/patos") +@Tag(name = "Patos") +public class PatoController { + + @Autowired + private PatoService patoService; + + @PostMapping + public ResponseEntity createPato(@Valid @RequestBody PatoRequestDTO pato) throws NotFound { + Pato savedPato = patoService.save(pato); + PatoResponseDTO responseDTO = PatoResponseDTO.fromPato(savedPato); + return ResponseEntity.ok(responseDTO); + } + + @GetMapping + public ResponseEntity> getAllPatos( + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size + ) { + Page patos = patoService.findAll(page, size); + Page responseDTO = PatoResponseDTO.fromPato(patos); + return ResponseEntity.ok(responseDTO); + } + + @GetMapping("/{id}") + public ResponseEntity getPatoById(@PathVariable Long id) throws NotFound { + Pato pato = patoService.findById(id); + PatoResponseDTO responseDTO = PatoResponseDTO.fromPato(pato); + return ResponseEntity.ok(responseDTO); + } + + @PutMapping("/{id}") + public ResponseEntity updatePato(@PathVariable Long id, @Valid @RequestBody PatoRequestDTO pato) + throws NotFound { + PatoResponseDTO responseDTO = PatoResponseDTO.fromPato(patoService.update(id, pato)); + return ResponseEntity.ok(responseDTO); + } + + @DeleteMapping("/{id}") + public void deletePato(@PathVariable Long id) throws NotFound { + patoService.delete(id); + } +} diff --git a/src/main/java/br/app/precojusto/controllers/RelatorioController.java b/src/main/java/br/app/precojusto/controllers/RelatorioController.java new file mode 100644 index 0000000..25f5ed0 --- /dev/null +++ b/src/main/java/br/app/precojusto/controllers/RelatorioController.java @@ -0,0 +1,154 @@ +package br.app.precojusto.controllers; + +import br.app.precojusto.models.dto.response.RelatorioResponseDTO; +import br.app.precojusto.services.PatoService; +import io.swagger.v3.oas.annotations.tags.Tag; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JasperCompileManager; +import net.sf.jasperreports.engine.JasperExportManager; +import net.sf.jasperreports.engine.JasperFillManager; +import net.sf.jasperreports.engine.JasperPrint; +import net.sf.jasperreports.engine.JasperReport; +import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/relatorio") +@Tag(name = "Relatórios") +public class RelatorioController { + + @Autowired + private PatoService patoService; + + @Autowired + private ResourceLoader resourceLoader; + + @GetMapping("/gerar") + public ResponseEntity gerarRelatorio() throws IOException { + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("Relatório de Patos"); + int rowNum = 0; + + Row headerRow = sheet.createRow(rowNum++); + Cell cell = headerRow.createCell(0); + cell.setCellValue("Nome"); + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2)); + headerRow.createCell(3).setCellValue("Status"); + headerRow.createCell(4).setCellValue("Cliente"); + headerRow.createCell(5).setCellValue("Tipo de Cliente"); + headerRow.createCell(6).setCellValue("Valor"); + + List patosOrganizados = patoService.obterPatosOrganizados(); + + for (RelatorioResponseDTO pato : patosOrganizados) { + rowNum = preencherLinha(sheet, pato, rowNum, 0); + } + + for (int i = 0; i <= 6; i++) { + sheet.autoSizeColumn(i); + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + workbook.write(baos); + workbook.close(); + + HttpHeaders headersResponse = new HttpHeaders(); + headersResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM); + headersResponse.setContentDispositionFormData("filename", "relatorio_patos.xlsx"); + + return new ResponseEntity<>(baos.toByteArray(), headersResponse, HttpStatus.OK); + } + + private int preencherLinha(Sheet sheet, RelatorioResponseDTO pato, int rowNum, int nivel) { + Row row = sheet.createRow(rowNum++); + + // Preenche o nome de acordo com o nível hierárquico + if (nivel == 0) { + row.createCell(0).setCellValue(pato.getNome()); + } else if (nivel == 1) { + row.createCell(1).setCellValue(pato.getNome()); + } else if (nivel == 2) { + row.createCell(2).setCellValue(pato.getNome()); + } + + // Preenche as outras colunas com os dados do pato + row.createCell(3).setCellValue(pato.getStatus()); + row.createCell(4).setCellValue(pato.getCliente()); + row.createCell(5).setCellValue(pato.getTipoCliente()); + row.createCell(6).setCellValue(pato.getValor()); + + // Chama recursivamente para preencher os filhos, aumentando o nível hierárquico + for (RelatorioResponseDTO filho : pato.getFilhos()) { + rowNum = preencherLinha(sheet, filho, rowNum, nivel + 1); + } + + return rowNum; + } + + @GetMapping("/pdf") + public ResponseEntity relatorioPdf() throws JRException, IOException { + byte[] relatorioBytes = gerarRelatorioPDF(); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_PDF); + headers.setContentDispositionFormData("filename", "relatorio_patos.pdf"); + + return new ResponseEntity<>(relatorioBytes, headers, HttpStatus.OK); + } + + private List organizarPatosParaRelatorio(List patos) { + List todosPatos = new ArrayList<>(); + for (RelatorioResponseDTO pato : patos) { + adicionarPatoETodosFilhos(todosPatos, pato, 0); + } + return todosPatos; + } + + private void adicionarPatoETodosFilhos(List lista, RelatorioResponseDTO pato, int nivel) { + pato.setNivel(nivel); // Adiciona o nível ao PatoDTO + lista.add(pato); + for (RelatorioResponseDTO filho : pato.getFilhos()) { + adicionarPatoETodosFilhos(lista, filho, nivel + 1); + } + } + + public byte[] gerarRelatorioPDF() throws JRException, IOException { + Resource resource = resourceLoader.getResource("classpath:PDF.jrxml"); + InputStream inputStream = resource.getInputStream(); + + JasperReport jasperReport = JasperCompileManager.compileReport(inputStream); + + List dados = patoService.obterPatosOrganizados(); + List dadosParaRelatorio = organizarPatosParaRelatorio(dados); + + JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(dadosParaRelatorio); + + JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<>(), dataSource); + + return JasperExportManager.exportReportToPdf(jasperPrint); + } + + @GetMapping + public ResponseEntity> obterPatosOrganizados() { + return ResponseEntity.ok(patoService.obterPatosOrganizados()); + } +} diff --git a/src/main/java/br/app/precojusto/controllers/VendaController.java b/src/main/java/br/app/precojusto/controllers/VendaController.java new file mode 100644 index 0000000..cd086c5 --- /dev/null +++ b/src/main/java/br/app/precojusto/controllers/VendaController.java @@ -0,0 +1,48 @@ +package br.app.precojusto.controllers; + +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Venda; +import br.app.precojusto.models.dto.request.VendaRequestDTO; +import br.app.precojusto.models.dto.response.VendaResponseDTO; +import br.app.precojusto.services.VendaService; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/vendas") +@Tag(name = "Vendas") +public class VendaController { + + @Autowired + private VendaService vendaService; + + @PostMapping + public ResponseEntity createVenda(@Valid @RequestBody VendaRequestDTO createVendaDTO) + throws NotFound { + Venda vendaRealizada = vendaService.create( + createVendaDTO.getClienteId(), + createVendaDTO.getPatosId() + ); + return ResponseEntity.ok(VendaResponseDTO.fromVendaPato(vendaRealizada)); + } + + @GetMapping + public ResponseEntity> getAllVendas( + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size + ) { + Page venda = vendaService.findAll(page, size); + return ResponseEntity.ok(VendaResponseDTO.fromVendaPato(venda)); + } + + @GetMapping("/{id}") + public ResponseEntity getVendaById(@PathVariable Long id) throws NotFound { + Venda venda = vendaService.findById(id); + return ResponseEntity.ok(VendaResponseDTO.fromVendaPato(venda)); + } +} diff --git a/src/main/java/br/app/precojusto/exceptions/BadRequest.java b/src/main/java/br/app/precojusto/exceptions/BadRequest.java new file mode 100644 index 0000000..7d501ec --- /dev/null +++ b/src/main/java/br/app/precojusto/exceptions/BadRequest.java @@ -0,0 +1,12 @@ +package br.app.precojusto.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.BAD_REQUEST) +public class BadRequest extends Exception { + + public BadRequest(String message) { + super(message); + } +} diff --git a/src/main/java/br/app/precojusto/exceptions/ErrorResponse.java b/src/main/java/br/app/precojusto/exceptions/ErrorResponse.java new file mode 100644 index 0000000..3ade99c --- /dev/null +++ b/src/main/java/br/app/precojusto/exceptions/ErrorResponse.java @@ -0,0 +1,22 @@ +package br.app.precojusto.exceptions; + +import lombok.Data; + +@Data +public class ErrorResponse { + + private Long timestamp; + private Integer status; + private String error; + private String message; + private String path; + + public ErrorResponse(Long timestamp, Integer status, String error, String message, String path) { + super(); + this.timestamp = timestamp; + this.status = status; + this.error = error; + this.message = message; + this.path = path; + } +} diff --git a/src/main/java/br/app/precojusto/exceptions/ErrorType.java b/src/main/java/br/app/precojusto/exceptions/ErrorType.java new file mode 100644 index 0000000..55fae77 --- /dev/null +++ b/src/main/java/br/app/precojusto/exceptions/ErrorType.java @@ -0,0 +1,17 @@ +package br.app.precojusto.exceptions; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum ErrorType { + NOT_FOUND("NOT_FOUND", "Resource not found"), + FORBIDDEN("FORBIDDEN", "Access denied"), + UNAUTHORIZED_CLIENT("UNAUTHORIZED_CLIENT", "Unauthorized client"), + BAD_REQUEST("BAD_REQUEST", "Bad request"), + INTERNAL_SERVER_ERROR("INTERNAL_SERVER_ERROR", "Internal server error"); + + private String errorType; + private String description; +} diff --git a/src/main/java/br/app/precojusto/exceptions/Forbidden.java b/src/main/java/br/app/precojusto/exceptions/Forbidden.java new file mode 100644 index 0000000..046ee2a --- /dev/null +++ b/src/main/java/br/app/precojusto/exceptions/Forbidden.java @@ -0,0 +1,12 @@ +package br.app.precojusto.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.FORBIDDEN) +public class Forbidden extends Exception { + + public Forbidden(String message) { + super(message); + } +} diff --git a/src/main/java/br/app/precojusto/exceptions/NotFound.java b/src/main/java/br/app/precojusto/exceptions/NotFound.java new file mode 100644 index 0000000..7670e17 --- /dev/null +++ b/src/main/java/br/app/precojusto/exceptions/NotFound.java @@ -0,0 +1,18 @@ +package br.app.precojusto.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +public class NotFound extends Exception { + + private static final String DEFAULT_MSG = "Recurso não encontrado!"; + + public NotFound() { + super(DEFAULT_MSG); + } + + public NotFound(String message) { + super(message); + } +} diff --git a/src/main/java/br/app/precojusto/exceptions/handler/ExceptionsHandler.java b/src/main/java/br/app/precojusto/exceptions/handler/ExceptionsHandler.java new file mode 100644 index 0000000..c69aa5f --- /dev/null +++ b/src/main/java/br/app/precojusto/exceptions/handler/ExceptionsHandler.java @@ -0,0 +1,52 @@ +package br.app.precojusto.exceptions.handler; + +import br.app.precojusto.exceptions.BadRequest; +import br.app.precojusto.exceptions.ErrorResponse; +import br.app.precojusto.exceptions.ErrorType; +import br.app.precojusto.exceptions.Forbidden; +import br.app.precojusto.exceptions.NotFound; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class ExceptionsHandler { + + @ExceptionHandler(BadRequest.class) + public ResponseEntity handlerBadRequest(BadRequest e, HttpServletRequest request) { + var err = new ErrorResponse( + System.currentTimeMillis(), + HttpStatus.BAD_REQUEST.value(), + ErrorType.BAD_REQUEST.getErrorType(), + e.getMessage(), + request.getRequestURI() + ); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(err); + } + + @ExceptionHandler(Forbidden.class) + public ResponseEntity handlerFobbiden(Forbidden e, HttpServletRequest request) { + var err = new ErrorResponse( + System.currentTimeMillis(), + HttpStatus.FORBIDDEN.value(), + ErrorType.FORBIDDEN.getErrorType(), + e.getMessage(), + request.getRequestURI() + ); + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(err); + } + + @ExceptionHandler(NotFound.class) + public ResponseEntity handlerNotFound(NotFound e, HttpServletRequest request) { + var err = new ErrorResponse( + System.currentTimeMillis(), + HttpStatus.NOT_FOUND.value(), + ErrorType.NOT_FOUND.getErrorType(), + e.getMessage(), + request.getRequestURI() + ); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(err); + } +} diff --git a/src/main/java/br/app/precojusto/exceptions/handler/GlobalExceptionHandler.java b/src/main/java/br/app/precojusto/exceptions/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..02fb911 --- /dev/null +++ b/src/main/java/br/app/precojusto/exceptions/handler/GlobalExceptionHandler.java @@ -0,0 +1,25 @@ +package br.app.precojusto.exceptions.handler; + +import br.app.precojusto.exceptions.ErrorResponse; +import br.app.precojusto.exceptions.ErrorType; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception e, HttpServletRequest request) { + var err = new ErrorResponse( + System.currentTimeMillis(), + HttpStatus.INTERNAL_SERVER_ERROR.value(), + ErrorType.INTERNAL_SERVER_ERROR.getErrorType(), + e.getMessage(), + request.getRequestURI() + ); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(err); + } +} diff --git a/src/main/java/br/app/precojusto/models/Cliente.java b/src/main/java/br/app/precojusto/models/Cliente.java new file mode 100644 index 0000000..3350bac --- /dev/null +++ b/src/main/java/br/app/precojusto/models/Cliente.java @@ -0,0 +1,54 @@ +package br.app.precojusto.models; + +import br.app.precojusto.utils.validation.CPF; +import jakarta.persistence.*; +import jakarta.validation.constraints.Email; +import java.util.List; +import java.util.Set; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Cliente { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(unique = false) + private String nome; + + @CPF(message = "CPF inválido") + @Column(unique = false) + private String cpf; // TODO: Mudar CPF para ser único + + @Email(message = "E-mail inválido") + @Column(unique = true) + private String email; + + @Column(name = "hashed_password", nullable = false) + private String hashedPassword; + + @Column(nullable = false) + private boolean elegivelDesconto; + + @Builder.Default + @Column(nullable = false) + private boolean ativo = true; + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "cliente", orphanRemoval = true) + private Set enderecos; + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "cliente", orphanRemoval = true) + private Set telefones; + + @OneToMany(mappedBy = "cliente") + private List compras; +} diff --git a/src/main/java/br/app/precojusto/models/Endereco.java b/src/main/java/br/app/precojusto/models/Endereco.java new file mode 100644 index 0000000..e54f8e2 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/Endereco.java @@ -0,0 +1,36 @@ +package br.app.precojusto.models; + +import jakarta.persistence.*; +import jakarta.validation.constraints.Pattern; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Endereco { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String logradouro; + private String numero; + private String complemento; + private String bairro; + private String cidade; + private String estado; + + @Pattern(regexp = "\\d{5}-?\\d{3}", message = "O CEP deve seguir o padrão 12345-678 ou 12345678") + private String cep; + + private String pais; + + @ManyToOne + @JoinColumn(name = "cliente_id") + private Cliente cliente; +} diff --git a/src/main/java/br/app/precojusto/models/Pato.java b/src/main/java/br/app/precojusto/models/Pato.java new file mode 100644 index 0000000..4f5929d --- /dev/null +++ b/src/main/java/br/app/precojusto/models/Pato.java @@ -0,0 +1,83 @@ +package br.app.precojusto.models; + +import com.fasterxml.jackson.annotation.JsonBackReference; +import jakarta.persistence.*; +import java.util.ArrayList; +import java.util.List; +import lombok.Data; + +@Entity +@Data +public class Pato { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String nome; + + @ManyToOne + @JoinColumn(name = "mae_id", nullable = true) + private Pato mae; + + private Double preco; + + @OneToMany(mappedBy = "mae") + @JsonBackReference + private List filhos; + + private boolean vendido; + + @ManyToOne + @JoinColumn(name = "venda_id") + @JsonBackReference + private Venda venda; + + // Método para adicionar um filho, garantindo que não ultrapasse dois filhos + public void adicionarFilho(Pato filho) { + if (this.filhos == null) { + this.filhos = new ArrayList<>(); + } + if (this.filhos.size() < 2) { + this.filhos.add(filho); + filho.setMae(this); + } else { + throw new IllegalStateException("Um pato não pode ter mais de dois filhos."); + } + } + + // Método para verificar a profundidade da linhagem + public static int getProfundidadeLinhagem(Pato pato) { + int profundidade = 0; + while (pato.getMae() != null) { + profundidade++; + pato = pato.getMae(); + if (profundidade >= 2) break; + } + return profundidade; + } + + // Método para definir a mãe, garantindo que a linhagem não ultrapasse dois níveis + public void setMae(Pato mae) { + if (getProfundidadeLinhagem(mae) < 2) { + this.mae = mae; + } else { + throw new IllegalStateException("Não é permitido ter uma linhagem maior que 'filho do filho'."); + } + } + + @PostLoad + @PostPersist + @PostUpdate + // Método para atualizar o preço do pato com base na quantidade de filhos + public void atualizarPreco() { + if (filhos == null || filhos.isEmpty()) { + this.preco = 70.00; + } else if (filhos.size() == 1) { + this.preco = 50.00; + } else { + this.preco = 25.00; + } + } +} diff --git a/src/main/java/br/app/precojusto/models/Telefone.java b/src/main/java/br/app/precojusto/models/Telefone.java new file mode 100644 index 0000000..9651d65 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/Telefone.java @@ -0,0 +1,41 @@ +package br.app.precojusto.models; + +import jakarta.persistence.*; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.Pattern; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Telefone { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private TipoTelefone tipo; + + @Min(value = 10, message = "O DDD deve ter exatamente 2 dígitos.") + @Max(value = 99, message = "O DDD deve ter exatamente 2 dígitos.") + private Integer ddd; + + @Pattern(regexp = "\\d{8,9}", message = "O número de telefone deve ter entre 8 e 9 dígitos.") + private String numero; + + @ManyToOne + @JoinColumn(name = "cliente_id") + private Cliente cliente; + + public enum TipoTelefone { + RESIDENCIAL, + COMERCIAL, + CELULAR, + } +} diff --git a/src/main/java/br/app/precojusto/models/Venda.java b/src/main/java/br/app/precojusto/models/Venda.java new file mode 100644 index 0000000..0b5af49 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/Venda.java @@ -0,0 +1,42 @@ +package br.app.precojusto.models; + +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonManagedReference; +import jakarta.persistence.*; +import java.util.Date; +import java.util.List; +import lombok.Data; + +@Entity +@Data +public class Venda { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "cliente_id") + @JsonBackReference + private Cliente cliente; + + @Temporal(TemporalType.TIMESTAMP) + private Date dataVenda; + + @Column(nullable = false) + private int quantidadePatos; + + private boolean desconto; + + @Column(nullable = false) + private Double valorTotal; + + @OneToMany(mappedBy = "venda", fetch = FetchType.EAGER) + @JsonManagedReference + private List patos; + + @PrePersist + protected void onCreate() { + dataVenda = new Date(); + } +} diff --git a/src/main/java/br/app/precojusto/models/dto/base/BaseClienteDTO.java b/src/main/java/br/app/precojusto/models/dto/base/BaseClienteDTO.java new file mode 100644 index 0000000..23b3170 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/base/BaseClienteDTO.java @@ -0,0 +1,21 @@ +package br.app.precojusto.models.dto.base; + +import br.app.precojusto.utils.validation.CPF; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +@Data +public class BaseClienteDTO { + + @NotBlank + private String nome; + + @NotBlank + @CPF(message = "CPF inválido") + private String cpf; + + @NotBlank + @Email(message = "Email inválido") + private String email; +} diff --git a/src/main/java/br/app/precojusto/models/dto/request/ClienteRequestDTO.java b/src/main/java/br/app/precojusto/models/dto/request/ClienteRequestDTO.java new file mode 100644 index 0000000..c5ea39d --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/request/ClienteRequestDTO.java @@ -0,0 +1,78 @@ +package br.app.precojusto.models.dto.request; + +import br.app.precojusto.models.Cliente; +import br.app.precojusto.models.Endereco; +import br.app.precojusto.models.Telefone; +import br.app.precojusto.models.Telefone.TipoTelefone; +import br.app.precojusto.models.dto.base.BaseClienteDTO; +import jakarta.validation.constraints.NotBlank; +import java.util.List; +import java.util.stream.Collectors; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +public class ClienteRequestDTO extends BaseClienteDTO { + + private boolean elegivelDesconto; + + @NotBlank + private String password; + + private List enderecos; + private List telefones; + + @Data + public static class EnderecoDTO { + + private String logradouro; + private String numero; + private String complemento; + private String bairro; + private String cidade; + private String estado; + private String cep; + private String pais; + + public Endereco toEndereco() { + Endereco endereco = Endereco.builder() + .logradouro(this.logradouro) + .numero(this.numero) + .complemento(this.complemento) + .bairro(this.bairro) + .cidade(this.cidade) + .estado(this.estado) + .cep(this.cep) + .pais(this.pais) + .build(); + return endereco; + } + } + + @Data + public static class TelefoneDTO { + + private TipoTelefone tipo; + private Integer ddd; + private String numero; + + public Telefone toTelefone() { + Telefone telefone = Telefone.builder().tipo(this.tipo).ddd(this.ddd).numero(this.numero).build(); + return telefone; + } + } + + public Cliente toCliente() { + Cliente cliente = Cliente.builder() + .nome(this.getNome()) + .cpf(this.getCpf()) + .email(this.getEmail()) + .hashedPassword(this.getPassword()) + .elegivelDesconto(this.isElegivelDesconto()) + .enderecos(this.getEnderecos().stream().map(EnderecoDTO::toEndereco).collect(Collectors.toSet())) + .telefones(this.getTelefones().stream().map(TelefoneDTO::toTelefone).collect(Collectors.toSet())) + .build(); + return cliente; + } +} diff --git a/src/main/java/br/app/precojusto/models/dto/request/PatoRequestDTO.java b/src/main/java/br/app/precojusto/models/dto/request/PatoRequestDTO.java new file mode 100644 index 0000000..b9e75d1 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/request/PatoRequestDTO.java @@ -0,0 +1,10 @@ +package br.app.precojusto.models.dto.request; + +import lombok.Data; + +@Data +public class PatoRequestDTO { + + private String nome; + private Long maeId; +} diff --git a/src/main/java/br/app/precojusto/models/dto/request/VendaRequestDTO.java b/src/main/java/br/app/precojusto/models/dto/request/VendaRequestDTO.java new file mode 100644 index 0000000..dc46906 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/request/VendaRequestDTO.java @@ -0,0 +1,11 @@ +package br.app.precojusto.models.dto.request; + +import java.util.List; +import lombok.Data; + +@Data +public class VendaRequestDTO { + + private Long clienteId; + private List patosId; +} diff --git a/src/main/java/br/app/precojusto/models/dto/response/ClienteResponseDTO.java b/src/main/java/br/app/precojusto/models/dto/response/ClienteResponseDTO.java new file mode 100644 index 0000000..0ba0639 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/response/ClienteResponseDTO.java @@ -0,0 +1,38 @@ +package br.app.precojusto.models.dto.response; + +import br.app.precojusto.models.Cliente; +import br.app.precojusto.models.dto.base.BaseClienteDTO; +import java.util.List; +import java.util.stream.Collectors; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; + +@Data +@EqualsAndHashCode(callSuper = false) +public class ClienteResponseDTO extends BaseClienteDTO { + + private Long id; + private boolean ativo; + private boolean elegivelDesconto; + + public static ClienteResponseDTO fromCliente(Cliente cliente) { + ClienteResponseDTO dto = new ClienteResponseDTO(); + dto.id = cliente.getId(); + dto.setNome(cliente.getNome()); + dto.setCpf(cliente.getCpf()); + dto.setEmail(cliente.getEmail()); + dto.setElegivelDesconto(cliente.isElegivelDesconto()); + dto.setAtivo(cliente.isAtivo()); + return dto; + } + + public static Page fromClientes(Page clientes) { + List dtoList = clientes + .stream() + .map(ClienteResponseDTO::fromCliente) + .collect(Collectors.toList()); + return new PageImpl<>(dtoList, clientes.getPageable(), clientes.getTotalElements()); + } +} diff --git a/src/main/java/br/app/precojusto/models/dto/response/PatoResponseDTO.java b/src/main/java/br/app/precojusto/models/dto/response/PatoResponseDTO.java new file mode 100644 index 0000000..c32ba45 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/response/PatoResponseDTO.java @@ -0,0 +1,29 @@ +package br.app.precojusto.models.dto.response; + +import br.app.precojusto.models.Pato; +import lombok.Data; +import org.springframework.data.domain.Page; + +@Data +public class PatoResponseDTO { + + private Long id; + private String mae; + private String nome; + private Double preco; + + public static PatoResponseDTO fromPato(Pato pato) { + PatoResponseDTO dto = new PatoResponseDTO(); + dto.setId(pato.getId()); + dto.setNome(pato.getNome()); + dto.setPreco(pato.getPreco()); + if (pato.getMae() != null) { + dto.setMae(pato.getMae().getNome()); + } + return dto; + } + + public static Page fromPato(Page patos) { + return patos.map(PatoResponseDTO::fromPato); + } +} diff --git a/src/main/java/br/app/precojusto/models/dto/response/RelatorioResponseDTO.java b/src/main/java/br/app/precojusto/models/dto/response/RelatorioResponseDTO.java new file mode 100644 index 0000000..6177e1e --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/response/RelatorioResponseDTO.java @@ -0,0 +1,61 @@ +package br.app.precojusto.models.dto.response; + +import br.app.precojusto.models.Pato; +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.data.domain.Page; + +import lombok.Data; + +@Data +public class RelatorioResponseDTO { + + private Long id; + private String nome; + private String status; + private String cliente; + private String tipoCliente; + private Double valor; + private List filhos; + private int nivel; + + public static RelatorioResponseDTO toPatoDTO(Pato pato) { + RelatorioResponseDTO dto = new RelatorioResponseDTO(); + dto.setId(pato.getId()); + dto.setNome(pato.getNome() != null && !pato.getNome().isEmpty() ? pato.getNome() : "Sem Nome"); + dto.setStatus(pato.isVendido() ? "Vendido" : "Disponível"); + dto.setCliente( + pato.getVenda() != null && pato.getVenda().getCliente() != null + ? pato.getVenda().getCliente().getNome() + : "" + ); + if (pato.getVenda() != null && pato.getVenda().getCliente() != null) { + dto.setTipoCliente(pato.getVenda().getCliente().isElegivelDesconto() ? "Elegível" : "Normal"); + if (pato.isVendido() && pato.getVenda().getCliente().isElegivelDesconto()) { + dto.setValor(pato.getPreco() * 0.8); // 20% de desconto + } else { + dto.setValor(pato.getPreco()); + } + } else { + dto.setValor(pato.getPreco()); + } + dto.setFilhos(pato.getFilhos().stream().map(RelatorioResponseDTO::toPatoDTO).collect(Collectors.toList())); + return dto; + } + + public static PatoResponseDTO fromPato(Pato pato) { + PatoResponseDTO dto = new PatoResponseDTO(); + dto.setId(pato.getId()); + dto.setNome(pato.getNome()); + dto.setPreco(pato.getPreco()); + if (pato.getMae() != null) { + dto.setMae(pato.getMae().getNome()); + } + return dto; + } + + public static Page fromPato(Page patos) { + return patos.map(PatoResponseDTO::fromPato); + } +} diff --git a/src/main/java/br/app/precojusto/models/dto/response/VendaResponseDTO.java b/src/main/java/br/app/precojusto/models/dto/response/VendaResponseDTO.java new file mode 100644 index 0000000..292e2d7 --- /dev/null +++ b/src/main/java/br/app/precojusto/models/dto/response/VendaResponseDTO.java @@ -0,0 +1,57 @@ +package br.app.precojusto.models.dto.response; + +import br.app.precojusto.models.Venda; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; +import lombok.Data; +import org.springframework.data.domain.Page; + +@Data +public class VendaResponseDTO { + + private Long id; + private ClienteResponseDTO cliente; + private List pato; + private Date dataVenda; + private double valorTotal; + private int quantidadePatos; + + @Data + public static class PatoVendidosDTO { + + private Long id; + private String nome; + private Double preco; + } + + public static VendaResponseDTO fromVendaPato(Venda venda) { + VendaResponseDTO dto = new VendaResponseDTO(); + dto.setId(venda.getId()); + dto.setDataVenda(venda.getDataVenda()); + dto.setQuantidadePatos(venda.getQuantidadePatos()); + dto.setValorTotal(venda.getValorTotal()); + + ClienteResponseDTO clienteResponseDTO = ClienteResponseDTO.fromCliente(venda.getCliente()); + dto.setCliente(clienteResponseDTO); + + List patosDTO = venda + .getPatos() + .stream() + .map(pato -> { + PatoVendidosDTO patoDTO = new PatoVendidosDTO(); + patoDTO.setId(pato.getId()); + patoDTO.setNome(pato.getNome()); + patoDTO.setPreco(pato.getPreco()); + return patoDTO; + }) + .collect(Collectors.toList()); + dto.setPato(patosDTO); + + return dto; + } + + public static Page fromVendaPato(Page vendaPato) { + return vendaPato.map(VendaResponseDTO::fromVendaPato); + } +} diff --git a/src/main/java/br/app/precojusto/repositories/ClienteRepository.java b/src/main/java/br/app/precojusto/repositories/ClienteRepository.java new file mode 100644 index 0000000..fa52150 --- /dev/null +++ b/src/main/java/br/app/precojusto/repositories/ClienteRepository.java @@ -0,0 +1,16 @@ +package br.app.precojusto.repositories; + +import br.app.precojusto.models.Cliente; +import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * ClienteRepository + */ +public interface ClienteRepository extends JpaRepository { + Cliente findByEmail(String email); + List findByAtivoTrue(); + Page findByAtivoTrue(Pageable pageable); +} diff --git a/src/main/java/br/app/precojusto/repositories/EnderecoRepository.java b/src/main/java/br/app/precojusto/repositories/EnderecoRepository.java new file mode 100644 index 0000000..58dd97a --- /dev/null +++ b/src/main/java/br/app/precojusto/repositories/EnderecoRepository.java @@ -0,0 +1,9 @@ +package br.app.precojusto.repositories; + +import br.app.precojusto.models.Endereco; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * EnderecoRepository + */ +public interface EnderecoRepository extends JpaRepository {} diff --git a/src/main/java/br/app/precojusto/repositories/PatoRepository.java b/src/main/java/br/app/precojusto/repositories/PatoRepository.java new file mode 100644 index 0000000..6fe87b9 --- /dev/null +++ b/src/main/java/br/app/precojusto/repositories/PatoRepository.java @@ -0,0 +1,12 @@ +package br.app.precojusto.repositories; + +import br.app.precojusto.models.Pato; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * PatoRepository + */ +public interface PatoRepository extends JpaRepository { + List findAllByVendaIsNull(); +} diff --git a/src/main/java/br/app/precojusto/repositories/TelefoneRepository.java b/src/main/java/br/app/precojusto/repositories/TelefoneRepository.java new file mode 100644 index 0000000..886cc4d --- /dev/null +++ b/src/main/java/br/app/precojusto/repositories/TelefoneRepository.java @@ -0,0 +1,9 @@ +package br.app.precojusto.repositories; + +import br.app.precojusto.models.Telefone; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * TelefoneRepository + */ +public interface TelefoneRepository extends JpaRepository {} diff --git a/src/main/java/br/app/precojusto/repositories/VendaRepository.java b/src/main/java/br/app/precojusto/repositories/VendaRepository.java new file mode 100644 index 0000000..0d0f5ad --- /dev/null +++ b/src/main/java/br/app/precojusto/repositories/VendaRepository.java @@ -0,0 +1,9 @@ +package br.app.precojusto.repositories; + +import br.app.precojusto.models.Venda; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * VendaRepository + */ +public interface VendaRepository extends JpaRepository {} diff --git a/src/main/java/br/app/precojusto/services/ClienteService.java b/src/main/java/br/app/precojusto/services/ClienteService.java new file mode 100644 index 0000000..da6e855 --- /dev/null +++ b/src/main/java/br/app/precojusto/services/ClienteService.java @@ -0,0 +1,70 @@ +package br.app.precojusto.services; + +import br.app.precojusto.exceptions.BadRequest; +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Cliente; +import br.app.precojusto.models.dto.request.ClienteRequestDTO; +import br.app.precojusto.models.dto.response.ClienteResponseDTO; +import br.app.precojusto.repositories.ClienteRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +@Service +public class ClienteService { + + @Autowired + private ClienteRepository clienteRepository; + + public ClienteResponseDTO save(ClienteRequestDTO clienteDTO) throws BadRequest { + Cliente clienteEmail = findByEmail(clienteDTO.getEmail()); + if (clienteEmail != null) { + throw new BadRequest("Email já cadastrado"); + } + // Cliente clienteCpf = clienteRepository.findByCpf(clienteDTO.getCpf()); + // if (clienteCpf != null) { + // throw new BadRequest("CPF já cadastrado"); + // } + Cliente clienteFromDTO = clienteDTO.toCliente(); + return ClienteResponseDTO.fromCliente(clienteRepository.save(clienteFromDTO)); + } + + public Page findAll(int page, int size) { + return ClienteResponseDTO.fromClientes(clienteRepository.findAll(PageRequest.of(page, size))); + } + + public Cliente findByEmail(String email) { + return clienteRepository.findByEmail(email); + } + + public Cliente update(Long id, ClienteRequestDTO clienteDTO) throws NotFound, BadRequest { + Cliente existingCliente = findById(id); + Cliente cliente = clienteDTO.toCliente(); + cliente.setId(existingCliente.getId()); + if (cliente.getEmail() != null) { + Cliente clienteEmail = findByEmail(cliente.getEmail()); + if (clienteEmail != null && !clienteEmail.getId().equals(id)) { + throw new BadRequest("Email já cadastrado"); + } + } + // TODO: Antes de salvar, verificar se o email já existe + // Verificar também CPF + return clienteRepository.save(cliente); + } + + public void delete(Long id) throws NotFound { + Cliente cliente = findById(id); + if (!cliente.isAtivo()) { + throw new NotFound("Cliente não encontrado com ID: " + id); + } + cliente.setAtivo(false); + clienteRepository.save(cliente); + } + + public Cliente findById(Long id) throws NotFound { + return clienteRepository + .findById(id) + .orElseThrow(() -> new NotFound("Cliente não encontrada com ID: " + id)); + } +} diff --git a/src/main/java/br/app/precojusto/services/PatoService.java b/src/main/java/br/app/precojusto/services/PatoService.java new file mode 100644 index 0000000..fd91f2d --- /dev/null +++ b/src/main/java/br/app/precojusto/services/PatoService.java @@ -0,0 +1,69 @@ +package br.app.precojusto.services; + +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Pato; +import br.app.precojusto.models.dto.request.PatoRequestDTO; +import br.app.precojusto.models.dto.response.RelatorioResponseDTO; +import br.app.precojusto.repositories.PatoRepository; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +@Service +public class PatoService { + + @Autowired + private PatoRepository patoRepository; + + public Pato save(PatoRequestDTO createPatoDTO) throws NotFound { + Pato pato = new Pato(); + pato.setNome(createPatoDTO.getNome()); + + if (createPatoDTO.getMaeId() != null) { + Pato mae = new Pato(); + mae.setId(createPatoDTO.getMaeId()); + if (patoRepository.findById(createPatoDTO.getMaeId()).isEmpty()) { + throw new NotFound("Mãe não encontrada com ID: " + createPatoDTO.getMaeId()); + } + pato.setMae(mae); + } + + return patoRepository.save(pato); + } + + public Page findAll(int page, int size) { + return patoRepository.findAll(PageRequest.of(page, size)); + } + + public Pato findById(Long id) throws NotFound { + return patoRepository + .findById(id) + .orElseThrow(() -> new NotFound("Pato não encontrada com ID: " + id)); + } + + public Pato update(Long id, PatoRequestDTO patoRequestDTO) throws NotFound { + Pato existingPato = findById(id); + existingPato.setNome(patoRequestDTO.getNome()); + if (patoRequestDTO.getMaeId() != null) { + Pato mae = new Pato(); + mae.setId(patoRequestDTO.getMaeId()); + existingPato.setMae(mae); + } + return patoRepository.save(existingPato); + } + + public void delete(Long id) throws NotFound { + // throw new NotFound("Pato não encontrado"); + findById(id); + patoRepository.deleteById(id); + } + + public List obterPatosOrganizados() { + List patos = patoRepository.findAll(); + List patosMaes = patos.stream().filter(p -> p.getMae() == null).collect(Collectors.toList()); + return patosMaes.stream().map(RelatorioResponseDTO::toPatoDTO).collect(Collectors.toList()); + } +} diff --git a/src/main/java/br/app/precojusto/services/VendaService.java b/src/main/java/br/app/precojusto/services/VendaService.java new file mode 100644 index 0000000..cb363a0 --- /dev/null +++ b/src/main/java/br/app/precojusto/services/VendaService.java @@ -0,0 +1,85 @@ +package br.app.precojusto.services; + +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Cliente; +import br.app.precojusto.models.Pato; +import br.app.precojusto.models.Venda; +import br.app.precojusto.repositories.VendaRepository; +import jakarta.persistence.EntityNotFoundException; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +@Service +public class VendaService { + + @Autowired + private VendaRepository vendaRepository; + + @Autowired + private ClienteService clienteService; + + @Autowired + private PatoService patoService; + + public Venda create(Long clienteId, List patosId) throws NotFound { + Cliente cliente = clienteService.findById(clienteId); + if (cliente == null) { + throw new EntityNotFoundException("Cliente não encontrado com ID: " + clienteId); + } + + List patos = getPatos(patosId); + + Venda venda = new Venda(); + venda.setCliente(cliente); + venda.setPatos(patos); + venda.setQuantidadePatos(patos.size()); + venda.setDesconto(cliente.isElegivelDesconto()); + double valorTotal = patos.stream().mapToDouble(Pato::getPreco).sum(); + if (cliente.isElegivelDesconto()) { + valorTotal *= 0.8; // Aplica 20% de desconto + } + venda.setValorTotal(valorTotal); + patos.forEach(pato -> { + pato.setVenda(venda); + pato.setVendido(true); + }); + + return vendaRepository.save(venda); + } + + public Page findAll(int page, int size) { + return vendaRepository.findAll(PageRequest.of(page, size)); + } + + public Venda findById(Long id) throws NotFound { + return vendaRepository + .findById(id) + .orElseThrow(() -> new NotFound("Venda não encontrada com ID: " + id)); + } + + private List getPatos(List patosId) { + return patosId + .stream() + .map(patoId -> { + Pato pato = null; + try { + pato = patoService.findById(patoId); + } catch (NotFound e) { + e.printStackTrace(); + throw new EntityNotFoundException("Pato não encontrado com ID: " + patoId); + } + if (pato == null) { + throw new EntityNotFoundException("Pato não encontrado com ID: " + patoId); + } + if (pato.isVendido()) { + throw new EntityNotFoundException("Pato já vendido com ID: " + patoId); + } + return pato; + }) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/br/app/precojusto/utils/validation/CPF.java b/src/main/java/br/app/precojusto/utils/validation/CPF.java new file mode 100644 index 0000000..ffcaf75 --- /dev/null +++ b/src/main/java/br/app/precojusto/utils/validation/CPF.java @@ -0,0 +1,20 @@ +package br.app.precojusto.utils.validation; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Target({ FIELD }) +@Retention(RUNTIME) +@Constraint(validatedBy = ValidadorCPF.class) +public @interface CPF { + String message() default "CPF inválido"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/src/main/java/br/app/precojusto/utils/validation/ValidadorCPF.java b/src/main/java/br/app/precojusto/utils/validation/ValidadorCPF.java new file mode 100644 index 0000000..8aefcc0 --- /dev/null +++ b/src/main/java/br/app/precojusto/utils/validation/ValidadorCPF.java @@ -0,0 +1,37 @@ +package br.app.precojusto.utils.validation; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; + +public class ValidadorCPF implements ConstraintValidator { + + @Override + public void initialize(CPF constraintAnnotation) {} + + @Override + public boolean isValid(String cpf, ConstraintValidatorContext context) { + if (cpf == null || cpf.length() != 11 || isSequencia(cpf)) { + return false; + } + + String cpfParcial = cpf.substring(0, 9); + String digitoVerificador1 = gerarDigitoVerificador(cpfParcial); + String digitoVerificador2 = gerarDigitoVerificador(cpfParcial + digitoVerificador1); + + return cpf.equals(cpfParcial + digitoVerificador1 + digitoVerificador2); + } + + private boolean isSequencia(String cpf) { + return cpf.chars().allMatch(c -> c == cpf.charAt(0)); + } + + private String gerarDigitoVerificador(String cpfParcial) { + int soma = 0; + int peso = cpfParcial.length() + 1; + for (int i = 0; i < cpfParcial.length(); i++) { + soma += Character.getNumericValue(cpfParcial.charAt(i)) * (peso - i); + } + int resto = soma % 11; + return (resto < 2) ? "0" : String.valueOf(11 - resto); + } +} diff --git a/src/main/resources/PDF.jrxml b/src/main/resources/PDF.jrxml new file mode 100644 index 0000000..f018118 --- /dev/null +++ b/src/main/resources/PDF.jrxml @@ -0,0 +1,126 @@ + + + + + + + + + + + + <band height="50"> + <staticText> + <reportElement x="0" y="0" width="555" height="30" uuid="42cfc179-5958-45e0-bd4c-dc9ed29b7e9f"/> + <textElement textAlignment="Center" verticalAlignment="Middle"> + <font size="20"/> + </textElement> + <text><![CDATA[Relatório de Patos]]></text> + </staticText> + </band> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..75a963e --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,16 @@ +spring.jpa.show-sql=true +spring.jpa.generate-ddl=true + +# Necessário para forçar o DROP de todas as tabelas, e posterior CREATE de cada uma +spring.jpa.hibernate.ddl-auto=create + +# Swagger +springdoc.swagger-ui.path=/swagger-ui.html +springdoc.swagger-ui.enabled=true + + +#Postgres +spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase +spring.datasource.username=myuser +spring.datasource.password=secret +spring.datasource.driverClassName=org.postgresql.Driver \ No newline at end of file diff --git a/src/test/java/br/app/precojusto/BackendApplicationTests.java b/src/test/java/br/app/precojusto/BackendApplicationTests.java new file mode 100644 index 0000000..cd6ce69 --- /dev/null +++ b/src/test/java/br/app/precojusto/BackendApplicationTests.java @@ -0,0 +1,11 @@ +package br.app.precojusto; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class BackendApplicationTests { + + @Test + void contextLoads() {} +} diff --git a/src/test/java/br/app/precojusto/models/response/RelatorioResponseDTOTest.java b/src/test/java/br/app/precojusto/models/response/RelatorioResponseDTOTest.java new file mode 100644 index 0000000..1aa3c15 --- /dev/null +++ b/src/test/java/br/app/precojusto/models/response/RelatorioResponseDTOTest.java @@ -0,0 +1,60 @@ +package br.app.precojusto.models.response; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import br.app.precojusto.models.Pato; +import br.app.precojusto.models.Venda; +import br.app.precojusto.models.dto.response.RelatorioResponseDTO; +import br.app.precojusto.models.Cliente; +import org.junit.jupiter.api.Test; +import java.util.ArrayList; + +public class RelatorioResponseDTOTest { + + @Test + public void testToPatoDTOWithNoNome() { + Pato pato = new Pato(); + pato.setId(1L); + pato.setNome(""); + pato.setVendido(false); + pato.setPreco(100.0); + pato.setFilhos(new ArrayList<>()); + + RelatorioResponseDTO dto = RelatorioResponseDTO.toPatoDTO(pato); + + assertEquals("Sem Nome", dto.getNome()); + assertEquals("Disponível", dto.getStatus()); + assertEquals(100.0, dto.getValor()); + assertEquals(0, dto.getFilhos().size()); + } + + @Test + public void testToPatoDTOWithVendidoAndElegivelDesconto() { + Pato pato = new Pato(); + pato.setId(1L); + pato.setNome("Pato Teste"); + pato.setVendido(true); + pato.setPreco(100.0); + + Cliente cliente = new Cliente(); + cliente.setNome("Cliente Teste"); + cliente.setElegivelDesconto(true); + + Venda venda = new Venda(); + venda.setCliente(cliente); + pato.setVenda(venda); + + pato.setFilhos(new ArrayList<>()); + + RelatorioResponseDTO dto = RelatorioResponseDTO.toPatoDTO(pato); + + assertEquals("Pato Teste", dto.getNome()); + assertEquals("Vendido", dto.getStatus()); + assertEquals("Cliente Teste", dto.getCliente()); + assertEquals("Elegível", dto.getTipoCliente()); + assertEquals(80.0, dto.getValor()); + assertEquals(0, dto.getFilhos().size()); + } + + +} \ No newline at end of file diff --git a/src/test/java/br/app/precojusto/services/ClienteServiceTest.java b/src/test/java/br/app/precojusto/services/ClienteServiceTest.java new file mode 100644 index 0000000..14d38c6 --- /dev/null +++ b/src/test/java/br/app/precojusto/services/ClienteServiceTest.java @@ -0,0 +1,110 @@ +package br.app.precojusto.services; + +import br.app.precojusto.exceptions.BadRequest; +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Cliente; +import br.app.precojusto.models.dto.request.ClienteRequestDTO; +import br.app.precojusto.models.dto.response.ClienteResponseDTO; +import br.app.precojusto.repositories.ClienteRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; + +import java.util.Collections; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class ClienteServiceTest { + + @Mock + private ClienteRepository clienteRepository; + + @InjectMocks + private ClienteService clienteService; + + private ClienteRequestDTO clienteRequestDTO; + private ClienteResponseDTO clienteResponseDTO; + private Cliente cliente; + + @BeforeEach + void setUp() { + clienteRequestDTO = new ClienteRequestDTO(); + clienteRequestDTO.setEmail("test@example.com"); + clienteRequestDTO.setNome("Test Name"); + + cliente = new Cliente(); + cliente.setId(1L); + cliente.setEmail("test@example.com"); + cliente.setNome("Test Name"); + + clienteResponseDTO = ClienteResponseDTO.fromCliente(cliente); + } + + + + @Test + void saveThrowsBadRequest() { + when(clienteRepository.findByEmail(anyString())).thenReturn(cliente); + + assertThrows(BadRequest.class, () -> clienteService.save(clienteRequestDTO)); + } + + @Test + void findAllSuccess() { + Page clientePage = new PageImpl<>(Collections.singletonList(cliente)); + when(clienteRepository.findAll(any(PageRequest.class))).thenReturn(clientePage); + + Page result = clienteService.findAll(0, 1); + + assertFalse(result.isEmpty()); + assertEquals(cliente.getEmail(), result.getContent().get(0).getEmail()); + } + + @Test + void findByEmailSuccess() { + when(clienteRepository.findByEmail(anyString())).thenReturn(cliente); + + Cliente result = clienteService.findByEmail("test@example.com"); + + assertNotNull(result); + assertEquals("test@example.com", result.getEmail()); + } + + + + + + @Test + void deleteThrowsNotFound() { + when(clienteRepository.findById(anyLong())).thenReturn(Optional.empty()); + + assertThrows(NotFound.class, () -> clienteService.delete(1L)); + } + + @Test + void findByIdSuccess() throws NotFound { + when(clienteRepository.findById(anyLong())).thenReturn(Optional.of(cliente)); + + Cliente foundCliente = clienteService.findById(1L); + + assertNotNull(foundCliente); + assertEquals(1L, foundCliente.getId()); + } + + @Test + void findByIdThrowsNotFound() { + when(clienteRepository.findById(anyLong())).thenReturn(Optional.empty()); + + assertThrows(NotFound.class, () -> clienteService.findById(1L)); + } +} \ No newline at end of file diff --git a/src/test/java/br/app/precojusto/services/PatoServiceTest.java b/src/test/java/br/app/precojusto/services/PatoServiceTest.java new file mode 100644 index 0000000..524e085 --- /dev/null +++ b/src/test/java/br/app/precojusto/services/PatoServiceTest.java @@ -0,0 +1,102 @@ +package br.app.precojusto.services; + +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Pato; +import br.app.precojusto.models.dto.request.PatoRequestDTO; +import br.app.precojusto.repositories.PatoRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; + +import java.util.Arrays; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class PatoServiceTest { + + @Mock + private PatoRepository patoRepository; + + @InjectMocks + private PatoService patoService; + + private Pato pato; + private PatoRequestDTO patoRequestDTO; + + @BeforeEach + void setUp() { + pato = new Pato(); + pato.setId(1L); + pato.setNome("Pato Teste"); + + patoRequestDTO = new PatoRequestDTO(); + patoRequestDTO.setNome("Pato Teste"); + patoRequestDTO.setMaeId(null); + } + + + @Test + void saveThrowsNotFound() { + patoRequestDTO.setMaeId(2L); + when(patoRepository.findById(anyLong())).thenReturn(Optional.empty()); + + assertThrows(NotFound.class, () -> patoService.save(patoRequestDTO)); + } + + @Test + void findAllSuccess() { + Page patoPage = new PageImpl<>(Arrays.asList(pato)); + when(patoRepository.findAll(any(PageRequest.class))).thenReturn(patoPage); + + Page result = patoService.findAll(0, 1); + + assertFalse(result.isEmpty()); + assertEquals(pato.getNome(), result.getContent().get(0).getNome()); + } + + @Test + void findByIdSuccess() throws NotFound { + when(patoRepository.findById(anyLong())).thenReturn(Optional.of(pato)); + + Pato foundPato = patoService.findById(1L); + + assertNotNull(foundPato); + assertEquals(pato.getNome(), foundPato.getNome()); + } + + @Test + void findByIdThrowsNotFound() { + when(patoRepository.findById(anyLong())).thenReturn(Optional.empty()); + + assertThrows(NotFound.class, () -> patoService.findById(1L)); + } + + + + @Test + void deleteSuccess() throws NotFound { + when(patoRepository.findById(anyLong())).thenReturn(Optional.of(pato)); + doNothing().when(patoRepository).deleteById(anyLong()); + + assertDoesNotThrow(() -> patoService.delete(1L)); + } + + @Test + void deleteThrowsNotFound() { + when(patoRepository.findById(anyLong())).thenReturn(Optional.empty()); + + assertThrows(NotFound.class, () -> patoService.delete(1L)); + } + + +} \ No newline at end of file diff --git a/src/test/java/br/app/precojusto/services/VendaServiceTest.java b/src/test/java/br/app/precojusto/services/VendaServiceTest.java new file mode 100644 index 0000000..1c0be9b --- /dev/null +++ b/src/test/java/br/app/precojusto/services/VendaServiceTest.java @@ -0,0 +1,71 @@ +package br.app.precojusto.services; + +import br.app.precojusto.exceptions.NotFound; +import br.app.precojusto.models.Cliente; +import br.app.precojusto.models.Pato; +import br.app.precojusto.models.Venda; +import br.app.precojusto.repositories.VendaRepository; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Arrays; + + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class VendaServiceTest { + + @Mock + private VendaRepository vendaRepository; + + @Mock + private ClienteService clienteService; + + @Mock + private PatoService patoService; + + @InjectMocks + private VendaService vendaService; + + @BeforeEach + void setUp() { + // Configuração inicial para os testes, se necessário + } + + @Test + void testCreateVendaComSucesso() throws NotFound { + Cliente clienteMock = new Cliente(); + clienteMock.setId(1L); + clienteMock.setElegivelDesconto(true); + + Pato patoMock = new Pato(); + patoMock.setId(1L); + patoMock.setPreco(100.0); + patoMock.setVendido(false); + + when(clienteService.findById(anyLong())).thenReturn(clienteMock); + when(patoService.findById(anyLong())).thenReturn(patoMock); + when(vendaRepository.save(any(Venda.class))).thenAnswer(invocation -> invocation.getArgument(0)); + + Venda venda = vendaService.create(1L, Arrays.asList(1L)); + + assertNotNull(venda); + assertTrue(venda.isDesconto()); + assertEquals(1, venda.getQuantidadePatos()); + assertEquals(80.0, venda.getValorTotal()); + assertTrue(venda.getPatos().get(0).isVendido()); + + verify(clienteService).findById(1L); + verify(patoService).findById(1L); + verify(vendaRepository).save(any(Venda.class)); + } + +} \ No newline at end of file diff --git a/src/test/java/br/app/precojusto/utils/validation/ValidadorCPFTest.java b/src/test/java/br/app/precojusto/utils/validation/ValidadorCPFTest.java new file mode 100644 index 0000000..d11183d --- /dev/null +++ b/src/test/java/br/app/precojusto/utils/validation/ValidadorCPFTest.java @@ -0,0 +1,40 @@ +package br.app.precojusto.utils.validation; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class ValidadorCPFTest { + + private final ValidadorCPF validador = new ValidadorCPF(); + + @Test + void cpfNuloDeveSerInvalido() { + assertFalse(validador.isValid(null, null)); + } + + @Test + void cpfComTamanhoIncorretoDeveSerInvalido() { + assertFalse(validador.isValid("1234567890", null)); // Menos de 11 caracteres + assertFalse(validador.isValid("123456789012", null)); // Mais de 11 caracteres + } + + @Test + void cpfComSequenciaDeveSerInvalido() { + assertFalse(validador.isValid("11111111111", null)); + } + + @Test + void cpfValidoDeveSerValido() { + assertTrue(validador.isValid("52998224725", null)); // Exemplo de CPF válido + } + + @Test + void cpfInvalidoDeveSerInvalido() { + assertFalse(validador.isValid("52998224724", null)); // Dígito verificador inválido + } + + @Test + void cpfComCaracteresNaoNumericosDeveSerInvalido() { + assertFalse(validador.isValid("5299822472a", null)); // Contém letra + } +} \ No newline at end of file diff --git a/wrapper/maven-wrapper.properties b/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..8f96f52 --- /dev/null +++ b/wrapper/maven-wrapper.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +wrapperVersion=3.3.2 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.7/apache-maven-3.9.7-bin.zip