├── .gitignore ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── LICENSE ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── br │ │ └── com │ │ └── leandrosnazareth │ │ └── pdvapi │ │ ├── PdvApiApplication.java │ │ ├── config │ │ ├── InternacionalizacaoConfig.java │ │ ├── MapperConfig.java │ │ ├── SpringFoxConfig.java │ │ └── WebConfig.java │ │ ├── controller │ │ ├── PaymentController.java │ │ ├── ProductController.java │ │ ├── SaleController.java │ │ └── UserController.java │ │ ├── domain │ │ ├── entity │ │ │ ├── Product.java │ │ │ ├── ProductSold.java │ │ │ ├── Role.java │ │ │ ├── Sale.java │ │ │ └── Usuario.java │ │ ├── model │ │ │ └── Payment.java │ │ └── repository │ │ │ ├── ProductRepository.java │ │ │ ├── SaleRepository.java │ │ │ └── UsuerRepository.java │ │ ├── dto │ │ ├── ProductDto.java │ │ ├── ProductSoldDTO.java │ │ ├── SaleDto.java │ │ ├── UserDTO.java │ │ └── UserFullDTO.java │ │ ├── exception │ │ ├── ErrorDetails.java │ │ ├── GlobalExceptionHandler.java │ │ └── ResourceNotFoundException.java │ │ ├── security │ │ ├── ApplicationContextLoad.java │ │ ├── JWTLoginFilter.java │ │ ├── JWTTokenAutenticacaoService.java │ │ ├── JwtApiAutenticacaoFilter.java │ │ ├── ServletInitializer.java │ │ └── WebConfigSecurity.java │ │ ├── service │ │ ├── ImplementacaoUserDetailsSercice.java │ │ ├── ProductService.java │ │ ├── SaleService.java │ │ └── UserService.java │ │ └── util │ │ └── MensageConstant.java └── resources │ ├── application.yml │ ├── logback-spring.xml │ └── mensagens.properties └── test ├── java └── br │ └── com │ └── blsoft │ └── pdvapi │ ├── integration │ ├── ProdutoIntegrationTest.java │ └── SaleIntegrationTest.java │ ├── unit │ ├── ProductSoldTest.java │ └── ProdutoTest.java │ └── util │ ├── ConstantTest.java │ ├── ProductDataTest.java │ ├── ProductSoldDataTest.java │ └── SaleDataTest.java └── resources └── application-integration-test.yml /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leandrosnazareth/pdv-api/efd2cdd1f297f9f6d81e8897f59e9d2973590043/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.5/apache-maven-3.8.5-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Paul Sterl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Bem vindo ao PDV Web!

2 |

3 | 4 | 5 | 6 |

7 |

8 | 9 | 10 | 11 | 12 | 13 | License: MIT 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |

24 | 25 | **PDV Web** é um sistema, **Open Source**, de vendas de frente de caixa. O PDV é a ferramenta para registar vendas de forma rápida e confiável. Este projeto foi pensando em atender as necessidades de pequenos empreendedores do ramo de mercearias, minimercados e afins. 26 | 27 | ## Objetivo do projeto 28 | Disponibilizar um projeto de código fonte **Open Source** de um sistema de vendas em PDF desenvolvido utilizando as tecnologias listadas abaixo: 29 | 30 | - Spring Boot; 31 | - Spring Data JPA (com Hibernate); 32 | - Spring Starter Test; 33 | - Spring Boot Starter Web; 34 | - Angular Material; 35 | - Flyway; 36 | - Lombok; 37 | - Maven; 38 | - JUnit; 39 | - Swagger (Exposição da API REST do sistema de forma padronizada); 40 | - Mockito; 41 | - Testcontainers; 42 | - Docker; 43 | - TDD (Test-Driven Development) testes unitários e de integração; 44 | - CoreUI Bootstrap Admin Dashboard Template. 45 | 46 | ## O sistema 47 | 48 | ### Dashboard 49 | ![](https://i.ibb.co/SVVBrJ2/dashboard.png) 50 | 51 | ### PDV 52 | 53 | ![](https://i.ibb.co/QrW4mMw/pdv.png) 54 | 55 | ### Produto 56 | 57 | ![](https://i.ibb.co/HXVssNT/cad-Produto.png) 58 | 59 | ![](https://i.ibb.co/LnPxG4r/list-Produto.png) 60 | 61 | ## Swagger Documentation 62 | 63 | ![](https://i.ibb.co/N3vy5Dk/Swagger.png) 64 | 65 | ## Pré-requisitos 66 | 67 | * npm >= 8.3.1 68 | 69 | * java = 11.0.14 70 | 71 | * mysql >= 8.0.29 72 | 73 | * Angular = 12.1.3 74 | 75 | * Node >= 16.13.1 76 | 77 | ## Instalação APP 78 | 79 | Faça download do NodeJs caso não tenha instalando em sua máquina 80 | 81 | ```sh 82 | 83 | https://nodejs.org/en/download/ 84 | 85 | ``` 86 | 87 | 1 - Git clone: https://github.com/leandrosnazareth/pdv-app 88 | 89 | 2 - Abrir projeto na IDE Visual Studio Code 90 | 91 | 3 - No terminal, na pasta do projeto executar os comando abaixo 92 | 93 | 4 - npm install @angular/cli@ 12.1.3 94 | 95 | 5 - ng serve --open 96 | 97 | ### Uso 98 | 99 | O projeto está localizado em 100 | 101 | 102 | ```sh 103 | 104 | localhost:4200 105 | 106 | ``` 107 | 108 | 109 | ## Instalação API 110 | 111 | 1 - Git clone: https://github.com/leandrosnazareth/pdv-api 112 | 113 | 2 - Faça o download e instalação padrão do Docker 114 | 115 | https://docs.docker.com/desktop/windows/install/ 116 | 117 | 3- execute o comando abaixo para instalar o mysql no docker 118 | 119 | docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest 120 | 121 | 4- Execute o mysql no docker 122 | 123 | 4- Faça download do JDK 11 caso não tenha instalado em sua máquina 124 | 125 | https://www.oracle.com/br/java/technologies/javase/jdk11-archive-downloads.html 126 | 127 | Execute o projeto... 128 | 129 | ## Uso 130 | 131 | O projeto está localizado em 132 | 133 | 134 | ```sh 135 | 136 | localhost:8080 137 | 138 | ``` 139 | 140 | 141 | ## Autor 142 | 143 | 👤 **Leandro Nazareth** 144 | 145 | * Github: [@leandrosnazareth](https://github.com/leandrosnazareth) 146 | 147 | ## 🤝 Contribuições 148 | 149 | Contribuições, são bem vindas!
150 | Sinta-se à vontade para perguntar API [issues page](https://github.com/leandrosnazareth/pdv-api/issues). 151 | 152 | ## Mostre seu apoio 153 | 154 | Dê uma ⭐️ se este projeto te ajudou! 155 | 156 | ## 📝 Licença 157 | 158 | Copyright © 2022 [Leandro Nazareth](https://github.com/leandrosnazareth).
159 | 160 | Este projeto está sob lincença [MIT](https://github.com/leandrosnazareth/pdv-app/blob/master/LICENSE). 161 | 162 | Template usado no projeto [CoreUi](https://coreui.io/angular/) 163 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /usr/local/etc/mavenrc ] ; then 40 | . /usr/local/etc/mavenrc 41 | fi 42 | 43 | if [ -f /etc/mavenrc ] ; then 44 | . /etc/mavenrc 45 | fi 46 | 47 | if [ -f "$HOME/.mavenrc" ] ; then 48 | . "$HOME/.mavenrc" 49 | fi 50 | 51 | fi 52 | 53 | # OS specific support. $var _must_ be set to either true or false. 54 | cygwin=false; 55 | darwin=false; 56 | mingw=false 57 | case "`uname`" in 58 | CYGWIN*) cygwin=true ;; 59 | MINGW*) mingw=true;; 60 | Darwin*) darwin=true 61 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 62 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 63 | if [ -z "$JAVA_HOME" ]; then 64 | if [ -x "/usr/libexec/java_home" ]; then 65 | export JAVA_HOME="`/usr/libexec/java_home`" 66 | else 67 | export JAVA_HOME="/Library/Java/Home" 68 | fi 69 | fi 70 | ;; 71 | esac 72 | 73 | if [ -z "$JAVA_HOME" ] ; then 74 | if [ -r /etc/gentoo-release ] ; then 75 | JAVA_HOME=`java-config --jre-home` 76 | fi 77 | fi 78 | 79 | if [ -z "$M2_HOME" ] ; then 80 | ## resolve links - $0 may be a link to maven's home 81 | PRG="$0" 82 | 83 | # need this for relative symlinks 84 | while [ -h "$PRG" ] ; do 85 | ls=`ls -ld "$PRG"` 86 | link=`expr "$ls" : '.*-> \(.*\)$'` 87 | if expr "$link" : '/.*' > /dev/null; then 88 | PRG="$link" 89 | else 90 | PRG="`dirname "$PRG"`/$link" 91 | fi 92 | done 93 | 94 | saveddir=`pwd` 95 | 96 | M2_HOME=`dirname "$PRG"`/.. 97 | 98 | # make it fully qualified 99 | M2_HOME=`cd "$M2_HOME" && pwd` 100 | 101 | cd "$saveddir" 102 | # echo Using m2 at $M2_HOME 103 | fi 104 | 105 | # For Cygwin, ensure paths are in UNIX format before anything is touched 106 | if $cygwin ; then 107 | [ -n "$M2_HOME" ] && 108 | M2_HOME=`cygpath --unix "$M2_HOME"` 109 | [ -n "$JAVA_HOME" ] && 110 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 111 | [ -n "$CLASSPATH" ] && 112 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 113 | fi 114 | 115 | # For Mingw, ensure paths are in UNIX format before anything is touched 116 | if $mingw ; then 117 | [ -n "$M2_HOME" ] && 118 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 119 | [ -n "$JAVA_HOME" ] && 120 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 121 | fi 122 | 123 | if [ -z "$JAVA_HOME" ]; then 124 | javaExecutable="`which javac`" 125 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 126 | # readlink(1) is not available as standard on Solaris 10. 127 | readLink=`which readlink` 128 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 129 | if $darwin ; then 130 | javaHome="`dirname \"$javaExecutable\"`" 131 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 132 | else 133 | javaExecutable="`readlink -f \"$javaExecutable\"`" 134 | fi 135 | javaHome="`dirname \"$javaExecutable\"`" 136 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 137 | JAVA_HOME="$javaHome" 138 | export JAVA_HOME 139 | fi 140 | fi 141 | fi 142 | 143 | if [ -z "$JAVACMD" ] ; then 144 | if [ -n "$JAVA_HOME" ] ; then 145 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 146 | # IBM's JDK on AIX uses strange locations for the executables 147 | JAVACMD="$JAVA_HOME/jre/sh/java" 148 | else 149 | JAVACMD="$JAVA_HOME/bin/java" 150 | fi 151 | else 152 | JAVACMD="`\\unset -f command; \\command -v java`" 153 | fi 154 | fi 155 | 156 | if [ ! -x "$JAVACMD" ] ; then 157 | echo "Error: JAVA_HOME is not defined correctly." >&2 158 | echo " We cannot execute $JAVACMD" >&2 159 | exit 1 160 | fi 161 | 162 | if [ -z "$JAVA_HOME" ] ; then 163 | echo "Warning: JAVA_HOME environment variable is not set." 164 | fi 165 | 166 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 167 | 168 | # traverses directory structure from process work directory to filesystem root 169 | # first directory with .mvn subdirectory is considered project base directory 170 | find_maven_basedir() { 171 | 172 | if [ -z "$1" ] 173 | then 174 | echo "Path not specified to find_maven_basedir" 175 | return 1 176 | fi 177 | 178 | basedir="$1" 179 | wdir="$1" 180 | while [ "$wdir" != '/' ] ; do 181 | if [ -d "$wdir"/.mvn ] ; then 182 | basedir=$wdir 183 | break 184 | fi 185 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 186 | if [ -d "${wdir}" ]; then 187 | wdir=`cd "$wdir/.."; pwd` 188 | fi 189 | # end of workaround 190 | done 191 | echo "${basedir}" 192 | } 193 | 194 | # concatenates all lines of a file 195 | concat_lines() { 196 | if [ -f "$1" ]; then 197 | echo "$(tr -s '\n' ' ' < "$1")" 198 | fi 199 | } 200 | 201 | BASE_DIR=`find_maven_basedir "$(pwd)"` 202 | if [ -z "$BASE_DIR" ]; then 203 | exit 1; 204 | fi 205 | 206 | ########################################################################################## 207 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 208 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 209 | ########################################################################################## 210 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Found .mvn/wrapper/maven-wrapper.jar" 213 | fi 214 | else 215 | if [ "$MVNW_VERBOSE" = true ]; then 216 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 217 | fi 218 | if [ -n "$MVNW_REPOURL" ]; then 219 | jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 220 | else 221 | jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 222 | fi 223 | while IFS="=" read key value; do 224 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 225 | esac 226 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 227 | if [ "$MVNW_VERBOSE" = true ]; then 228 | echo "Downloading from: $jarUrl" 229 | fi 230 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 231 | if $cygwin; then 232 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 233 | fi 234 | 235 | if command -v wget > /dev/null; then 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Found wget ... using wget" 238 | fi 239 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 240 | wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 241 | else 242 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 243 | fi 244 | elif command -v curl > /dev/null; then 245 | if [ "$MVNW_VERBOSE" = true ]; then 246 | echo "Found curl ... using curl" 247 | fi 248 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 249 | curl -o "$wrapperJarPath" "$jarUrl" -f 250 | else 251 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 252 | fi 253 | 254 | else 255 | if [ "$MVNW_VERBOSE" = true ]; then 256 | echo "Falling back to using Java to download" 257 | fi 258 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 259 | # For Cygwin, switch paths to Windows format before running javac 260 | if $cygwin; then 261 | javaClass=`cygpath --path --windows "$javaClass"` 262 | fi 263 | if [ -e "$javaClass" ]; then 264 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 265 | if [ "$MVNW_VERBOSE" = true ]; then 266 | echo " - Compiling MavenWrapperDownloader.java ..." 267 | fi 268 | # Compiling the Java class 269 | ("$JAVA_HOME/bin/javac" "$javaClass") 270 | fi 271 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 272 | # Running the downloader 273 | if [ "$MVNW_VERBOSE" = true ]; then 274 | echo " - Running MavenWrapperDownloader.java ..." 275 | fi 276 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 277 | fi 278 | fi 279 | fi 280 | fi 281 | ########################################################################################## 282 | # End of extension 283 | ########################################################################################## 284 | 285 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 286 | if [ "$MVNW_VERBOSE" = true ]; then 287 | echo $MAVEN_PROJECTBASEDIR 288 | fi 289 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 290 | 291 | # For Cygwin, switch paths to Windows format before running java 292 | if $cygwin; then 293 | [ -n "$M2_HOME" ] && 294 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 295 | [ -n "$JAVA_HOME" ] && 296 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 297 | [ -n "$CLASSPATH" ] && 298 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 299 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 300 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 301 | fi 302 | 303 | # Provide a "standardized" way to retrieve the CLI args that will 304 | # work with both Windows and non-Windows executions. 305 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 306 | export MAVEN_CMD_LINE_ARGS 307 | 308 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 309 | 310 | exec "$JAVACMD" \ 311 | $MAVEN_OPTS \ 312 | $MAVEN_DEBUG_OPTS \ 313 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 314 | "-Dmaven.home=${M2_HOME}" \ 315 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 316 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 317 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 50 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 124 | 125 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% ^ 162 | %JVM_CONFIG_MAVEN_PROPS% ^ 163 | %MAVEN_OPTS% ^ 164 | %MAVEN_DEBUG_OPTS% ^ 165 | -classpath %WRAPPER_JAR% ^ 166 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 167 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 168 | if ERRORLEVEL 1 goto error 169 | goto end 170 | 171 | :error 172 | set ERROR_CODE=1 173 | 174 | :end 175 | @endlocal & set ERROR_CODE=%ERROR_CODE% 176 | 177 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 178 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 179 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 180 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 181 | :skipRcPost 182 | 183 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 184 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 185 | 186 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 187 | 188 | cmd /C exit /B %ERROR_CODE% 189 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | org.springframework.boot 6 | spring-boot-starter-parent 7 | 2.5.2 8 | 9 | 10 | br.com.blsoft 11 | pdv-api 12 | 0.0.1-SNAPSHOT 13 | pdv-api 14 | Sistema de venda PDV web para testes 15 | 16 | 11 17 | 1.17.2 18 | 3.0.0 19 | 3.0.0 20 | 1.17.2 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-data-jpa 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | org.testcontainers 41 | junit-jupiter 42 | ${junit-jupter.version} 43 | test 44 | 45 | 46 | org.testcontainers 47 | mysql 48 | test 49 | 50 | 51 | 52 | 53 | io.springfox 54 | springfox-swagger2 55 | ${springfox_swagger.version} 56 | 57 | 58 | io.springfox 59 | springfox-boot-starter 60 | ${springfox-boot.version} 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-devtools 66 | runtime 67 | true 68 | 69 | 70 | 71 | mysql 72 | mysql-connector-java 73 | runtime 74 | 75 | 76 | 77 | org.projectlombok 78 | lombok 79 | true 80 | 81 | 82 | 83 | junit 84 | junit 85 | test 86 | 87 | 88 | 89 | org.springframework.boot 90 | spring-boot-starter-validation 91 | 92 | 93 | 94 | org.modelmapper 95 | modelmapper 96 | 3.1.0 97 | 98 | 99 | com.github.dozermapper 100 | dozer-core 101 | 6.5.2 102 | 103 | 104 | 105 | org.springframework.security 106 | spring-security-web 107 | 5.0.7.RELEASE 108 | 109 | 110 | org.springframework.security 111 | spring-security-config 112 | 5.0.7.RELEASE 113 | 114 | 115 | io.jsonwebtoken 116 | jjwt 117 | 0.7.0 118 | 119 | 120 | 121 | com.zaxxer 122 | HikariCP 123 | 3.3.1 124 | 125 | 127 | 128 | org.springframework.boot 129 | spring-boot-starter-cache 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | org.testcontainers 138 | testcontainers-bom 139 | ${testcontainers.version} 140 | pom 141 | import 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | org.springframework.boot 150 | spring-boot-maven-plugin 151 | 152 | 153 | 154 | org.projectlombok 155 | lombok 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/PdvApiApplication.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 | 8 | @SpringBootApplication 9 | @EnableCaching 10 | public class PdvApiApplication { 11 | public static void main(String[] args) { 12 | SpringApplication.run(PdvApiApplication.class, args); 13 | System.out.println(new BCryptPasswordEncoder().encode("123")); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/config/InternacionalizacaoConfig.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.config; 2 | 3 | import java.util.Locale; 4 | 5 | import org.springframework.context.MessageSource; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.support.ReloadableResourceBundleMessageSource; 9 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 10 | 11 | @Configuration 12 | public class InternacionalizacaoConfig { 13 | 14 | // definir arquivo fonte das mensagens 15 | @Bean 16 | public MessageSource messageSource() { 17 | ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); 18 | // nome do arquivo .properties que contem as mensagens 19 | messageSource.setBasename("classpath:mensagens"); 20 | messageSource.setDefaultEncoding("utf-8"); 21 | messageSource.setDefaultLocale(Locale.getDefault()); 22 | return messageSource; 23 | } 24 | 25 | // definir bean para ser utilizado pela validacao do bean validator 26 | @Bean 27 | public LocalValidatorFactoryBean validatorFactoryBean() { 28 | LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); 29 | bean.setValidationMessageSource(messageSource()); 30 | return bean; 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/config/MapperConfig.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.config; 2 | 3 | import org.modelmapper.ModelMapper; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | @Configuration 8 | public class MapperConfig { 9 | 10 | @Bean 11 | public ModelMapper modelMapper() { 12 | ModelMapper modelMapper = new ModelMapper(); 13 | return modelMapper; 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/config/SpringFoxConfig.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.config; 2 | 3 | import static springfox.documentation.builders.PathSelectors.regex; 4 | 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.List; 8 | 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.context.annotation.Import; 12 | 13 | import springfox.documentation.builders.RequestHandlerSelectors; 14 | import springfox.documentation.service.ApiInfo; 15 | import springfox.documentation.service.ApiKey; 16 | import springfox.documentation.service.AuthorizationScope; 17 | import springfox.documentation.service.Contact; 18 | import springfox.documentation.service.SecurityReference; 19 | import springfox.documentation.service.Tag; 20 | import springfox.documentation.spi.DocumentationType; 21 | import springfox.documentation.spi.service.contexts.SecurityContext; 22 | import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration; 23 | import springfox.documentation.spring.web.plugins.Docket; 24 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 25 | 26 | @Configuration 27 | @EnableSwagger2 28 | @Import(SpringDataRestConfiguration.class) 29 | public class SpringFoxConfig { 30 | 31 | public static final String PRODUCT_TAG = "Product"; 32 | public static final String SALE_TAG = "Sale"; 33 | public static final String PAYMENT_TAG = "Payment"; 34 | public static final String USER_TAG = "User"; 35 | 36 | public static final String AUTHORIZATION_HEADER = "Authorization"; 37 | 38 | // Adicionamos ApiKey para incluir o JWT como um cabeçalho de autorização: 39 | private ApiKey apiKey() { 40 | return new ApiKey("JWT", AUTHORIZATION_HEADER, "header"); 41 | } 42 | 43 | @Bean 44 | public Docket api() { 45 | return new Docket(DocumentationType.SWAGGER_2) 46 | .securityContexts(Arrays.asList(securityContext())) 47 | .securitySchemes(Arrays.asList(apiKey())) 48 | .select() 49 | .apis(RequestHandlerSelectors.basePackage("br.com.leandrosnazareth.pdvapi")) 50 | .paths(regex("/api/pdv.*")) 51 | .build() 52 | .tags(new Tag(PRODUCT_TAG, "Api REST PDV tag Product")) 53 | .tags(new Tag(SALE_TAG, "Api REST PDV tag Sale")) 54 | .tags(new Tag(PAYMENT_TAG, "Api REST Payment")) 55 | .tags(new Tag(USER_TAG, "Api REST User")) 56 | .apiInfo(metaInfo()); 57 | } 58 | 59 | private ApiInfo metaInfo() { 60 | return new ApiInfo( 61 | "PDV API REST", 62 | "API REST sistema de vendas em PDV.", 63 | "1.0", 64 | "Terms of Service", 65 | new Contact("Leandro Nazareth", "https://www.linkedin.com/in/leandrosnazareth", 66 | "leandrosnazareth@gmail.com"), 67 | "GNU GENERAL PUBLIC LICENSE", 68 | "https://www.gnu.org/licenses/gpl-3.0.pt-br.html", Collections.emptyList()); 69 | } 70 | 71 | // Em seguida, vamos configurar o JWT SecurityContext global AuthorizationScope 72 | private SecurityContext securityContext() { 73 | return SecurityContext.builder().securityReferences(defaultAuth()).build(); 74 | } 75 | 76 | private List defaultAuth() { 77 | AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); 78 | AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; 79 | authorizationScopes[0] = authorizationScope; 80 | return Arrays.asList(new SecurityReference("JWT", authorizationScopes)); 81 | } 82 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/config/WebConfig.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.config; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import org.springframework.boot.web.servlet.FilterRegistrationBean; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.core.Ordered; 10 | import org.springframework.web.cors.CorsConfiguration; 11 | import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 12 | import org.springframework.web.filter.CorsFilter; 13 | 14 | @Configuration 15 | public class WebConfig { 16 | 17 | // CORS CONFIGURATION 18 | @Bean 19 | public FilterRegistrationBean corsFilterRegistrationBean() { 20 | List all = Arrays.asList("*"); 21 | 22 | CorsConfiguration corsConfiguration = new CorsConfiguration(); 23 | corsConfiguration.setAllowedOriginPatterns(all); 24 | corsConfiguration.setAllowedHeaders(all); 25 | corsConfiguration.setAllowedMethods(all); 26 | corsConfiguration.setAllowCredentials(true); 27 | 28 | UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 29 | source.registerCorsConfiguration("/**", corsConfiguration); 30 | 31 | CorsFilter corsFilter = new CorsFilter(source); 32 | FilterRegistrationBean filter = new FilterRegistrationBean<>(corsFilter); 33 | filter.setOrder(Ordered.HIGHEST_PRECEDENCE); 34 | 35 | return filter; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import br.com.leandrosnazareth.pdvapi.config.SpringFoxConfig; 8 | import br.com.leandrosnazareth.pdvapi.domain.model.Payment; 9 | import io.swagger.annotations.Api; 10 | import io.swagger.annotations.ApiOperation; 11 | 12 | @RestController 13 | @RequestMapping("/api/pdv/payment") 14 | @Api(tags = { SpringFoxConfig.PAYMENT_TAG }) 15 | public class PaymentController { 16 | 17 | @ApiOperation(value = "Listar formas de pagamentos") 18 | @GetMapping 19 | public Payment[] findAllActive() { 20 | return Payment.values(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/controller/ProductController.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.controller; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import javax.validation.Valid; 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.data.domain.Page; 11 | import org.springframework.data.domain.PageRequest; 12 | import org.springframework.http.HttpStatus; 13 | import org.springframework.http.ResponseEntity; 14 | import org.springframework.web.bind.annotation.DeleteMapping; 15 | import org.springframework.web.bind.annotation.GetMapping; 16 | import org.springframework.web.bind.annotation.PathVariable; 17 | import org.springframework.web.bind.annotation.PostMapping; 18 | import org.springframework.web.bind.annotation.PutMapping; 19 | import org.springframework.web.bind.annotation.RequestBody; 20 | import org.springframework.web.bind.annotation.RequestMapping; 21 | import org.springframework.web.bind.annotation.RequestParam; 22 | import org.springframework.web.bind.annotation.ResponseStatus; 23 | import org.springframework.web.bind.annotation.RestController; 24 | 25 | import br.com.leandrosnazareth.pdvapi.config.SpringFoxConfig; 26 | import br.com.leandrosnazareth.pdvapi.dto.ProductDTO; 27 | import br.com.leandrosnazareth.pdvapi.exception.ResourceNotFoundException; 28 | import br.com.leandrosnazareth.pdvapi.service.ProductService; 29 | import br.com.leandrosnazareth.pdvapi.util.MensageConstant; 30 | import io.swagger.annotations.Api; 31 | import io.swagger.annotations.ApiOperation; 32 | 33 | @RestController 34 | @RequestMapping("/api/pdv/product") 35 | @Api(tags = { SpringFoxConfig.PRODUCT_TAG }) 36 | public class ProductController { 37 | 38 | @Autowired 39 | private ProductService productService; 40 | 41 | @ApiOperation(value = "Listar todos produtos com paginação") 42 | @GetMapping 43 | public Page findAllPagination( 44 | @RequestParam(value = "page", defaultValue = "0") Integer pagina, 45 | @RequestParam(value = "size", defaultValue = "5") Integer tamanhoPagina) { 46 | PageRequest pageRequest = PageRequest.of(pagina, tamanhoPagina); 47 | return productService.findAllDto(pageRequest); 48 | } 49 | 50 | @ApiOperation(value = "Listar todos produtos ativos") 51 | @GetMapping("all") 52 | public List findAllActive() { 53 | return productService.findAllActive(); 54 | } 55 | 56 | @GetMapping("{id}") 57 | @ApiOperation(value = "Buscar produto pelo ID") 58 | public ResponseEntity findProductById(@PathVariable Long id) 59 | throws ResourceNotFoundException { 60 | // retornar um Optional e converte para productDto, em caso nulo 61 | ProductDTO product = productService.findById(id) 62 | .orElseThrow(() -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + id)); 63 | return ResponseEntity.ok().body(product); 64 | } 65 | 66 | @GetMapping("active/{id}") 67 | @ApiOperation(value = "Buscar produto ativo pelo ID") 68 | public ResponseEntity findByIDAndActive(@PathVariable Integer id) 69 | throws ResourceNotFoundException { 70 | // retornar um Optional e converte para productDto, em caso nulo 71 | // retorna a exception 72 | ProductDTO productDto = productService.findByIdAndActive(id) 73 | .orElseThrow(() -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + id)); 74 | return ResponseEntity.ok().body(productDto); 75 | } 76 | 77 | @GetMapping("name/{name}") 78 | @ApiOperation(value = "Buscar qualquer produto pelo nome") 79 | public ResponseEntity findByName(@PathVariable String name) 80 | throws ResourceNotFoundException { 81 | // retornar um Optional e converte para productDto, em caso nulo 82 | // retorna a exception 83 | ProductDTO productDto = productService.findByName(name) 84 | .orElseThrow(() -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + name)); 85 | return ResponseEntity.ok().body(productDto); 86 | } 87 | 88 | @GetMapping("name/active/{name}") 89 | @ApiOperation(value = "Buscar produto ativos pelo nome") 90 | public ResponseEntity findByNameAndActive(@PathVariable String name) 91 | throws ResourceNotFoundException { 92 | // retornar um Optional e converte para productDto, em caso nulo 93 | // retorna a exception 94 | ProductDTO productDto = productService.findByNameAndActive(name) 95 | .orElseThrow(() -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + name)); 96 | return ResponseEntity.ok().body(productDto); 97 | } 98 | 99 | @PostMapping 100 | @ApiOperation(value = "Salva um produto") 101 | public ProductDTO createProduct(@Valid @RequestBody ProductDTO productDto) { 102 | return productService.save(productDto); 103 | } 104 | 105 | @ApiOperation(value = "Deletar produto pelo ID") 106 | @DeleteMapping("{id}") 107 | public Map deleteById(@PathVariable Long id) { 108 | ProductDTO productDto = productService.findByIdAndActive(id) 109 | .orElseThrow(() -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + id)); 110 | productService.delete(productDto); 111 | Map response = new HashMap<>(); 112 | response.put("deleted", Boolean.TRUE); 113 | return response; 114 | } 115 | 116 | @PutMapping("{id}") 117 | @ApiOperation(value = "Atualizar produto") 118 | public ResponseEntity updateProduct(@PathVariable(value = "id") Long id, 119 | @Valid @RequestBody ProductDTO productDto) throws ResourceNotFoundException { 120 | productService.findById(productDto.getId()) 121 | .orElseThrow(() -> new ResourceNotFoundException( 122 | MensageConstant.PRODUTO_NAO_ENCONTRADO + productDto.getId())); 123 | return ResponseEntity.ok(productService.save(productDto)); 124 | } 125 | 126 | @PutMapping("desativar/{id}") 127 | @ApiOperation(value = "Desativar um produto") 128 | @ResponseStatus(HttpStatus.CREATED) 129 | public void deactivate(@Valid @RequestBody Long id) { 130 | productService.deactivate(id); 131 | } 132 | 133 | @ApiOperation(value = "Retornar quantidade de Produtos") 134 | @GetMapping("count") 135 | public long countProducts() { 136 | return productService.countProducts(); 137 | } 138 | 139 | @ApiOperation(value = "Retornar quantidade de Produtos Ativos") 140 | @GetMapping("active") 141 | public long countProductActive() { 142 | return productService.countProductActive(); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/controller/SaleController.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.controller; 2 | 3 | import java.math.BigDecimal; 4 | import java.time.LocalDateTime; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import javax.validation.Valid; 10 | 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.data.domain.Page; 13 | import org.springframework.data.domain.PageRequest; 14 | import org.springframework.http.ResponseEntity; 15 | import org.springframework.web.bind.annotation.DeleteMapping; 16 | import org.springframework.web.bind.annotation.GetMapping; 17 | import org.springframework.web.bind.annotation.PathVariable; 18 | import org.springframework.web.bind.annotation.PostMapping; 19 | import org.springframework.web.bind.annotation.PutMapping; 20 | import org.springframework.web.bind.annotation.RequestBody; 21 | import org.springframework.web.bind.annotation.RequestMapping; 22 | import org.springframework.web.bind.annotation.RequestParam; 23 | import org.springframework.web.bind.annotation.RestController; 24 | 25 | import br.com.leandrosnazareth.pdvapi.config.SpringFoxConfig; 26 | import br.com.leandrosnazareth.pdvapi.dto.SaleDTO; 27 | import br.com.leandrosnazareth.pdvapi.exception.ResourceNotFoundException; 28 | import br.com.leandrosnazareth.pdvapi.service.SaleService; 29 | import io.swagger.annotations.Api; 30 | import io.swagger.annotations.ApiOperation; 31 | 32 | @RestController 33 | @RequestMapping("/api/pdv/sale") 34 | @Api(tags = { SpringFoxConfig.SALE_TAG }) 35 | public class SaleController { 36 | 37 | @Autowired 38 | private SaleService saleService; 39 | 40 | @GetMapping("{id}") 41 | @ApiOperation(value = "Buscar venda pelo ID") 42 | public ResponseEntity findSaleById(@PathVariable Long id) 43 | throws ResourceNotFoundException { 44 | // retornar um Optional e converte para saleDto, em caso nulo 45 | SaleDTO saleDto = saleService.findById(id) 46 | .orElseThrow(() -> new ResourceNotFoundException("Não foi encontrado uma venda com id: " + id)); 47 | return ResponseEntity.ok().body(saleDto); 48 | } 49 | 50 | @PostMapping 51 | @ApiOperation(value = "Salva uma venda") 52 | public SaleDTO createSale(@Valid @RequestBody SaleDTO saleDto) { 53 | return saleService.save(saleDto); 54 | } 55 | 56 | @ApiOperation(value = "Listar todas vendas ativas com paginação") 57 | @GetMapping 58 | public Page findAllPagination( 59 | @RequestParam(value = "page", defaultValue = "0") Integer pagina, 60 | @RequestParam(value = "size", defaultValue = "5") Integer tamanhoPagina) { 61 | PageRequest pageRequest = PageRequest.of(pagina, tamanhoPagina); 62 | return saleService.findAll(pageRequest); 63 | } 64 | 65 | @ApiOperation(value = "Deletar venda pelo ID") 66 | @DeleteMapping("{id}") 67 | public Map deleteById(@PathVariable Long id) 68 | throws ResourceNotFoundException { 69 | SaleDTO saleDto = saleService.findById(id) 70 | .orElseThrow(() -> new ResourceNotFoundException("Não foi encontrada uma venda com id: " + id)); 71 | saleService.delete(saleDto); 72 | Map response = new HashMap<>(); 73 | response.put("deleted", Boolean.TRUE); 74 | return response; 75 | } 76 | 77 | @PutMapping("{id}") 78 | @ApiOperation(value = "Atualizar venda") 79 | public ResponseEntity updateSale(@PathVariable(value = "id") Long id, 80 | @Valid @RequestBody SaleDTO saleDto) throws ResourceNotFoundException { 81 | saleService.findById(saleDto.getId()) 82 | .orElseThrow(() -> new ResourceNotFoundException( 83 | "Não foi encontrada uma venda com id: " + saleDto.getId())); 84 | return ResponseEntity.ok(saleService.save(saleDto)); 85 | } 86 | 87 | @GetMapping("total/{createdAt}") 88 | @ApiOperation(value = "Listar valor total de vendas no mes") 89 | public BigDecimal findValorTotalMonthAndYear2(@Valid @RequestBody LocalDateTime createdAt) { 90 | return saleService.findValorTotalMonthAndYear2(createdAt); 91 | } 92 | 93 | @GetMapping("somatotal") 94 | @ApiOperation(value = "Listar valor total de vendas") 95 | public BigDecimal findValorTotalSales() { 96 | return saleService.findValorTotalSales(); 97 | } 98 | 99 | @ApiOperation(value = "Retornar quantidade de vendas") 100 | @GetMapping("count") 101 | public long countSales() { 102 | return saleService.countSales(); 103 | } 104 | 105 | @ApiOperation(value = "Retornar ultimas 10 vendas") 106 | @GetMapping("limit") 107 | public List findTopByOrderByIdDesc() { 108 | return saleService.findTopByOrderByIdDesc(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.controller; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.cache.annotation.CacheEvict; 9 | import org.springframework.cache.annotation.CachePut; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12 | import org.springframework.web.bind.annotation.DeleteMapping; 13 | import org.springframework.web.bind.annotation.GetMapping; 14 | import org.springframework.web.bind.annotation.PathVariable; 15 | import org.springframework.web.bind.annotation.PostMapping; 16 | import org.springframework.web.bind.annotation.PutMapping; 17 | import org.springframework.web.bind.annotation.RequestBody; 18 | import org.springframework.web.bind.annotation.RequestMapping; 19 | import org.springframework.web.bind.annotation.RestController; 20 | 21 | import br.com.leandrosnazareth.pdvapi.config.SpringFoxConfig; 22 | import br.com.leandrosnazareth.pdvapi.domain.entity.Usuario; 23 | import br.com.leandrosnazareth.pdvapi.dto.UserDTO; 24 | import br.com.leandrosnazareth.pdvapi.dto.UserFullDTO; 25 | import br.com.leandrosnazareth.pdvapi.exception.ResourceNotFoundException; 26 | import br.com.leandrosnazareth.pdvapi.service.UserService; 27 | import br.com.leandrosnazareth.pdvapi.util.MensageConstant; 28 | import io.swagger.annotations.Api; 29 | import io.swagger.annotations.ApiOperation; 30 | 31 | @RestController 32 | @RequestMapping("/api/pdv/user") 33 | @Api(tags = { SpringFoxConfig.USER_TAG }) 34 | public class UserController { 35 | 36 | @Autowired 37 | private UserService usuarioService; 38 | 39 | @ApiOperation(value = "Buscar usuario pelo ID") 40 | @GetMapping("{id}") 41 | @CacheEvict(value = "cacheuserid", allEntries = true) // limpar o cache que não é utilizado a um tempo 42 | @CachePut("cacheuserid") // identificar as atualizações no bd e add ao cache 43 | public ResponseEntity findUserByID(@PathVariable Long id) 44 | throws ResourceNotFoundException { 45 | UserDTO usuarioDTO = usuarioService.findByIdDTO(id) 46 | .orElseThrow(() -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + id)); 47 | return ResponseEntity.ok().body(usuarioDTO); 48 | } 49 | 50 | @ApiOperation(value = "Deletar usuario pelo ID") 51 | @DeleteMapping("{id}") 52 | public Map deleteById(@PathVariable Long id) { 53 | Usuario usuario = usuarioService.findById(id) 54 | .orElseThrow(() -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + id)); 55 | usuarioService.delete(usuario); 56 | Map response = new HashMap<>(); 57 | response.put("deleted", Boolean.TRUE); 58 | return response; 59 | } 60 | 61 | @ApiOperation(value = "Listar todos usuarios") 62 | @GetMapping 63 | public List findAllUsuario() { 64 | return usuarioService.findAll(); 65 | } 66 | 67 | @ApiOperation(value = "Listar todos usuarios") 68 | @GetMapping("/testeuser/{id}") 69 | public Usuario findcomplete(@PathVariable Long id) { 70 | return usuarioService.findById(id).get(); 71 | } 72 | 73 | @ApiOperation(value = "Criar/cadastrar novo usuario") 74 | @PostMapping 75 | public UserDTO createUsuraio(@RequestBody UserFullDTO usuarioFullDTO) { 76 | String senhacriptografada = new BCryptPasswordEncoder().encode(usuarioFullDTO.getPassword()); 77 | usuarioFullDTO.setPassword(senhacriptografada); 78 | return usuarioService.save(usuarioFullDTO); 79 | } 80 | 81 | @ApiOperation(value = "Atualizar usuario") 82 | @PutMapping 83 | public UserDTO atualizar(@RequestBody UserFullDTO usuarioFullDTO) { 84 | Usuario usuarioTemporadrio = usuarioService.findById(usuarioFullDTO.getId()).orElseThrow( 85 | () -> new ResourceNotFoundException(MensageConstant.PRODUTO_NAO_ENCONTRADO + usuarioFullDTO.getId())); 86 | if (!usuarioFullDTO.getPassword().equals(usuarioTemporadrio.getPassword())) { // se Senhas for diferentes 87 | String senhaCriptografada = new BCryptPasswordEncoder().encode(usuarioFullDTO.getPassword()); 88 | usuarioFullDTO.setPassword(senhaCriptografada); 89 | } 90 | return usuarioService.save(usuarioFullDTO); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/entity/Product.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.entity; 2 | 3 | import java.io.Serializable; 4 | import java.math.BigDecimal; 5 | import java.time.LocalDateTime; 6 | import java.util.Objects; 7 | 8 | import javax.persistence.Column; 9 | import javax.persistence.Entity; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.PrePersist; 14 | import javax.persistence.PreUpdate; 15 | import javax.validation.constraints.NotEmpty; 16 | import javax.validation.constraints.NotNull; 17 | import javax.validation.constraints.Size; 18 | 19 | import org.springframework.data.annotation.CreatedDate; 20 | import org.springframework.data.annotation.LastModifiedBy; 21 | 22 | import com.fasterxml.jackson.annotation.JsonFormat; 23 | 24 | import lombok.AccessLevel; 25 | import lombok.AllArgsConstructor; 26 | import lombok.Data; 27 | import lombok.NoArgsConstructor; 28 | 29 | @Entity 30 | @Data 31 | @NoArgsConstructor(access = AccessLevel.PRIVATE) 32 | @AllArgsConstructor 33 | public class Product implements Serializable { 34 | 35 | private static final long serialVersionUID = 1L; 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.IDENTITY) 38 | private Long id; 39 | 40 | @Column(nullable = false, unique = true) 41 | @Size(min = 2, max = 50, message = "{campo.nome.tamanho}") 42 | @NotNull(message = "{campo.nome.obrigatorio}") 43 | @NotEmpty(message = "{campo.nome.obrigatorio}") 44 | private String name; 45 | 46 | @Column(nullable = false) 47 | @NotNull(message = "{campo.preco.obrigatorio}") 48 | private BigDecimal price; 49 | 50 | @Column(nullable = false) 51 | private Boolean active; 52 | 53 | @CreatedDate 54 | @Column(nullable = false, updatable = false) 55 | // formatar data no json de retorno 56 | @JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss") 57 | private LocalDateTime createdAt; 58 | 59 | @LastModifiedBy 60 | // formatar data no json de retorno 61 | @JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss") 62 | private LocalDateTime updatedAt; 63 | 64 | @PrePersist 65 | protected void prePersist() { 66 | if (Objects.isNull(active)) 67 | active = true; 68 | if (this.createdAt == null) 69 | createdAt = LocalDateTime.now(); 70 | if (this.updatedAt == null) 71 | updatedAt = LocalDateTime.now(); 72 | } 73 | 74 | @PreUpdate 75 | protected void preUpdate() { 76 | this.updatedAt = LocalDateTime.now(); 77 | } 78 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/entity/ProductSold.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.entity; 2 | 3 | import java.io.Serializable; 4 | import java.math.BigDecimal; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.OneToOne; 12 | import javax.validation.constraints.NotNull; 13 | 14 | import lombok.AccessLevel; 15 | import lombok.AllArgsConstructor; 16 | import lombok.Data; 17 | import lombok.NoArgsConstructor; 18 | 19 | @Entity 20 | @Data 21 | @NoArgsConstructor(access = AccessLevel.PRIVATE) 22 | @AllArgsConstructor 23 | public class ProductSold implements Serializable { 24 | 25 | private static final long serialVersionUID = 1L; 26 | @Id 27 | @GeneratedValue(strategy = GenerationType.IDENTITY) 28 | private Long id; 29 | @OneToOne 30 | private Product product; 31 | @Column(nullable = false) 32 | @NotNull(message = "{campo.preco.obrigatorio}") 33 | private BigDecimal price; 34 | @Column(nullable = false) 35 | @NotNull(message = "{campo.preco.obrigatorio}") 36 | private BigDecimal priceTotal; 37 | @Column(nullable = false) 38 | @NotNull(message = "{campo.quantidade.obrigatorio}") 39 | private int quantity; 40 | 41 | public void calcularPrecoTotal() { 42 | this.price = this.price.multiply(new BigDecimal(this.quantity)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/entity/Role.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.entity; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.validation.constraints.NotEmpty; 8 | import javax.validation.constraints.NotNull; 9 | import javax.validation.constraints.Size; 10 | 11 | import org.springframework.security.core.GrantedAuthority; 12 | 13 | import lombok.AccessLevel; 14 | import lombok.AllArgsConstructor; 15 | import lombok.EqualsAndHashCode; 16 | import lombok.Getter; 17 | import lombok.NoArgsConstructor; 18 | import lombok.Setter; 19 | import lombok.ToString; 20 | 21 | @Entity 22 | @Getter 23 | @Setter 24 | @NoArgsConstructor(access = AccessLevel.PRIVATE) 25 | @AllArgsConstructor(access = AccessLevel.PRIVATE) 26 | @ToString 27 | @EqualsAndHashCode 28 | public class Role implements GrantedAuthority { 29 | 30 | private static final long serialVersionUID = 1L; 31 | @Id 32 | @GeneratedValue(strategy = GenerationType.IDENTITY) 33 | private Long id; 34 | @Size(min = 3, max = 20, message = "Campo nome deve ter entre 3 e 20 caracteres") 35 | @NotNull(message = "Campo nome é obrigatório") 36 | @NotEmpty(message = "Campo nome é obrigatório") 37 | private String nameRole; // Papel, exemplo ROLE_SECRETARIO OU ROLE_GERENTE 38 | 39 | // implementação obriatório da interface GrantedAuthority 40 | @Override 41 | public String getAuthority() { 42 | return this.nameRole; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/entity/Sale.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.entity; 2 | 3 | import java.io.Serializable; 4 | import java.math.BigDecimal; 5 | import java.time.LocalDateTime; 6 | import java.util.List; 7 | 8 | import javax.persistence.CascadeType; 9 | import javax.persistence.Column; 10 | import javax.persistence.Entity; 11 | import javax.persistence.GeneratedValue; 12 | import javax.persistence.GenerationType; 13 | import javax.persistence.Id; 14 | import javax.persistence.OneToMany; 15 | import javax.persistence.PrePersist; 16 | import javax.persistence.PreUpdate; 17 | import javax.validation.constraints.NotNull; 18 | 19 | import org.springframework.data.annotation.CreatedDate; 20 | import org.springframework.data.annotation.LastModifiedBy; 21 | 22 | import com.fasterxml.jackson.annotation.JsonFormat; 23 | 24 | import br.com.leandrosnazareth.pdvapi.domain.model.Payment; 25 | import lombok.AccessLevel; 26 | import lombok.AllArgsConstructor; 27 | import lombok.Data; 28 | import lombok.NoArgsConstructor; 29 | 30 | @Entity 31 | @Data 32 | @NoArgsConstructor(access = AccessLevel.PRIVATE) 33 | @AllArgsConstructor 34 | public class Sale implements Serializable { 35 | 36 | private static final long serialVersionUID = 1L; 37 | @Id 38 | @GeneratedValue(strategy = GenerationType.IDENTITY) 39 | private Long id; 40 | 41 | @Column(nullable = false) 42 | @NotNull(message = "{campo.valortotal.obrigatorio}") 43 | private BigDecimal amount; 44 | @Column(nullable = false) 45 | @NotNull(message = "{campo.valorpago.obrigatorio}") 46 | private BigDecimal amountPaid; 47 | @Column(nullable = false) 48 | @NotNull(message = "{campo.troco.obrigatorio}") 49 | private BigDecimal difference; 50 | @Column(nullable = false) 51 | @NotNull(message = "{campo.formapagamento.obrigatorio}") 52 | private Payment payment; 53 | 54 | @OneToMany(cascade = CascadeType.PERSIST, orphanRemoval = true) 55 | private List productSolds; 56 | 57 | @CreatedDate 58 | @Column(nullable = false, updatable = false) 59 | @JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss") // formatar data no json de retorno 60 | private LocalDateTime createdAt; 61 | 62 | @LastModifiedBy 63 | @JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss") // formatar data no json de retorno 64 | private LocalDateTime updatedAt; 65 | 66 | @PrePersist 67 | protected void prePersist() { 68 | if (this.createdAt == null) 69 | createdAt = LocalDateTime.now(); 70 | if (this.updatedAt == null) 71 | updatedAt = LocalDateTime.now(); 72 | } 73 | 74 | @PreUpdate 75 | protected void preUpdate() { 76 | this.updatedAt = LocalDateTime.now(); 77 | } 78 | 79 | public void calcularValorTotal() { 80 | for (ProductSold productSold : this.productSolds) { 81 | this.amountPaid = this.amountPaid.add(productSold.getPrice()); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/entity/Usuario.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.entity; 2 | 3 | import java.util.Collection; 4 | import java.util.List; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.FetchType; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.OneToMany; 13 | import javax.validation.constraints.NotEmpty; 14 | import javax.validation.constraints.NotNull; 15 | import javax.validation.constraints.Size; 16 | 17 | import org.springframework.security.core.GrantedAuthority; 18 | import org.springframework.security.core.userdetails.UserDetails; 19 | 20 | import com.fasterxml.jackson.annotation.JsonIgnore; 21 | 22 | import lombok.AccessLevel; 23 | import lombok.AllArgsConstructor; 24 | import lombok.EqualsAndHashCode; 25 | import lombok.Getter; 26 | import lombok.NoArgsConstructor; 27 | import lombok.Setter; 28 | import lombok.ToString; 29 | 30 | @Entity 31 | @Getter 32 | @Setter 33 | @NoArgsConstructor(access = AccessLevel.PRIVATE) 34 | @AllArgsConstructor(access = AccessLevel.PRIVATE) 35 | @ToString 36 | @EqualsAndHashCode 37 | //implementar a interface UserDetails com os metodos de login do spring security 38 | public class Usuario implements UserDetails { 39 | 40 | private static final long serialVersionUID = 1L; 41 | @Id 42 | @GeneratedValue(strategy = GenerationType.IDENTITY) 43 | private Long id; 44 | @Size(min = 3, max = 20, message = "Campo login deve ter entre 3 e 20 caracteres") 45 | @NotNull(message = "Campo login é obrigatório") 46 | @NotEmpty(message = "Campo login é obrigatório") 47 | @Column(unique = true) 48 | private String username; 49 | @NotNull(message = "Campo senha é obrigatório") 50 | @NotEmpty(message = "Campo senha é obrigatório") 51 | private String password; 52 | @Size(min = 3, max = 50, message = "Campo nome deve ter entre 3 e 50 caracteres") 53 | @NotNull(message = "Campo nome é obrigatório") 54 | @NotEmpty(message = "Campo nome é obrigatório") 55 | private String name; 56 | private String token = ""; 57 | 58 | @OneToMany(fetch = FetchType.EAGER) 59 | private List roles; //Os papeis ou acessos 60 | 61 | //permissões do usuario role_admin role_gerente 62 | @Override 63 | public Collection getAuthorities() { 64 | return roles; 65 | } 66 | 67 | @JsonIgnore 68 | @Override 69 | public boolean isAccountNonExpired() { 70 | return true; 71 | } 72 | @JsonIgnore 73 | @Override 74 | public boolean isAccountNonLocked() { 75 | return true; 76 | } 77 | @JsonIgnore 78 | @Override 79 | public boolean isCredentialsNonExpired() { 80 | return true; 81 | } 82 | @JsonIgnore 83 | @Override 84 | public boolean isEnabled() { 85 | return true; 86 | } 87 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/model/Payment.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.model; 2 | 3 | public enum Payment { 4 | 5 | DINHEIRO("DINHEIRO"), 6 | CARTAO("CARTÃO"), 7 | VALEALIMENTACAO("VALE ALIMENTAÇÃO"), 8 | CHEQUE("CHEQUE"), 9 | FIADO("FIADO"); 10 | 11 | private String name; 12 | 13 | public String getDescricao() { 14 | return name; 15 | } 16 | 17 | private Payment(String name) { 18 | this.name = name; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.domain.Page; 6 | import org.springframework.data.domain.Pageable; 7 | import org.springframework.data.jpa.repository.JpaRepository; 8 | import org.springframework.data.jpa.repository.Modifying; 9 | import org.springframework.data.jpa.repository.Query; 10 | import org.springframework.data.repository.query.Param; 11 | import org.springframework.transaction.annotation.Transactional; 12 | 13 | import br.com.leandrosnazareth.pdvapi.domain.entity.Product; 14 | 15 | public interface ProductRepository extends JpaRepository { 16 | 17 | Optional findByActive(boolean active); 18 | 19 | Optional findByIdAndActive(Long id, Boolean active); 20 | 21 | // busca produto por nome ativo ou inativo 22 | Optional findByNameAndActive(String name, Boolean active); 23 | 24 | Optional findByName(String name); 25 | 26 | // desativar sem deletar 27 | @Modifying(clearAutomatically = true) 28 | @Transactional 29 | @Query("UPDATE Product r SET r.active = false WHERE r.id = :productId") 30 | void deactivate(@Param("productId") Long productId); 31 | 32 | Page findAllByActive(Pageable pageable, boolean active); 33 | 34 | long count(); 35 | 36 | @Query("SELECT COUNT(*) FROM Product WHERE active = true") 37 | long countProductActive(); 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/repository/SaleRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.repository; 2 | 3 | import java.math.BigDecimal; 4 | import java.time.LocalDateTime; 5 | import java.util.List; 6 | 7 | import org.springframework.data.jpa.repository.JpaRepository; 8 | import org.springframework.data.jpa.repository.Query; 9 | import org.springframework.data.repository.query.Param; 10 | 11 | import br.com.leandrosnazareth.pdvapi.domain.entity.Sale; 12 | 13 | public interface SaleRepository extends JpaRepository { 14 | 15 | @Query("SELECT SUM(amount) FROM Sale") 16 | BigDecimal findValorTotalSales(); 17 | 18 | @Query("SELECT MONTHNAME(createdAt), SUM(amount) FROM Sale GROUP BY YEAR(createdAt), MONTH(createdAt)") 19 | BigDecimal findValorTotalMonthAndYear2(@Param("createdAt") LocalDateTime createdAt); 20 | 21 | long count(); 22 | 23 | List findTop5ByOrderByIdDesc(); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/domain/repository/UsuerRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.domain.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.Modifying; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.stereotype.Repository; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import br.com.leandrosnazareth.pdvapi.domain.entity.Usuario; 10 | 11 | @Repository 12 | public interface UsuerRepository extends JpaRepository { 13 | //1 referencia primeiro paramentro 14 | @Query("select u from Usuario u where u.username = ?1") 15 | Usuario findUserByUsername(String username); 16 | 17 | Usuario findByUsername(String username); 18 | 19 | //1 e 2 para referenciar os paramentros 20 | @Transactional 21 | @Modifying 22 | @Query(nativeQuery = true, value = "update usuario set token = ?1 where username = ?2") 23 | void atualizaTokenUser(String token, String username); 24 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/dto/ProductDto.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.dto; 2 | 3 | import java.math.BigDecimal; 4 | import java.time.LocalDateTime; 5 | 6 | import lombok.Data; 7 | 8 | @Data 9 | public class ProductDTO { 10 | 11 | private Long id; 12 | private String name; 13 | private BigDecimal price; 14 | private Boolean active; 15 | private LocalDateTime createdAt; 16 | private LocalDateTime updatedAt; 17 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/dto/ProductSoldDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.dto; 2 | 3 | import java.math.BigDecimal; 4 | 5 | import lombok.Data; 6 | 7 | @Data 8 | public class ProductSoldDTO { 9 | 10 | private Long id; 11 | private ProductDTO product; 12 | private BigDecimal price; 13 | private BigDecimal priceTotal; 14 | private int quantity; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/dto/SaleDto.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.dto; 2 | 3 | import java.math.BigDecimal; 4 | import java.time.LocalDateTime; 5 | import java.util.List; 6 | 7 | import br.com.leandrosnazareth.pdvapi.domain.model.Payment; 8 | import lombok.Data; 9 | 10 | @Data 11 | public class SaleDTO { 12 | 13 | private Long id; 14 | private BigDecimal amount; 15 | private BigDecimal amountPaid; 16 | private BigDecimal difference; 17 | private Payment payment; 18 | private List productSolds; 19 | private LocalDateTime createdAt; 20 | private LocalDateTime updatedAt; 21 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class UserDTO { 7 | 8 | private String username; 9 | private String name; 10 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/dto/UserFullDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.dto; 2 | 3 | import java.util.List; 4 | 5 | import br.com.leandrosnazareth.pdvapi.domain.entity.Role; 6 | import lombok.Data; 7 | 8 | @Data 9 | public class UserFullDTO { 10 | private Long id; 11 | private String username; 12 | private String password; 13 | private String name; 14 | private String token = ""; 15 | private List roles; 16 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/exception/ErrorDetails.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.exception; 2 | 3 | import java.util.Date; 4 | 5 | public class ErrorDetails { 6 | private Date timestamp; 7 | private String message; 8 | private String details; 9 | 10 | public ErrorDetails(Date timestamp, String message, String details) { 11 | super(); 12 | this.timestamp = timestamp; 13 | this.message = message; 14 | this.details = details; 15 | } 16 | 17 | public Date getTimestamp() { 18 | return timestamp; 19 | } 20 | 21 | public String getMessage() { 22 | return message; 23 | } 24 | 25 | public String getDetails() { 26 | return details; 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/exception/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.exception; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.context.request.WebRequest; 10 | 11 | @ControllerAdvice 12 | public class GlobalExceptionHandler { 13 | @ExceptionHandler(ResourceNotFoundException.class) 14 | public ResponseEntity resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) { 15 | ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); 16 | return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND); 17 | } 18 | 19 | @ExceptionHandler(Exception.class) 20 | public ResponseEntity globleExcpetionHandler(Exception ex, WebRequest request) { 21 | ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); 22 | return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/exception/ResourceNotFoundException.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.NOT_FOUND) 7 | public class ResourceNotFoundException extends RuntimeException { 8 | 9 | private static final long serialVersionUID = 1L; 10 | 11 | public ResourceNotFoundException(String message) { 12 | super(message); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/security/ApplicationContextLoad.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.security; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.context.ApplicationContextAware; 7 | import org.springframework.stereotype.Component; 8 | 9 | @Component 10 | public class ApplicationContextLoad implements ApplicationContextAware { 11 | 12 | @Autowired 13 | private static ApplicationContext applicationContext2; 14 | 15 | @Override 16 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 17 | this.applicationContext2 = applicationContext; 18 | } 19 | 20 | public static ApplicationContext getApplicationContext() { 21 | return applicationContext2; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/security/JWTLoginFilter.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.security; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.FilterChain; 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.springframework.security.authentication.AuthenticationManager; 11 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 12 | import org.springframework.security.core.Authentication; 13 | import org.springframework.security.core.AuthenticationException; 14 | import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; 15 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 16 | 17 | import com.fasterxml.jackson.databind.ObjectMapper; 18 | 19 | import br.com.leandrosnazareth.pdvapi.domain.entity.Usuario; 20 | 21 | //Estabelece o gerenciador de Token 22 | public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter { 23 | 24 | // Configurando o gerenciador de autenticacao*/ 25 | protected JWTLoginFilter(String url, AuthenticationManager authenticationManager) { 26 | // Obriga a autenticar a URL 27 | super(new AntPathRequestMatcher(url)); 28 | // Gerenciador de autenticacao 29 | setAuthenticationManager(authenticationManager); 30 | } 31 | 32 | // Retorna o usuário ao processar a autenticação 33 | @Override 34 | public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) 35 | throws AuthenticationException, IOException, ServletException { 36 | // Pega usuario para validar 37 | Usuario user = new ObjectMapper().readValue(request.getInputStream(), Usuario.class); 38 | // Retorna o usuario (login, senha e acessos) 39 | return getAuthenticationManager() 40 | .authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword())); 41 | } 42 | 43 | @Override 44 | protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, 45 | Authentication authResult) throws IOException, ServletException { 46 | new JWTTokenAutenticacaoService().addAuthentication(response, authResult.getName()); 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/security/JWTTokenAutenticacaoService.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.security; 2 | 3 | import java.io.IOException; 4 | import java.util.Date; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 10 | import org.springframework.security.core.Authentication; 11 | import org.springframework.stereotype.Component; 12 | import org.springframework.stereotype.Service; 13 | 14 | import br.com.leandrosnazareth.pdvapi.domain.entity.Usuario; 15 | import br.com.leandrosnazareth.pdvapi.domain.repository.UsuerRepository; 16 | import io.jsonwebtoken.ExpiredJwtException; 17 | import io.jsonwebtoken.Jwts; 18 | import io.jsonwebtoken.SignatureAlgorithm; 19 | 20 | @Service 21 | @Component 22 | // responsavel por gerar e validar o token 23 | public class JWTTokenAutenticacaoService { 24 | 25 | // Tempo de validade do Token 2 dias, após esse tempo, será necessário logar 26 | // novamente 27 | private static final long EXPIRATION_TIME = 172800000; 28 | 29 | // Senha unica para compor a autenticacao e ajudar na segurança 30 | private static final String SECRET = "$enha$ecretaDaAPI"; 31 | 32 | // Prefixo padrão de Token 33 | private static final String TOKEN_PREFIX = "Bearer"; 34 | 35 | // identificação do cabeçalho da resposta 36 | private static final String HEADER_STRING = "Authorization"; 37 | 38 | // Gerar token de autenticado e adiconando ao cabeçalho e resposta Http 39 | public void addAuthentication(HttpServletResponse response, String username) throws IOException { 40 | // Montagem do Token 41 | String JWT = Jwts.builder() // Chama o gerador de Token 42 | .setSubject(username) // Adicona o usuario 43 | .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME)) // Tempo de expiração 44 | .signWith(SignatureAlgorithm.HS512, SECRET).compact(); // Compactação e algoritmos criptografia 45 | 46 | // Junta token com o prefixo 47 | String token = TOKEN_PREFIX + " " + JWT; // Bearer + o token gerado 48 | 49 | // Adiciona no cabeçalho http 50 | response.addHeader(HEADER_STRING, token); // Authorization: Bearer + o token gerado 51 | 52 | // atualizar token no banco de dados 53 | ApplicationContextLoad.getApplicationContext() 54 | .getBean(UsuerRepository.class).atualizaTokenUser(JWT, username); 55 | 56 | // Escreve o token como responsta no corpo http 57 | response.getWriter().write("{\"Authorization\": \"" + token + "\"}"); 58 | 59 | } 60 | 61 | // Retorna o usuário validado com token ou caso não sejá valido retorna null 62 | public Authentication getAuhentication(HttpServletRequest request, HttpServletResponse response) { 63 | // Pega o token enviado no cabeçalho http 64 | String token = request.getHeader(HEADER_STRING); 65 | try { 66 | if (token != null) { 67 | // pegar o token e retirar os espaços e o bearer 68 | String tokenLimpo = token.replace(TOKEN_PREFIX, "").trim(); 69 | // Faz a validação do token do usuário na requisição 70 | String user = Jwts.parser().setSigningKey(SECRET) // Bearer + o token gerado 71 | .parseClaimsJws(tokenLimpo) // somente o token 72 | .getBody().getSubject(); // get usuário 73 | if (user != null) { 74 | // retornar o usuário logado 75 | Usuario usuario = ApplicationContextLoad.getApplicationContext() 76 | .getBean(UsuerRepository.class).findUserByUsername(user); 77 | if (usuario != null) { 78 | // comparar token do banco com o fornecido pelo usuário 79 | if (tokenLimpo.equalsIgnoreCase(usuario.getToken())) { 80 | // retornar usuário com as permissões 81 | return new UsernamePasswordAuthenticationToken( 82 | usuario.getUsername(), 83 | usuario.getPassword(), 84 | usuario.getAuthorities()); 85 | } 86 | } 87 | } 88 | } 89 | } catch (ExpiredJwtException e) { 90 | try { 91 | // add ao response a msg de erro do token expirado 92 | response.getOutputStream().print("Seu token está expirado, faça novamente o login do usuário"); 93 | } catch (IOException e1) { 94 | e1.printStackTrace(); 95 | } 96 | } 97 | 98 | return null; // Não autorizado 99 | } 100 | 101 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/security/JwtApiAutenticacaoFilter.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.security; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.FilterChain; 6 | import javax.servlet.ServletException; 7 | import javax.servlet.ServletRequest; 8 | import javax.servlet.ServletResponse; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | import org.springframework.security.core.Authentication; 13 | import org.springframework.security.core.context.SecurityContextHolder; 14 | import org.springframework.web.filter.GenericFilterBean; 15 | 16 | //Filtro onde todas as requisições serão capturadas para autenticar 17 | public class JwtApiAutenticacaoFilter extends GenericFilterBean { 18 | 19 | @Override 20 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 21 | throws IOException, ServletException { 22 | // Estabelece a autenticação para a requisição 23 | Authentication authentication = new JWTTokenAutenticacaoService() 24 | .getAuhentication((HttpServletRequest) request, (HttpServletResponse) response); 25 | // Coloca o processo de autenticação no spring security 26 | SecurityContextHolder.getContext().setAuthentication(authentication); 27 | // Continua o processo 28 | chain.doFilter(request, response); 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/security/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.security; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | import br.com.leandrosnazareth.pdvapi.PdvApiApplication; 7 | 8 | public class ServletInitializer extends SpringBootServletInitializer { 9 | 10 | @Override 11 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 12 | return application.sources(PdvApiApplication.class); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/security/WebConfigSecurity.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.security; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 11 | import org.springframework.security.web.csrf.CookieCsrfTokenRepository; 12 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 13 | 14 | import br.com.leandrosnazareth.pdvapi.service.ImplementacaoUserDetailsSercice; 15 | 16 | /*Mapeaia URL, enderecos, autoriza ou bloqueia acessoa a URL*/ 17 | @Configuration 18 | @EnableWebSecurity 19 | public class WebConfigSecurity extends WebSecurityConfigurerAdapter { 20 | 21 | @Autowired 22 | private ImplementacaoUserDetailsSercice implementacaoUserDetailsSercice; 23 | 24 | // Configura as solicitações de acesso por Http 25 | @Override 26 | protected void configure(HttpSecurity http) throws Exception { 27 | 28 | http.csrf() 29 | // Ativando a proteção contra usuário que não estão validados por TOKEN 30 | .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) 31 | 32 | // Permitir todos acessarem a pagina pincipal index ou / 33 | .disable().authorizeRequests() 34 | .antMatchers("/api/login/").permitAll() 35 | .antMatchers("/index").permitAll() 36 | .antMatchers(AUTH_WHITELIST).permitAll()//liberar acesso ao endereço da doc swagger 37 | 38 | // Logout - Redireciona após o user deslogar do sistema 39 | .anyRequest().authenticated().and().logout().logoutSuccessUrl("/index") 40 | 41 | // Maperia URL de Logout e insvalida o usuário 42 | .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 43 | 44 | // Filtra requisições de login para autenticação 45 | .and().addFilterBefore(new JWTLoginFilter("/login", authenticationManager()), 46 | UsernamePasswordAuthenticationFilter.class) 47 | 48 | // Filtra demais requisições paa verificar a presenção do TOKEN JWT no HEADER 49 | // HTTP 50 | .addFilterBefore(new JwtApiAutenticacaoFilter(), UsernamePasswordAuthenticationFilter.class); 51 | } 52 | 53 | @Override 54 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { 55 | // Service que irá consultar o usuário no banco de dados 56 | auth.userDetailsService(implementacaoUserDetailsSercice) 57 | // Padrão de codigição de senha 58 | .passwordEncoder(new BCryptPasswordEncoder()); 59 | } 60 | 61 | private static final String[] AUTH_WHITELIST = { 62 | // -- swagger ui 63 | "/v2/api-docs", 64 | "/v3/api-docs", 65 | "/swagger-resources/**", 66 | "/swagger-ui/**", 67 | }; 68 | 69 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/service/ImplementacaoUserDetailsSercice.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.security.core.userdetails.User; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | import org.springframework.security.core.userdetails.UserDetailsService; 7 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 8 | import org.springframework.stereotype.Service; 9 | 10 | import br.com.leandrosnazareth.pdvapi.domain.entity.Usuario; 11 | import br.com.leandrosnazareth.pdvapi.domain.repository.UsuerRepository; 12 | 13 | @Service 14 | public class ImplementacaoUserDetailsSercice implements UserDetailsService { 15 | 16 | @Autowired 17 | private UsuerRepository usuarioRepository; 18 | 19 | @Override 20 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 21 | // Consulta no banco o usuario 22 | Usuario usuario = usuarioRepository.findUserByUsername(username); 23 | if (usuario == null) { 24 | throw new UsernameNotFoundException("Usuário não foi encontrado"); 25 | } 26 | return new User(usuario.getUsername(), usuario.getPassword(), usuario.getAuthorities()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.service; 2 | 3 | import java.util.List; 4 | import java.util.Optional; 5 | import java.util.stream.Collectors; 6 | 7 | import javax.validation.Valid; 8 | 9 | import org.modelmapper.ModelMapper; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.data.domain.Page; 12 | import org.springframework.data.domain.Pageable; 13 | import org.springframework.stereotype.Service; 14 | 15 | import com.github.dozermapper.core.DozerBeanMapperBuilder; 16 | 17 | import br.com.leandrosnazareth.pdvapi.domain.entity.Product; 18 | import br.com.leandrosnazareth.pdvapi.domain.repository.ProductRepository; 19 | import br.com.leandrosnazareth.pdvapi.dto.ProductDTO; 20 | 21 | @Service 22 | public class ProductService { 23 | 24 | @Autowired 25 | private ModelMapper modelMapper; 26 | 27 | @Autowired 28 | private ProductRepository productRepository; 29 | 30 | public Page findAllDto(Pageable pageable) { 31 | return productRepository.findAll(pageable) 32 | .map((product -> DozerBeanMapperBuilder.buildDefault()// converte pag para pga 33 | .map(product, ProductDTO.class))); 34 | } 35 | 36 | public List findAllActive() { 37 | // busca lista de produtos e mapeia para lista de produtosdto 38 | return productRepository.findByActive(true).stream() 39 | .map(product -> modelMapper.map(product, ProductDTO.class)) 40 | .collect(Collectors.toList()); 41 | } 42 | 43 | public ProductDTO save(ProductDTO productDto) { 44 | Product product = productRepository.save(modelMapper.map(productDto, Product.class)); 45 | return modelMapper.map(product, ProductDTO.class); 46 | } 47 | 48 | public Optional findById(long id) { 49 | return productRepository.findById(id) 50 | .map(product -> modelMapper.map(product, ProductDTO.class)); 51 | } 52 | 53 | public Optional findByName(String name) { 54 | return productRepository.findByName(name) 55 | .map(product -> modelMapper.map(product, ProductDTO.class)); 56 | } 57 | 58 | public Optional findByIdAndActive(long id) { 59 | return productRepository.findByIdAndActive(id, true) 60 | .map(product -> modelMapper.map(product, ProductDTO.class)); 61 | } 62 | 63 | public void delete(ProductDTO productDto) { 64 | Product product = modelMapper.map(productDto, Product.class); 65 | productRepository.delete(product); 66 | } 67 | 68 | public Optional findByNameAndActive(String name) { 69 | return productRepository.findByNameAndActive(name, true) 70 | .map(product -> modelMapper.map(product, ProductDTO.class)); 71 | } 72 | 73 | public void deactivate(@Valid Long id) { 74 | productRepository.deactivate(id); 75 | } 76 | 77 | public long countProducts() { 78 | return productRepository.count(); 79 | } 80 | 81 | public long countProductActive() { 82 | return productRepository.countProductActive(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/service/SaleService.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.service; 2 | 3 | import java.math.BigDecimal; 4 | import java.time.LocalDateTime; 5 | import java.util.List; 6 | import java.util.Optional; 7 | import java.util.stream.Collectors; 8 | 9 | import javax.validation.Valid; 10 | 11 | import org.modelmapper.ModelMapper; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.data.domain.Page; 14 | import org.springframework.data.domain.Pageable; 15 | import org.springframework.stereotype.Service; 16 | 17 | import com.github.dozermapper.core.DozerBeanMapperBuilder; 18 | 19 | import br.com.leandrosnazareth.pdvapi.domain.entity.Sale; 20 | import br.com.leandrosnazareth.pdvapi.domain.repository.SaleRepository; 21 | import br.com.leandrosnazareth.pdvapi.dto.SaleDTO; 22 | 23 | @Service 24 | public class SaleService { 25 | 26 | @Autowired 27 | private ModelMapper modelMapper; 28 | 29 | @Autowired 30 | private SaleRepository saletRepository; 31 | 32 | public SaleDTO save(SaleDTO saleDto) { 33 | Sale sale = saletRepository.save(modelMapper.map(saleDto, Sale.class)); 34 | return modelMapper.map(sale, SaleDTO.class); 35 | } 36 | 37 | public Page findAll(Pageable pageable) { 38 | return saletRepository.findAll(pageable) 39 | .map((sale -> DozerBeanMapperBuilder.buildDefault()// converte pag para page 40 | .map(sale, SaleDTO.class))); 41 | } 42 | 43 | public Optional findById(long id) { 44 | return saletRepository.findById(id) 45 | .map(sale -> modelMapper.map(sale, SaleDTO.class)); 46 | } 47 | 48 | public void delete(SaleDTO saletDto) { 49 | Sale sale = modelMapper.map(saletDto, Sale.class); 50 | saletRepository.delete(sale); 51 | } 52 | 53 | public BigDecimal findValorTotalMonthAndYear2(@Valid LocalDateTime createdAt) { 54 | return saletRepository.findValorTotalMonthAndYear2(createdAt); 55 | } 56 | 57 | public BigDecimal findValorTotalSales() { 58 | return saletRepository.findValorTotalSales(); 59 | } 60 | 61 | public long countSales() { 62 | return saletRepository.count(); 63 | } 64 | 65 | public List findTopByOrderByIdDesc() { 66 | return saletRepository.findTop5ByOrderByIdDesc().stream() 67 | .map(sale -> modelMapper.map(sale, SaleDTO.class)) 68 | .collect(Collectors.toList()); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/service/UserService.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.service; 2 | 3 | import java.util.List; 4 | import java.util.Optional; 5 | import java.util.stream.Collectors; 6 | 7 | import org.modelmapper.ModelMapper; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import br.com.leandrosnazareth.pdvapi.domain.entity.Usuario; 12 | import br.com.leandrosnazareth.pdvapi.domain.repository.UsuerRepository; 13 | import br.com.leandrosnazareth.pdvapi.dto.UserDTO; 14 | import br.com.leandrosnazareth.pdvapi.dto.UserFullDTO; 15 | 16 | @Service 17 | public class UserService { 18 | 19 | @Autowired 20 | private ModelMapper modelMapper; 21 | 22 | @Autowired 23 | private UsuerRepository usuarioRepository; 24 | 25 | public Optional findByIdDTO(long id) { 26 | return usuarioRepository.findById(id) 27 | .map(usuario -> modelMapper.map(usuario, UserDTO.class)); 28 | } 29 | 30 | public Optional findById(long id) { 31 | return usuarioRepository.findById(id); 32 | } 33 | 34 | public void delete(Usuario usuario) { 35 | usuarioRepository.delete(usuario); 36 | } 37 | 38 | public List findAll() { 39 | return usuarioRepository.findAll().stream() 40 | .map(usuario -> modelMapper.map(usuario, UserDTO.class)) 41 | .collect(Collectors.toList()); 42 | } 43 | 44 | public UserDTO save(UserFullDTO usuarioFullDTO) { 45 | Usuario usuario = modelMapper.map(usuarioFullDTO, Usuario.class); 46 | return modelMapper.map(usuarioRepository.save(usuario), UserDTO.class); 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/br/com/leandrosnazareth/pdvapi/util/MensageConstant.java: -------------------------------------------------------------------------------- 1 | package br.com.leandrosnazareth.pdvapi.util; 2 | 3 | public final class MensageConstant { 4 | 5 | public static final String PRODUTO_NAO_ENCONTRADO = "Não foi encontrado o produto: "; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # ================================= 2 | # = Server 3 | # ================================= 4 | server: 5 | port: 8080 6 | # servlet: 7 | # context-path: /v1 # controle de versao 8 | 9 | # ================================= 10 | # = Spring 11 | # ================================= 12 | spring: 13 | profiles: 14 | active: dev 15 | datasource: 16 | driver-class-name: com.mysql.cj.jdbc.Driver 17 | url: jdbc:mysql://localhost:3306/pdvdb?createDatabaseIfNotExist=true&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true 18 | username: root 19 | password: root 20 | type: com.zaxxer.hikari.HikariDataSource 21 | jpa: 22 | database-platform: org.hibernate.dialect.MySQL8Dialect 23 | generate-ddl: true 24 | show-sql: true 25 | properties: 26 | hibernate: 27 | # update atualiza os schemas de banco de dados, none, validate, update, create, create-drop 28 | ddl-auto: update 29 | mvc: 30 | pathmatch: 31 | matching-strategy: ANT_PATH_MATCHER 32 | hiddenmethod: 33 | filter: 34 | enabled: true 35 | 36 | 37 | # ================================= 38 | # = Logging - INFO - TRACE - DEBUG - ERROR 39 | # ================================= 40 | logging: 41 | level: 42 | org-springframework: INFO 43 | # org-flywaydb: INFO -------------------------------------------------------------------------------- /src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | %d{HH:mm:ss,MMM} %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/main/resources/mensagens.properties: -------------------------------------------------------------------------------- 1 | campo.nome.obrigatorio= O campo nome é obrigatorio! 2 | campo.nome.tamanho= Nome deve ter entre 2 e 50 caracteres! 3 | campo.preco.obrigatorio= O campo preço é obrigatorio! 4 | campo.quantidade.obrigatorio=O campo quantidade é obrigatorio! 5 | campo.valortotal.obrigatorio= Valor total é obrigatorio! 6 | campo.valorpago.obrigatorio= Valor pago é obrigatorio! 7 | campo.troco.obrigatorio= Troco é obrigatorio! 8 | campo.formapagamento.obrigatorio= Forma de pagamento é obrigatótio! -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/integration/ProdutoIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.integration; 2 | 3 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_ID; 4 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_NOME; 5 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_VALOR; 6 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_ACTIVE; 7 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_DATE_CREATE; 8 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_DATE_UPDATE; 9 | import static org.junit.jupiter.api.Assertions.assertEquals; 10 | import static org.junit.jupiter.api.Assertions.assertFalse; 11 | import static org.junit.jupiter.api.Assertions.assertNotNull; 12 | import static org.junit.jupiter.api.Assertions.assertTrue; 13 | 14 | import org.junit.jupiter.api.Test; 15 | import org.springframework.beans.factory.annotation.Autowired; 16 | import org.springframework.boot.test.context.SpringBootTest; 17 | import org.springframework.test.context.ActiveProfiles; 18 | 19 | import br.com.leandrosnazareth.pdvapi.domain.entity.Product; 20 | import br.com.leandrosnazareth.pdvapi.domain.repository.ProductRepository; 21 | 22 | @ActiveProfiles(profiles = "integration-test") 23 | @SpringBootTest(classes = br.com.leandrosnazareth.pdvapi.PdvApiApplication.class) 24 | public class ProdutoIntegrationTest { 25 | 26 | @Autowired 27 | ProductRepository productRepository; 28 | 29 | @Test 30 | public void testFindallProdutos() { 31 | assertNotNull(productRepository.findAll()); 32 | } 33 | 34 | @Test 35 | public void testCreteProduto() { 36 | var product = this.criarProdutoPadrao(); 37 | 38 | var productSalvo = productRepository.save(product); 39 | assertEquals(product.getName(), productSalvo.getName()); 40 | assertEquals(product.getPrice(), productSalvo.getPrice()); 41 | 42 | productRepository.delete(productSalvo); 43 | assertFalse(productRepository.findById(productSalvo.getId()).isPresent()); 44 | } 45 | 46 | @Test 47 | public void testDeactivateProduct() { 48 | var product = this.criarProdutoPadrao(); 49 | 50 | var productSalvo = productRepository.save(product); 51 | assertEquals(product.getName(), productSalvo.getName()); 52 | assertEquals(product.getPrice(), productSalvo.getPrice()); 53 | 54 | productRepository.deactivate(productSalvo.getId()); 55 | 56 | assertFalse(productRepository.findByIdAndActive(productSalvo.getId(), true).isPresent()); 57 | assertTrue(productRepository.findByIdAndActive(productSalvo.getId(), false).isPresent()); 58 | 59 | productRepository.delete(productSalvo); 60 | assertFalse(productRepository.findById(productSalvo.getId()).isPresent()); 61 | } 62 | 63 | @Test 64 | public void testFindProdutoByID() { 65 | var product = this.criarProdutoPadrao(); 66 | 67 | var productSalvo = productRepository.save(product); 68 | assertEquals(product.getName(), productSalvo.getName()); 69 | assertEquals(product.getPrice(), productSalvo.getPrice()); 70 | 71 | var productfound = productRepository.findById(productSalvo.getId()).get(); 72 | 73 | assertEquals(productSalvo.getId(), productfound.getId()); 74 | assertEquals(productSalvo.getName(), productfound.getName()); 75 | assertEquals(productSalvo.getPrice(), productfound.getPrice()); 76 | 77 | productRepository.delete(productfound); 78 | assertFalse(productRepository.findById(productSalvo.getId()).isPresent()); 79 | } 80 | 81 | @Test 82 | public void testFindProdutoByIDActive() { 83 | var product = this.criarProdutoPadrao(); 84 | 85 | var productSalvo = productRepository.save(product); 86 | assertEquals(product.getName(), productSalvo.getName()); 87 | assertEquals(product.getPrice(), productSalvo.getPrice()); 88 | 89 | var productfound = productRepository.findByIdAndActive(productSalvo.getId(), true).get(); 90 | 91 | assertEquals(productSalvo.getId(), productfound.getId()); 92 | assertEquals(productSalvo.getName(), productfound.getName()); 93 | assertEquals(productSalvo.getPrice(), productfound.getPrice()); 94 | 95 | productRepository.delete(productfound); 96 | assertFalse(productRepository.findById(productSalvo.getId()).isPresent()); 97 | } 98 | 99 | @Test 100 | public void testFindProdutoByNameActive() { 101 | var product = this.criarProdutoPadrao(); 102 | 103 | var productSalvo = productRepository.save(product); 104 | assertEquals(product.getName(), productSalvo.getName()); 105 | assertEquals(product.getPrice(), productSalvo.getPrice()); 106 | 107 | var productfound = productRepository.findByNameAndActive(productSalvo.getName(), true).get(); 108 | 109 | assertEquals(productSalvo.getId(), productfound.getId()); 110 | assertEquals(productSalvo.getName(), productfound.getName()); 111 | assertEquals(productSalvo.getPrice(), productfound.getPrice()); 112 | 113 | productRepository.delete(productfound); 114 | assertFalse(productRepository.findById(productSalvo.getId()).isPresent()); 115 | } 116 | 117 | @Test 118 | public void testFindProdutoByIDNotActive() { 119 | var product = this.criarProdutoNotActive(); 120 | 121 | var productSalvo = productRepository.save(product); 122 | assertEquals(product.getName(), productSalvo.getName()); 123 | assertEquals(product.getPrice(), productSalvo.getPrice()); 124 | assertEquals(product.getActive(), productSalvo.getActive()); 125 | 126 | assertFalse(productRepository.findByIdAndActive(productSalvo.getId(), true).isPresent()); 127 | 128 | productRepository.delete(productSalvo); 129 | assertFalse(productRepository.findById(productSalvo.getId()).isPresent()); 130 | } 131 | 132 | @Test 133 | public void testFindProdutoByName() { 134 | var product = this.criarProdutoPadrao(); 135 | 136 | var productSalvo = productRepository.save(product); 137 | assertEquals(product.getName(), productSalvo.getName()); 138 | assertEquals(product.getPrice(), productSalvo.getPrice()); 139 | 140 | var productfound = productRepository.findByName(product.getName()).get(); 141 | 142 | assertEquals(productSalvo.getId(), productfound.getId()); 143 | assertEquals(productSalvo.getName(), productfound.getName()); 144 | assertEquals(productSalvo.getPrice(), productfound.getPrice()); 145 | 146 | productRepository.delete(productfound); 147 | assertFalse(productRepository.findById(productSalvo.getId()).isPresent()); 148 | } 149 | 150 | public Product criarProdutoPadrao() { 151 | var product = new Product( 152 | DEFAULT_PRODUTO_ID, 153 | DEFAULT_PRODUTO_NOME, 154 | DEFAULT_PRODUTO_VALOR, 155 | DEFAULT_PRODUTO_ACTIVE, 156 | DEFAULT_DATE_CREATE, 157 | DEFAULT_DATE_UPDATE); 158 | return product; 159 | } 160 | 161 | public Product criarProdutoNotActive() { 162 | var product = new Product( 163 | DEFAULT_PRODUTO_ID, 164 | DEFAULT_PRODUTO_NOME, 165 | DEFAULT_PRODUTO_VALOR, 166 | false, 167 | DEFAULT_DATE_CREATE, 168 | DEFAULT_DATE_UPDATE); 169 | return product; 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/integration/SaleIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.integration; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertFalse; 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; 6 | 7 | import java.math.BigDecimal; 8 | 9 | import org.junit.jupiter.api.Test; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.test.context.ActiveProfiles; 13 | 14 | import com.fasterxml.jackson.core.JsonProcessingException; 15 | import com.fasterxml.jackson.databind.DeserializationFeature; 16 | import com.fasterxml.jackson.databind.ObjectMapper; 17 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 18 | 19 | import br.com.blsoft.pdvapi.util.ProductDataTest; 20 | import br.com.blsoft.pdvapi.util.SaleDataTest; 21 | import br.com.leandrosnazareth.pdvapi.domain.entity.Sale; 22 | import br.com.leandrosnazareth.pdvapi.domain.repository.ProductRepository; 23 | import br.com.leandrosnazareth.pdvapi.domain.repository.SaleRepository; 24 | 25 | @ActiveProfiles(profiles = "integration-test") 26 | // @ActiveProfiles(profiles = "aplication") 27 | @SpringBootTest(classes = br.com.leandrosnazareth.pdvapi.PdvApiApplication.class) 28 | public class SaleIntegrationTest { 29 | 30 | @Autowired 31 | SaleRepository saleRepository; 32 | @Autowired 33 | ProductRepository productRepository; 34 | 35 | 36 | @Test 37 | public void testFindallSales() { 38 | assertNotNull(saleRepository.findAll()); 39 | } 40 | 41 | @Test 42 | public void testCreateSale() throws JsonProcessingException { 43 | var product = ProductDataTest.newProductSemId(); 44 | productRepository.save(product); 45 | 46 | var valorPago = new BigDecimal("66.65"); 47 | 48 | var products = productRepository.findAll(); 49 | 50 | var sale = new SaleDataTest().newSaleBuilderSemID(products); 51 | 52 | Sale saleSave = saleRepository.save(sale); 53 | 54 | //imprimir json 55 | ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 56 | objectMapper.registerModule(new JavaTimeModule()); 57 | var json = objectMapper.writeValueAsString(saleSave); 58 | System.out.println(json.toString()+"AQUI"); 59 | 60 | assertEquals(sale.getAmountPaid(), saleSave.getAmountPaid()); 61 | assertEquals(valorPago, saleSave.getAmountPaid()); 62 | 63 | saleRepository.delete(sale); 64 | assertFalse(saleRepository.findById(sale.getId()).isPresent()); 65 | 66 | productRepository.delete(product); 67 | assertFalse(productRepository.findById(product.getId()).isPresent()); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/unit/ProductSoldTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.unit; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.math.BigDecimal; 6 | 7 | import org.junit.jupiter.api.Test; 8 | 9 | import br.com.leandrosnazareth.pdvapi.domain.entity.ProductSold; 10 | 11 | public class ProductSoldTest { 12 | 13 | @Test 14 | public void calcPriceTotalProductSoldQuantity5() { 15 | var productSold = new ProductSold( 16 | null, 17 | null, 18 | new BigDecimal("20"), 19 | new BigDecimal("0"), 20 | 5); 21 | productSold.calcularPrecoTotal(); 22 | var expected = new BigDecimal("100"); 23 | assertEquals(expected, productSold.getPrice()); 24 | } 25 | 26 | @Test 27 | public void calcPriceTotalProductSoldQuantity1000() { 28 | var productSold = new ProductSold( 29 | null, 30 | null, 31 | new BigDecimal("20"), 32 | new BigDecimal("0"), 33 | 100); 34 | productSold.calcularPrecoTotal(); 35 | var expected = new BigDecimal("2000"); 36 | assertEquals(expected, productSold.getPrice()); 37 | } 38 | 39 | @Test 40 | public void calcPriceTotalProductSoldQuantity5Real() { 41 | // productSolds 42 | var productSold = new ProductSold( 43 | null, 44 | null, 45 | new BigDecimal("3.25"), 46 | new BigDecimal("0"), 47 | 5); 48 | productSold.calcularPrecoTotal(); 49 | var expected = new BigDecimal("16.25"); 50 | assertEquals(expected, productSold.getPrice()); 51 | } 52 | 53 | @Test 54 | public void calcPriceTotalProductSoldQuantityZero() { 55 | // productSolds 56 | var productSold = new ProductSold( 57 | null, 58 | null, 59 | new BigDecimal("3.25"), 60 | new BigDecimal("0"), 61 | 0); 62 | productSold.calcularPrecoTotal(); 63 | var expected = new BigDecimal("0.00"); 64 | assertEquals(expected, productSold.getPrice()); 65 | } 66 | 67 | @Test 68 | public void calcPriceTotalProductSoldPriceZero() { 69 | // productSolds 70 | var productSold = new ProductSold( 71 | null, 72 | null, 73 | new BigDecimal("0"), 74 | new BigDecimal("0"), 75 | 5); 76 | productSold.calcularPrecoTotal(); 77 | var expected = new BigDecimal("0"); 78 | assertEquals(expected, productSold.getPrice()); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/unit/ProdutoTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.unit; 2 | 3 | public class ProdutoTest { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/util/ConstantTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.util; 2 | 3 | import java.math.BigDecimal; 4 | import java.time.LocalDateTime; 5 | 6 | public final class ConstantTest { 7 | 8 | public static final long DEFAULT_PRODUTO_ID = 1L; 9 | public static final long DEFAULT_SALE_ID = 1L; 10 | public static final String DEFAULT_PRODUTO_NOME = "COCA COLA 2L"; 11 | public static final BigDecimal DEFAULT_PRODUTO_VALOR = new BigDecimal("13.33"); 12 | public static final boolean DEFAULT_PRODUTO_ACTIVE = true; 13 | public static final LocalDateTime DEFAULT_DATE_CREATE = LocalDateTime.now(); 14 | public static final LocalDateTime DEFAULT_DATE_UPDATE = LocalDateTime.now(); 15 | public static final int DEFAULT_PROD_SOLD_QUANTIDADE_5 = 5; 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/util/ProductDataTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.util; 2 | 3 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_DATE_CREATE; 4 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_DATE_UPDATE; 5 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_ACTIVE; 6 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_ID; 7 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_NOME; 8 | import static br.com.blsoft.pdvapi.util.ConstantTest.DEFAULT_PRODUTO_VALOR; 9 | 10 | import br.com.leandrosnazareth.pdvapi.domain.entity.Product; 11 | 12 | public final class ProductDataTest { 13 | 14 | public static Product newProductComplete() { 15 | var product = new Product( 16 | DEFAULT_PRODUTO_ID, 17 | DEFAULT_PRODUTO_NOME, 18 | DEFAULT_PRODUTO_VALOR, 19 | DEFAULT_PRODUTO_ACTIVE, 20 | DEFAULT_DATE_CREATE, 21 | DEFAULT_DATE_UPDATE); 22 | return product; 23 | } 24 | 25 | public static Product newProductSemId() { 26 | var product = new Product( 27 | null, 28 | DEFAULT_PRODUTO_NOME, 29 | DEFAULT_PRODUTO_VALOR, 30 | DEFAULT_PRODUTO_ACTIVE, 31 | DEFAULT_DATE_CREATE, 32 | DEFAULT_DATE_UPDATE); 33 | return product; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/util/ProductSoldDataTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import br.com.leandrosnazareth.pdvapi.domain.entity.Product; 7 | import br.com.leandrosnazareth.pdvapi.domain.entity.ProductSold; 8 | 9 | public class ProductSoldDataTest { 10 | 11 | public List newProductsSoldsSemID(List products) { 12 | List productSolds = new ArrayList<>(); 13 | ProductSold productSold; 14 | for (Product product : products) { 15 | productSold = new ProductSold( 16 | null, 17 | product, 18 | product.getPrice(), 19 | product.getPrice(), 20 | 5); 21 | productSold.calcularPrecoTotal(); 22 | productSolds.add(productSold); 23 | } 24 | return productSolds; 25 | } 26 | 27 | public ProductSold newProductSoldSemID(Product product) { 28 | var productSold = new ProductSold( 29 | null, 30 | product, 31 | product.getPrice(), 32 | product.getPrice(), 33 | 5); 34 | productSold.calcularPrecoTotal(); 35 | return productSold; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/br/com/blsoft/pdvapi/util/SaleDataTest.java: -------------------------------------------------------------------------------- 1 | package br.com.blsoft.pdvapi.util; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.List; 5 | 6 | import br.com.leandrosnazareth.pdvapi.domain.entity.Product; 7 | import br.com.leandrosnazareth.pdvapi.domain.entity.Sale; 8 | import br.com.leandrosnazareth.pdvapi.domain.model.Payment; 9 | 10 | public class SaleDataTest { 11 | 12 | public Sale newSaleBuilderSemID(List products) { 13 | var sale = new Sale( 14 | null, 15 | new BigDecimal("0"), 16 | new BigDecimal("0"), 17 | new BigDecimal("0"), 18 | Payment.DINHEIRO, 19 | new ProductSoldDataTest().newProductsSoldsSemID(products), 20 | null, 21 | null); 22 | sale.calcularValorTotal(); 23 | return sale; 24 | } 25 | } -------------------------------------------------------------------------------- /src/test/resources/application-integration-test.yml: -------------------------------------------------------------------------------- 1 | # ================================= 2 | # = Spring conectar com db testcontainers do docker 3 | # ================================= 4 | spring: 5 | datasource: 6 | driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver 7 | url: jdbc:tc:mysql://localhost/pdvdb 8 | username: admin 9 | password: admin 10 | main: 11 | allow-bean-definition-overriding: true --------------------------------------------------------------------------------