├── .gitignore ├── .mvn └── wrapper │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── dailycodework │ │ └── dreamshops │ │ ├── DreamShopsApplication.java │ │ ├── config │ │ └── ShopConfig.java │ │ ├── controller │ │ ├── CartController.java │ │ ├── CartItemController.java │ │ ├── CategoryController.java │ │ ├── ImageController.java │ │ ├── OrderController.java │ │ ├── ProductController.java │ │ └── UserController.java │ │ ├── dto │ │ ├── CartDto.java │ │ ├── CartItemDto.java │ │ ├── ImageDto.java │ │ ├── OrderDto.java │ │ ├── OrderItemDto.java │ │ ├── ProductDto.java │ │ └── UserDto.java │ │ ├── enums │ │ └── OrderStatus.java │ │ ├── exceptions │ │ ├── AlreadyExistsException.java │ │ ├── ProductNotFoundException.java │ │ └── ResourceNotFoundException.java │ │ ├── model │ │ ├── Cart.java │ │ ├── CartItem.java │ │ ├── Category.java │ │ ├── Image.java │ │ ├── Order.java │ │ ├── OrderItem.java │ │ ├── Product.java │ │ └── User.java │ │ ├── repository │ │ ├── CartItemRepository.java │ │ ├── CartRepository.java │ │ ├── CategoryRepository.java │ │ ├── ImageRepository.java │ │ ├── OrderRepository.java │ │ ├── ProductRepository.java │ │ └── UserRepository.java │ │ ├── request │ │ ├── AddProductRequest.java │ │ ├── CreateUserRequest.java │ │ ├── ProductUpdateRequest.java │ │ └── UserUpdateRequest.java │ │ ├── response │ │ └── ApiResponse.java │ │ └── service │ │ ├── cart │ │ ├── CartItemService.java │ │ ├── CartService.java │ │ ├── ICartItemService.java │ │ └── ICartService.java │ │ ├── category │ │ ├── CategoryService.java │ │ └── ICategoryService.java │ │ ├── image │ │ ├── IImageService.java │ │ └── ImageService.java │ │ ├── order │ │ ├── IOrderService.java │ │ └── OrderService.java │ │ ├── product │ │ ├── IProductService.java │ │ └── ProductService.java │ │ └── user │ │ ├── IUserService.java │ │ └── UserService.java └── resources │ └── application.properties └── test └── java └── com └── dailycodework └── dreamshops └── DreamShopsApplicationTests.java /.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.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | wrapperVersion=3.3.2 18 | distributionType=only-script 19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.7/apache-maven-3.9.7-bin.zip 20 | -------------------------------------------------------------------------------- /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 | # Apache Maven Wrapper startup batch script, version 3.3.2 23 | # 24 | # Optional ENV vars 25 | # ----------------- 26 | # JAVA_HOME - location of a JDK home dir, required when download maven via java source 27 | # MVNW_REPOURL - repo url base for downloading maven distribution 28 | # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 29 | # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output 30 | # ---------------------------------------------------------------------------- 31 | 32 | set -euf 33 | [ "${MVNW_VERBOSE-}" != debug ] || set -x 34 | 35 | # OS specific support. 36 | native_path() { printf %s\\n "$1"; } 37 | case "$(uname)" in 38 | CYGWIN* | MINGW*) 39 | [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" 40 | native_path() { cygpath --path --windows "$1"; } 41 | ;; 42 | esac 43 | 44 | # set JAVACMD and JAVACCMD 45 | set_java_home() { 46 | # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched 47 | if [ -n "${JAVA_HOME-}" ]; then 48 | if [ -x "$JAVA_HOME/jre/sh/java" ]; then 49 | # IBM's JDK on AIX uses strange locations for the executables 50 | JAVACMD="$JAVA_HOME/jre/sh/java" 51 | JAVACCMD="$JAVA_HOME/jre/sh/javac" 52 | else 53 | JAVACMD="$JAVA_HOME/bin/java" 54 | JAVACCMD="$JAVA_HOME/bin/javac" 55 | 56 | if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then 57 | echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 58 | echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 59 | return 1 60 | fi 61 | fi 62 | else 63 | JAVACMD="$( 64 | 'set' +e 65 | 'unset' -f command 2>/dev/null 66 | 'command' -v java 67 | )" || : 68 | JAVACCMD="$( 69 | 'set' +e 70 | 'unset' -f command 2>/dev/null 71 | 'command' -v javac 72 | )" || : 73 | 74 | if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then 75 | echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 76 | return 1 77 | fi 78 | fi 79 | } 80 | 81 | # hash string like Java String::hashCode 82 | hash_string() { 83 | str="${1:-}" h=0 84 | while [ -n "$str" ]; do 85 | char="${str%"${str#?}"}" 86 | h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) 87 | str="${str#?}" 88 | done 89 | printf %x\\n $h 90 | } 91 | 92 | verbose() { :; } 93 | [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } 94 | 95 | die() { 96 | printf %s\\n "$1" >&2 97 | exit 1 98 | } 99 | 100 | trim() { 101 | # MWRAPPER-139: 102 | # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. 103 | # Needed for removing poorly interpreted newline sequences when running in more 104 | # exotic environments such as mingw bash on Windows. 105 | printf "%s" "${1}" | tr -d '[:space:]' 106 | } 107 | 108 | # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties 109 | while IFS="=" read -r key value; do 110 | case "${key-}" in 111 | distributionUrl) distributionUrl=$(trim "${value-}") ;; 112 | distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; 113 | esac 114 | done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" 115 | [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" 116 | 117 | case "${distributionUrl##*/}" in 118 | maven-mvnd-*bin.*) 119 | MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ 120 | case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in 121 | *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; 122 | :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; 123 | :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; 124 | :Linux*x86_64*) distributionPlatform=linux-amd64 ;; 125 | *) 126 | echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 127 | distributionPlatform=linux-amd64 128 | ;; 129 | esac 130 | distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" 131 | ;; 132 | maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; 133 | *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; 134 | esac 135 | 136 | # apply MVNW_REPOURL and calculate MAVEN_HOME 137 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 138 | [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" 139 | distributionUrlName="${distributionUrl##*/}" 140 | distributionUrlNameMain="${distributionUrlName%.*}" 141 | distributionUrlNameMain="${distributionUrlNameMain%-bin}" 142 | MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" 143 | MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" 144 | 145 | exec_maven() { 146 | unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : 147 | exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" 148 | } 149 | 150 | if [ -d "$MAVEN_HOME" ]; then 151 | verbose "found existing MAVEN_HOME at $MAVEN_HOME" 152 | exec_maven "$@" 153 | fi 154 | 155 | case "${distributionUrl-}" in 156 | *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; 157 | *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; 158 | esac 159 | 160 | # prepare tmp dir 161 | if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then 162 | clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } 163 | trap clean HUP INT TERM EXIT 164 | else 165 | die "cannot create temp dir" 166 | fi 167 | 168 | mkdir -p -- "${MAVEN_HOME%/*}" 169 | 170 | # Download and Install Apache Maven 171 | verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 172 | verbose "Downloading from: $distributionUrl" 173 | verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 174 | 175 | # select .zip or .tar.gz 176 | if ! command -v unzip >/dev/null; then 177 | distributionUrl="${distributionUrl%.zip}.tar.gz" 178 | distributionUrlName="${distributionUrl##*/}" 179 | fi 180 | 181 | # verbose opt 182 | __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' 183 | [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v 184 | 185 | # normalize http auth 186 | case "${MVNW_PASSWORD:+has-password}" in 187 | '') MVNW_USERNAME='' MVNW_PASSWORD='' ;; 188 | has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; 189 | esac 190 | 191 | if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then 192 | verbose "Found wget ... using wget" 193 | wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" 194 | elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then 195 | verbose "Found curl ... using curl" 196 | curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" 197 | elif set_java_home; then 198 | verbose "Falling back to use Java to download" 199 | javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" 200 | targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" 201 | cat >"$javaSource" <<-END 202 | public class Downloader extends java.net.Authenticator 203 | { 204 | protected java.net.PasswordAuthentication getPasswordAuthentication() 205 | { 206 | return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); 207 | } 208 | public static void main( String[] args ) throws Exception 209 | { 210 | setDefault( new Downloader() ); 211 | java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); 212 | } 213 | } 214 | END 215 | # For Cygwin/MinGW, switch paths to Windows format before running javac and java 216 | verbose " - Compiling Downloader.java ..." 217 | "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" 218 | verbose " - Running Downloader.java ..." 219 | "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" 220 | fi 221 | 222 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 223 | if [ -n "${distributionSha256Sum-}" ]; then 224 | distributionSha256Result=false 225 | if [ "$MVN_CMD" = mvnd.sh ]; then 226 | echo "Checksum validation is not supported for maven-mvnd." >&2 227 | echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 228 | exit 1 229 | elif command -v sha256sum >/dev/null; then 230 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then 231 | distributionSha256Result=true 232 | fi 233 | elif command -v shasum >/dev/null; then 234 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then 235 | distributionSha256Result=true 236 | fi 237 | else 238 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 239 | echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 240 | exit 1 241 | fi 242 | if [ $distributionSha256Result = false ]; then 243 | echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 244 | echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 245 | exit 1 246 | fi 247 | fi 248 | 249 | # unzip and move 250 | if command -v unzip >/dev/null; then 251 | unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" 252 | else 253 | tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" 254 | fi 255 | printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" 256 | mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" 257 | 258 | clean || : 259 | exec_maven "$@" 260 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | <# : batch portion 2 | @REM ---------------------------------------------------------------------------- 3 | @REM Licensed to the Apache Software Foundation (ASF) under one 4 | @REM or more contributor license agreements. See the NOTICE file 5 | @REM distributed with this work for additional information 6 | @REM regarding copyright ownership. The ASF licenses this file 7 | @REM to you under the Apache License, Version 2.0 (the 8 | @REM "License"); you may not use this file except in compliance 9 | @REM with the License. You may obtain a copy of the License at 10 | @REM 11 | @REM https://www.apache.org/licenses/LICENSE-2.0 12 | @REM 13 | @REM Unless required by applicable law or agreed to in writing, 14 | @REM software distributed under the License is distributed on an 15 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | @REM KIND, either express or implied. See the License for the 17 | @REM specific language governing permissions and limitations 18 | @REM under the License. 19 | @REM ---------------------------------------------------------------------------- 20 | 21 | @REM ---------------------------------------------------------------------------- 22 | @REM Apache Maven Wrapper startup batch script, version 3.3.2 23 | @REM 24 | @REM Optional ENV vars 25 | @REM MVNW_REPOURL - repo url base for downloading maven distribution 26 | @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 27 | @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output 28 | @REM ---------------------------------------------------------------------------- 29 | 30 | @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) 31 | @SET __MVNW_CMD__= 32 | @SET __MVNW_ERROR__= 33 | @SET __MVNW_PSMODULEP_SAVE=%PSModulePath% 34 | @SET PSModulePath= 35 | @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( 36 | IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) 37 | ) 38 | @SET PSModulePath=%__MVNW_PSMODULEP_SAVE% 39 | @SET __MVNW_PSMODULEP_SAVE= 40 | @SET __MVNW_ARG0_NAME__= 41 | @SET MVNW_USERNAME= 42 | @SET MVNW_PASSWORD= 43 | @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) 44 | @echo Cannot start maven from wrapper >&2 && exit /b 1 45 | @GOTO :EOF 46 | : end batch / begin powershell #> 47 | 48 | $ErrorActionPreference = "Stop" 49 | if ($env:MVNW_VERBOSE -eq "true") { 50 | $VerbosePreference = "Continue" 51 | } 52 | 53 | # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties 54 | $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl 55 | if (!$distributionUrl) { 56 | Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" 57 | } 58 | 59 | switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { 60 | "maven-mvnd-*" { 61 | $USE_MVND = $true 62 | $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" 63 | $MVN_CMD = "mvnd.cmd" 64 | break 65 | } 66 | default { 67 | $USE_MVND = $false 68 | $MVN_CMD = $script -replace '^mvnw','mvn' 69 | break 70 | } 71 | } 72 | 73 | # apply MVNW_REPOURL and calculate MAVEN_HOME 74 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 75 | if ($env:MVNW_REPOURL) { 76 | $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } 77 | $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" 78 | } 79 | $distributionUrlName = $distributionUrl -replace '^.*/','' 80 | $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' 81 | $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" 82 | if ($env:MAVEN_USER_HOME) { 83 | $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" 84 | } 85 | $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' 86 | $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" 87 | 88 | if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { 89 | Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" 90 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 91 | exit $? 92 | } 93 | 94 | if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { 95 | Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" 96 | } 97 | 98 | # prepare tmp dir 99 | $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile 100 | $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" 101 | $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null 102 | trap { 103 | if ($TMP_DOWNLOAD_DIR.Exists) { 104 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 105 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 106 | } 107 | } 108 | 109 | New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null 110 | 111 | # Download and Install Apache Maven 112 | Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 113 | Write-Verbose "Downloading from: $distributionUrl" 114 | Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 115 | 116 | $webclient = New-Object System.Net.WebClient 117 | if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { 118 | $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) 119 | } 120 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 121 | $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null 122 | 123 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 124 | $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum 125 | if ($distributionSha256Sum) { 126 | if ($USE_MVND) { 127 | Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." 128 | } 129 | Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash 130 | if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { 131 | Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." 132 | } 133 | } 134 | 135 | # unzip and move 136 | Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null 137 | Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null 138 | try { 139 | Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null 140 | } catch { 141 | if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { 142 | Write-Error "fail to move MAVEN_HOME" 143 | } 144 | } finally { 145 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 146 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 147 | } 148 | 149 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 150 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.3.2 9 | 10 | 11 | com.dailycodework 12 | dream-shops 13 | 0.0.1-SNAPSHOT 14 | dream-shops 15 | A simple eCommerce demo app 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 17 31 | 2.4.4 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-data-jpa 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-validation 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-web 45 | 46 | 47 | 48 | org.modelmapper.extensions 49 | modelmapper-spring 50 | ${modelmapper-spring.version} 51 | 52 | 53 | 54 | com.mysql 55 | mysql-connector-j 56 | runtime 57 | 58 | 59 | org.projectlombok 60 | lombok 61 | true 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-test 66 | test 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.springframework.boot 74 | spring-boot-maven-plugin 75 | 76 | 77 | 78 | org.projectlombok 79 | lombok 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/DreamShopsApplication.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DreamShopsApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DreamShopsApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/config/ShopConfig.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.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 ShopConfig { 9 | 10 | @Bean 11 | public ModelMapper modelMapper() { 12 | return new ModelMapper(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/controller/CartController.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.controller; 2 | 3 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 4 | import com.dailycodework.dreamshops.model.Cart; 5 | import com.dailycodework.dreamshops.response.ApiResponse; 6 | import com.dailycodework.dreamshops.service.cart.CartService; 7 | import com.dailycodework.dreamshops.service.cart.ICartService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | import java.math.BigDecimal; 13 | 14 | import static org.springframework.http.HttpStatus.NOT_FOUND; 15 | 16 | @RequiredArgsConstructor 17 | @RestController 18 | @RequestMapping("${api.prefix}/carts") 19 | public class CartController { 20 | private final ICartService cartService; 21 | 22 | @GetMapping("/{cartId}/my-cart") 23 | public ResponseEntity getCart( @PathVariable Long cartId) { 24 | try { 25 | Cart cart = cartService.getCart(cartId); 26 | return ResponseEntity.ok(new ApiResponse("Success", cart)); 27 | } catch (ResourceNotFoundException e) { 28 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 29 | } 30 | } 31 | 32 | @DeleteMapping("/{cartId}/clear") 33 | public ResponseEntity clearCart( @PathVariable Long cartId) { 34 | try { 35 | cartService.clearCart(cartId); 36 | return ResponseEntity.ok(new ApiResponse("Clear Cart Success!", null)); 37 | } catch (ResourceNotFoundException e) { 38 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 39 | } 40 | } 41 | 42 | @GetMapping("/{cartId}/cart/total-price") 43 | public ResponseEntity getTotalAmount( @PathVariable Long cartId) { 44 | try { 45 | BigDecimal totalPrice = cartService.getTotalPrice(cartId); 46 | return ResponseEntity.ok(new ApiResponse("Total Price", totalPrice)); 47 | } catch (ResourceNotFoundException e) { 48 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/controller/CartItemController.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.controller; 2 | 3 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 4 | import com.dailycodework.dreamshops.response.ApiResponse; 5 | import com.dailycodework.dreamshops.service.cart.CartItemService; 6 | import com.dailycodework.dreamshops.service.cart.ICartItemService; 7 | import com.dailycodework.dreamshops.service.cart.ICartService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | import static org.springframework.http.HttpStatus.NOT_FOUND; 13 | 14 | @RequiredArgsConstructor 15 | @RestController 16 | @RequestMapping("${api.prefix}/cartItems") 17 | public class CartItemController { 18 | private final ICartItemService cartItemService; 19 | private final ICartService cartService; 20 | 21 | 22 | @PostMapping("/item/add") 23 | public ResponseEntity addItemToCart(@RequestParam(required = false) Long cartId, 24 | @RequestParam Long productId, 25 | @RequestParam Integer quantity) { 26 | try { 27 | if (cartId == null) { 28 | cartId= cartService.initializeNewCart(); 29 | } 30 | cartItemService.addItemToCart(cartId, productId, quantity); 31 | return ResponseEntity.ok(new ApiResponse("Add Item Success", null)); 32 | } catch (ResourceNotFoundException e) { 33 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 34 | } 35 | } 36 | 37 | @DeleteMapping("/cart/{cartId}/item/{itemId}/remove") 38 | public ResponseEntity removeItemFromCart(@PathVariable Long cartId, @PathVariable Long itemId) { 39 | try { 40 | cartItemService.removeItemFromCart(cartId, itemId); 41 | return ResponseEntity.ok(new ApiResponse("Remove Item Success", null)); 42 | } catch (ResourceNotFoundException e) { 43 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 44 | } 45 | } 46 | 47 | @PutMapping("/cart/{cartId}/item/{itemId}/update") 48 | public ResponseEntity updateItemQuantity(@PathVariable Long cartId, 49 | @PathVariable Long itemId, 50 | @RequestParam Integer quantity) { 51 | try { 52 | cartItemService.updateItemQuantity(cartId, itemId, quantity); 53 | return ResponseEntity.ok(new ApiResponse("Update Item Success", null)); 54 | } catch (ResourceNotFoundException e) { 55 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 56 | } 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/controller/CategoryController.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.controller; 2 | 3 | 4 | import com.dailycodework.dreamshops.exceptions.AlreadyExistsException; 5 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 6 | import com.dailycodework.dreamshops.model.Category; 7 | import com.dailycodework.dreamshops.response.ApiResponse; 8 | import com.dailycodework.dreamshops.service.category.ICategoryService; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import java.util.List; 14 | 15 | import static org.springframework.http.HttpStatus.*; 16 | 17 | @RequiredArgsConstructor 18 | @RestController 19 | @RequestMapping("${api.prefix}/categories") 20 | public class CategoryController { 21 | private final ICategoryService categoryService; 22 | 23 | @GetMapping("/all") 24 | public ResponseEntity getAllCategories() { 25 | try { 26 | List categories = categoryService.getAllCategories(); 27 | return ResponseEntity.ok(new ApiResponse("Found!", categories)); 28 | } catch (Exception e) { 29 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse("Error:", INTERNAL_SERVER_ERROR)); 30 | } 31 | } 32 | 33 | @PostMapping("/add") 34 | public ResponseEntity addCategory(@RequestBody Category name) { 35 | try { 36 | Category theCategory = categoryService.addCategory(name); 37 | return ResponseEntity.ok(new ApiResponse("Success", theCategory)); 38 | } catch (AlreadyExistsException e) { 39 | return ResponseEntity.status(CONFLICT).body(new ApiResponse(e.getMessage(), null)); 40 | } 41 | } 42 | 43 | @GetMapping("/category/{id}/category") 44 | public ResponseEntity getCategoryById(@PathVariable Long id){ 45 | try { 46 | Category theCategory = categoryService.getCategoryById(id); 47 | return ResponseEntity.ok(new ApiResponse("Found", theCategory)); 48 | } catch (ResourceNotFoundException e) { 49 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 50 | } 51 | } 52 | 53 | @GetMapping("/category/{name}/category") 54 | public ResponseEntity getCategoryByName(@PathVariable String name){ 55 | try { 56 | Category theCategory = categoryService.getCategoryByName(name); 57 | return ResponseEntity.ok(new ApiResponse("Found", theCategory)); 58 | } catch (ResourceNotFoundException e) { 59 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 60 | } 61 | } 62 | 63 | 64 | @DeleteMapping("/category/{id}/delete") 65 | public ResponseEntity deleteCategory(@PathVariable Long id){ 66 | try { 67 | categoryService.deleteCategoryById(id); 68 | return ResponseEntity.ok(new ApiResponse("Found", null)); 69 | } catch (ResourceNotFoundException e) { 70 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 71 | } 72 | } 73 | 74 | @PutMapping("/category/{id}/update") 75 | public ResponseEntity updateCategory(@PathVariable Long id, @RequestBody Category category) { 76 | try { 77 | Category updatedCategory = categoryService.updateCategory(category, id); 78 | return ResponseEntity.ok(new ApiResponse("Update success!", updatedCategory)); 79 | } catch (ResourceNotFoundException e) { 80 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 81 | } 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/controller/ImageController.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.controller; 2 | 3 | import com.dailycodework.dreamshops.dto.ImageDto; 4 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 5 | import com.dailycodework.dreamshops.model.Image; 6 | import com.dailycodework.dreamshops.model.Product; 7 | import com.dailycodework.dreamshops.response.ApiResponse; 8 | import com.dailycodework.dreamshops.service.image.IImageService; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.core.io.ByteArrayResource; 11 | import org.springframework.core.io.Resource; 12 | import org.springframework.http.HttpHeaders; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.bind.annotation.*; 17 | import org.springframework.web.multipart.MultipartFile; 18 | 19 | import java.sql.SQLException; 20 | import java.util.List; 21 | 22 | import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; 23 | import static org.springframework.http.HttpStatus.NOT_FOUND; 24 | 25 | @RequiredArgsConstructor 26 | @RestController 27 | @RequestMapping("${api.prefix}/images") 28 | public class ImageController { 29 | private final IImageService imageService; 30 | 31 | 32 | @PostMapping("/upload") 33 | public ResponseEntity saveImages(@RequestParam List files, @RequestParam Long productId) { 34 | try { 35 | List imageDtos = imageService.saveImages(productId, files); 36 | return ResponseEntity.ok(new ApiResponse("Upload success!", imageDtos)); 37 | } catch (Exception e) { 38 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse("Upload failed!", e.getMessage())); 39 | } 40 | 41 | } 42 | 43 | @GetMapping("/image/download/{imageId}") 44 | public ResponseEntity downloadImage(@PathVariable Long imageId) throws SQLException { 45 | Image image = imageService.getImageById(imageId); 46 | ByteArrayResource resource = new ByteArrayResource(image.getImage().getBytes(1, (int) image.getImage().length())); 47 | return ResponseEntity.ok().contentType(MediaType.parseMediaType(image.getFileType())) 48 | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" +image.getFileName() + "\"") 49 | .body(resource); 50 | } 51 | 52 | @PutMapping("/image/{imageId}/update") 53 | public ResponseEntity updateImage(@PathVariable Long imageId, @RequestBody MultipartFile file) { 54 | try { 55 | Image image = imageService.getImageById(imageId); 56 | if(image != null) { 57 | imageService.updateImage(file, imageId); 58 | return ResponseEntity.ok(new ApiResponse("Update success!", null)); 59 | } 60 | } catch (ResourceNotFoundException e) { 61 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 62 | } 63 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse("Update failed!", INTERNAL_SERVER_ERROR)); 64 | } 65 | 66 | 67 | @DeleteMapping("/image/{imageId}/delete") 68 | public ResponseEntity deleteImage(@PathVariable Long imageId) { 69 | try { 70 | Image image = imageService.getImageById(imageId); 71 | if(image != null) { 72 | imageService.deleteImageById( imageId); 73 | return ResponseEntity.ok(new ApiResponse("Delete success!", null)); 74 | } 75 | } catch (ResourceNotFoundException e) { 76 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 77 | } 78 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse("Delete failed!", INTERNAL_SERVER_ERROR)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.controller; 2 | 3 | import com.dailycodework.dreamshops.dto.OrderDto; 4 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 5 | import com.dailycodework.dreamshops.model.Order; 6 | import com.dailycodework.dreamshops.response.ApiResponse; 7 | import com.dailycodework.dreamshops.service.order.IOrderService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.HttpStatus; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import java.util.List; 14 | 15 | @RequiredArgsConstructor 16 | @RestController 17 | @RequestMapping("${api.prefix}/orders") 18 | public class OrderController { 19 | private final IOrderService orderService; 20 | 21 | @PostMapping("/order") 22 | public ResponseEntity createOrder(@RequestParam Long userId) { 23 | try { 24 | Order order = orderService.placeOrder(userId); 25 | return ResponseEntity.ok(new ApiResponse("Item Order Success!", order)); 26 | } catch (Exception e) { 27 | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse("Error Occured!", e.getMessage())); 28 | } 29 | } 30 | 31 | @GetMapping("/{orderId}/order") 32 | public ResponseEntity getOrderById(@PathVariable Long orderId) { 33 | try { 34 | OrderDto order = orderService.getOrder(orderId); 35 | return ResponseEntity.ok(new ApiResponse("Item Order Success!", order)); 36 | } catch (ResourceNotFoundException e) { 37 | return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ApiResponse("Oops!", e.getMessage())); 38 | } 39 | } 40 | 41 | @GetMapping("/{userId}/order") 42 | public ResponseEntity getUserOrders(@PathVariable Long userId) { 43 | try { 44 | List order = orderService.getUserOrders(userId); 45 | return ResponseEntity.ok(new ApiResponse("Item Order Success!", order)); 46 | } catch (ResourceNotFoundException e) { 47 | return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ApiResponse("Oops!", e.getMessage())); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/controller/ProductController.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.controller; 2 | 3 | 4 | import com.dailycodework.dreamshops.dto.ProductDto; 5 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 6 | import com.dailycodework.dreamshops.model.Product; 7 | import com.dailycodework.dreamshops.request.AddProductRequest; 8 | import com.dailycodework.dreamshops.request.ProductUpdateRequest; 9 | import com.dailycodework.dreamshops.response.ApiResponse; 10 | import com.dailycodework.dreamshops.service.product.IProductService; 11 | import lombok.RequiredArgsConstructor; 12 | import org.springframework.http.ResponseEntity; 13 | import org.springframework.web.bind.annotation.*; 14 | 15 | import java.util.List; 16 | 17 | import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; 18 | import static org.springframework.http.HttpStatus.NOT_FOUND; 19 | 20 | @RequiredArgsConstructor 21 | @RestController 22 | @RequestMapping("${api.prefix}/products") 23 | public class ProductController { 24 | private final IProductService productService; 25 | 26 | @GetMapping("/all") 27 | public ResponseEntity getAllProducts() { 28 | List products = productService.getAllProducts(); 29 | List convertedProducts = productService.getConvertedProducts(products); 30 | return ResponseEntity.ok(new ApiResponse("success", convertedProducts)); 31 | } 32 | 33 | @GetMapping("product/{productId}/product") 34 | public ResponseEntity getProductById(@PathVariable Long productId) { 35 | try { 36 | Product product = productService.getProductById(productId); 37 | ProductDto productDto = productService.convertToDto(product); 38 | return ResponseEntity.ok(new ApiResponse("success", productDto)); 39 | } catch (ResourceNotFoundException e) { 40 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 41 | } 42 | } 43 | 44 | @PostMapping("/add") 45 | public ResponseEntity addProduct(@RequestBody AddProductRequest product) { 46 | try { 47 | Product theProduct = productService.addProduct(product); 48 | ProductDto productDto = productService.convertToDto(theProduct); 49 | return ResponseEntity.ok(new ApiResponse("Add product success!", productDto)); 50 | } catch (Exception e) { 51 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse(e.getMessage(), null)); 52 | } 53 | } 54 | 55 | @PutMapping("/product/{productId}/update") 56 | public ResponseEntity updateProduct(@RequestBody ProductUpdateRequest request, @PathVariable Long productId) { 57 | try { 58 | Product theProduct = productService.updateProduct(request, productId); 59 | ProductDto productDto = productService.convertToDto(theProduct); 60 | return ResponseEntity.ok(new ApiResponse("Update product success!", productDto)); 61 | } catch (ResourceNotFoundException e) { 62 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 63 | } 64 | } 65 | 66 | @DeleteMapping("/product/{productId}/delete") 67 | public ResponseEntity deleteProduct(@PathVariable Long productId) { 68 | try { 69 | productService.deleteProductById(productId); 70 | return ResponseEntity.ok(new ApiResponse("Delete product success!", productId)); 71 | } catch (ResourceNotFoundException e) { 72 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 73 | } 74 | } 75 | 76 | @GetMapping("/products/by/brand-and-name") 77 | public ResponseEntity getProductByBrandAndName(@RequestParam String brandName, @RequestParam String productName) { 78 | try { 79 | List products = productService.getProductsByBrandAndName(brandName, productName); 80 | if (products.isEmpty()) { 81 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse("No products found ", null)); 82 | } 83 | List convertedProducts = productService.getConvertedProducts(products); 84 | return ResponseEntity.ok(new ApiResponse("success", convertedProducts)); 85 | } catch (Exception e) { 86 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse(e.getMessage(), null)); 87 | } 88 | } 89 | 90 | @GetMapping("/products/by/category-and-brand") 91 | public ResponseEntity getProductByCategoryAndBrand(@RequestParam String category, @RequestParam String brand){ 92 | try { 93 | List products = productService.getProductsByCategoryAndBrand(category, brand); 94 | if (products.isEmpty()) { 95 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse("No products found ", null)); 96 | } 97 | List convertedProducts = productService.getConvertedProducts(products); 98 | return ResponseEntity.ok(new ApiResponse("success", convertedProducts)); 99 | } catch (Exception e) { 100 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse("error", e.getMessage())); 101 | } 102 | } 103 | 104 | @GetMapping("/products/{name}/products") 105 | public ResponseEntity getProductByName(@PathVariable String name){ 106 | try { 107 | List products = productService.getProductsByName(name); 108 | if (products.isEmpty()) { 109 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse("No products found ", null)); 110 | } 111 | List convertedProducts = productService.getConvertedProducts(products); 112 | return ResponseEntity.ok(new ApiResponse("success", convertedProducts)); 113 | } catch (Exception e) { 114 | return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ApiResponse("error", e.getMessage())); 115 | } 116 | } 117 | 118 | @GetMapping("/product/by-brand") 119 | public ResponseEntity findProductByBrand(@RequestParam String brand) { 120 | try { 121 | List products = productService.getProductsByBrand(brand); 122 | if (products.isEmpty()) { 123 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse("No products found ", null)); 124 | } 125 | List convertedProducts = productService.getConvertedProducts(products); 126 | return ResponseEntity.ok(new ApiResponse("success", convertedProducts)); 127 | } catch (Exception e) { 128 | return ResponseEntity.ok(new ApiResponse(e.getMessage(), null)); 129 | } 130 | } 131 | 132 | @GetMapping("/product/{category}/all/products") 133 | public ResponseEntity findProductByCategory(@PathVariable String category) { 134 | try { 135 | List products = productService.getProductsByCategory(category); 136 | if (products.isEmpty()) { 137 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse("No products found ", null)); 138 | } 139 | List convertedProducts = productService.getConvertedProducts(products); 140 | return ResponseEntity.ok(new ApiResponse("success", convertedProducts)); 141 | } catch (Exception e) { 142 | return ResponseEntity.ok(new ApiResponse(e.getMessage(), null)); 143 | } 144 | } 145 | 146 | @GetMapping("/product/count/by-brand/and-name") 147 | public ResponseEntity countProductsByBrandAndName(@RequestParam String brand, @RequestParam String name) { 148 | try { 149 | var productCount = productService.countProductsByBrandAndName(brand, name); 150 | return ResponseEntity.ok(new ApiResponse("Product count!", productCount)); 151 | } catch (Exception e) { 152 | return ResponseEntity.ok(new ApiResponse(e.getMessage(), null)); 153 | } 154 | } 155 | 156 | 157 | } 158 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.controller; 2 | 3 | import com.dailycodework.dreamshops.dto.UserDto; 4 | import com.dailycodework.dreamshops.exceptions.AlreadyExistsException; 5 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 6 | import com.dailycodework.dreamshops.model.User; 7 | import com.dailycodework.dreamshops.request.CreateUserRequest; 8 | import com.dailycodework.dreamshops.request.UserUpdateRequest; 9 | import com.dailycodework.dreamshops.response.ApiResponse; 10 | import com.dailycodework.dreamshops.service.user.IUserService; 11 | import com.dailycodework.dreamshops.service.user.UserService; 12 | import lombok.RequiredArgsConstructor; 13 | import org.springframework.http.ResponseEntity; 14 | import org.springframework.web.bind.annotation.*; 15 | 16 | import static org.springframework.http.HttpStatus.CONFLICT; 17 | import static org.springframework.http.HttpStatus.NOT_FOUND; 18 | 19 | @RequiredArgsConstructor 20 | @RestController 21 | @RequestMapping("${api.prefix}/users") 22 | public class UserController { 23 | private final IUserService userService; 24 | 25 | @GetMapping("/{userId}/user") 26 | public ResponseEntity getUserById(@PathVariable Long userId) { 27 | try { 28 | User user = userService.getUserById(userId); 29 | UserDto userDto = userService.convertUserToDto(user); 30 | return ResponseEntity.ok(new ApiResponse("Success", userDto)); 31 | } catch (ResourceNotFoundException e) { 32 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 33 | } 34 | } 35 | 36 | @PostMapping("/add") 37 | public ResponseEntity createUser(@RequestBody CreateUserRequest request) { 38 | try { 39 | User user = userService.createUser(request); 40 | UserDto userDto = userService.convertUserToDto(user); 41 | return ResponseEntity.ok(new ApiResponse("Create User Success!", userDto)); 42 | } catch (AlreadyExistsException e) { 43 | return ResponseEntity.status(CONFLICT).body(new ApiResponse(e.getMessage(), null)); 44 | } 45 | } 46 | @PutMapping("/{userId}/update") 47 | public ResponseEntity updateUser(@RequestBody UserUpdateRequest request, @PathVariable Long userId) { 48 | try { 49 | User user = userService.updateUser(request, userId); 50 | UserDto userDto = userService.convertUserToDto(user); 51 | return ResponseEntity.ok(new ApiResponse("Update User Success!", userDto)); 52 | } catch (ResourceNotFoundException e) { 53 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 54 | } 55 | } 56 | @DeleteMapping("/{userId}/delete") 57 | public ResponseEntity deleteUser(@PathVariable Long userId) { 58 | try { 59 | userService.deleteUser(userId); 60 | return ResponseEntity.ok(new ApiResponse("Delete User Success!", null)); 61 | } catch (ResourceNotFoundException e) { 62 | return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null)); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/dto/CartDto.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.dto; 2 | 3 | import lombok.Data; 4 | 5 | import java.math.BigDecimal; 6 | import java.util.Set; 7 | 8 | @Data 9 | public class CartDto { 10 | private Long cartId; 11 | private Set items; 12 | private BigDecimal totalAmount; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/dto/CartItemDto.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.dto; 2 | 3 | import com.dailycodework.dreamshops.model.Product; 4 | 5 | import java.math.BigDecimal; 6 | 7 | public class CartItemDto { 8 | private Long itemId; 9 | private Integer quantity; 10 | private BigDecimal unitPrice; 11 | private ProductDto product; 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/dto/ImageDto.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class ImageDto { 7 | private Long id; 8 | private String fileName; 9 | private String downloadUrl; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/dto/OrderDto.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.dto; 2 | 3 | import lombok.Data; 4 | 5 | import java.math.BigDecimal; 6 | import java.time.LocalDateTime; 7 | import java.util.List; 8 | 9 | @Data 10 | public class OrderDto { 11 | private Long id; 12 | private Long userId; 13 | private LocalDateTime orderDate; 14 | private BigDecimal totalAmount; 15 | private String status; 16 | private List items; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/dto/OrderItemDto.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.dto; 2 | 3 | import lombok.Data; 4 | 5 | import java.math.BigDecimal; 6 | 7 | @Data 8 | public class OrderItemDto { 9 | private Long productId; 10 | private String productName; 11 | private int quantity; 12 | private BigDecimal price; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/dto/ProductDto.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.dto; 2 | 3 | import com.dailycodework.dreamshops.model.Category; 4 | import com.dailycodework.dreamshops.model.Image; 5 | import jakarta.persistence.CascadeType; 6 | import jakarta.persistence.JoinColumn; 7 | import jakarta.persistence.ManyToOne; 8 | import jakarta.persistence.OneToMany; 9 | import lombok.Data; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.List; 13 | 14 | @Data 15 | public class ProductDto { 16 | private Long id; 17 | private String name; 18 | private String brand; 19 | private BigDecimal price; 20 | private int inventory; 21 | private String description; 22 | private Category category; 23 | private List images; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/dto/UserDto.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.dto; 2 | 3 | import com.dailycodework.dreamshops.model.Cart; 4 | import com.dailycodework.dreamshops.model.Order; 5 | import lombok.Data; 6 | 7 | import java.util.List; 8 | 9 | @Data 10 | public class UserDto { 11 | private Long id; 12 | private String firstName; 13 | private String lastName; 14 | private String email; 15 | private List orders; 16 | private CartDto cart; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/enums/OrderStatus.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.enums; 2 | 3 | public enum OrderStatus { 4 | PENDING, 5 | PROCESSING, 6 | SHIPPED, 7 | DELIVERED, 8 | CANCELLED 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/exceptions/AlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.exceptions; 2 | 3 | public class AlreadyExistsException extends RuntimeException { 4 | public AlreadyExistsException(String message) { 5 | super(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/exceptions/ProductNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.exceptions; 2 | 3 | public class ProductNotFoundException extends RuntimeException { 4 | public ProductNotFoundException(String message) { 5 | super(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/exceptions/ResourceNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.exceptions; 2 | 3 | public class ResourceNotFoundException extends RuntimeException { 4 | public ResourceNotFoundException(String message) { 5 | super(message); 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/Cart.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | import java.math.BigDecimal; 10 | import java.util.HashSet; 11 | import java.util.Set; 12 | 13 | @Getter 14 | @Setter 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | @Entity 18 | public class Cart { 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private Long id; 22 | private BigDecimal totalAmount = BigDecimal.ZERO; 23 | 24 | @OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, orphanRemoval = true) 25 | private Set items = new HashSet<>(); 26 | 27 | @OneToOne 28 | @JoinColumn(name = "user_id") 29 | private User user; 30 | 31 | 32 | 33 | 34 | 35 | 36 | public void addItem(CartItem item) { 37 | this.items.add(item); 38 | item.setCart(this); 39 | updateTotalAmount(); 40 | } 41 | 42 | public void removeItem(CartItem item) { 43 | this.items.remove(item); 44 | item.setCart(null); 45 | updateTotalAmount(); 46 | } 47 | 48 | private void updateTotalAmount() { 49 | this.totalAmount = items.stream().map(item -> { 50 | BigDecimal unitPrice = item.getUnitPrice(); 51 | if (unitPrice == null) { 52 | return BigDecimal.ZERO; 53 | } 54 | return unitPrice.multiply(BigDecimal.valueOf(item.getQuantity())); 55 | }).reduce(BigDecimal.ZERO, BigDecimal::add); 56 | } 57 | 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/CartItem.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | import jakarta.persistence.*; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Getter; 7 | import lombok.NoArgsConstructor; 8 | import lombok.Setter; 9 | 10 | import java.math.BigDecimal; 11 | 12 | @Getter 13 | @Setter 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | @Entity 17 | public class CartItem { 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private Long id; 21 | private int quantity; 22 | private BigDecimal unitPrice; 23 | private BigDecimal totalPrice; 24 | 25 | @ManyToOne 26 | @JoinColumn(name = "product_id") 27 | private Product product; 28 | 29 | @JsonIgnore 30 | @ManyToOne(cascade = CascadeType.ALL) 31 | @JoinColumn(name = "cart_id") 32 | private Cart cart; 33 | 34 | public void setTotalPrice() { 35 | this.totalPrice = this.unitPrice.multiply(new BigDecimal(quantity)); 36 | 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/Category.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | import jakarta.persistence.*; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Getter; 7 | import lombok.NoArgsConstructor; 8 | import lombok.Setter; 9 | 10 | import java.util.List; 11 | 12 | @Getter 13 | @Setter 14 | @NoArgsConstructor 15 | @Entity 16 | public class Category { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | private String name; 21 | 22 | 23 | @JsonIgnore 24 | @OneToMany(mappedBy = "category") 25 | private List products; 26 | 27 | public Category(String name) { 28 | this.name = name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/Image.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | import java.sql.Blob; 10 | 11 | @Getter 12 | @Setter 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Entity 16 | public class Image { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | private String fileName; 21 | private String fileType; 22 | 23 | @Lob 24 | private Blob image; 25 | private String downloadUrl; 26 | 27 | @ManyToOne 28 | @JoinColumn(name = "product_id") 29 | private Product product; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/Order.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import com.dailycodework.dreamshops.enums.OrderStatus; 4 | import jakarta.persistence.*; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | import java.math.BigDecimal; 10 | import java.time.LocalDate; 11 | import java.time.LocalDateTime; 12 | import java.util.HashSet; 13 | import java.util.Set; 14 | 15 | @Getter 16 | @Setter 17 | @NoArgsConstructor 18 | @Entity 19 | @Table(name = "orders") 20 | public class Order { 21 | @Id 22 | @GeneratedValue(strategy = GenerationType.IDENTITY) 23 | private Long orderId; 24 | private LocalDate orderDate; 25 | private BigDecimal totalAmount; 26 | @Enumerated(EnumType.STRING) 27 | private OrderStatus orderStatus; 28 | @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) 29 | private Set orderItems = new HashSet<>(); 30 | 31 | @ManyToOne 32 | @JoinColumn(name = "user_id") 33 | private User user; 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/OrderItem.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | import java.math.BigDecimal; 9 | 10 | @Getter 11 | @Setter 12 | @NoArgsConstructor 13 | @Entity 14 | public class OrderItem { 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | private Long id; 18 | private int quantity; 19 | private BigDecimal price; 20 | 21 | @ManyToOne 22 | @JoinColumn(name = "order_id") 23 | private Order order; 24 | 25 | @ManyToOne 26 | @JoinColumn(name = "product_id") 27 | private Product product; 28 | 29 | public OrderItem(Order order, Product product, int quantity, BigDecimal price) { 30 | this.order = order; 31 | this.product = product; 32 | this.quantity = quantity; 33 | this.price = price; 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/Product.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | import java.math.BigDecimal; 10 | import java.util.List; 11 | 12 | @Getter 13 | @Setter 14 | @NoArgsConstructor 15 | @Entity 16 | public class Product { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | private String name; 21 | private String brand; 22 | private BigDecimal price; 23 | private int inventory; 24 | private String description; 25 | 26 | @ManyToOne(cascade = CascadeType.ALL) 27 | @JoinColumn(name = "category_id") 28 | private Category category; 29 | 30 | @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true) 31 | private List images; 32 | 33 | public Product(String name, String brand, BigDecimal price, int inventory, String description, Category category) { 34 | this.name = name; 35 | this.brand = brand; 36 | this.price = price; 37 | this.inventory = inventory; 38 | this.description = description; 39 | this.category = category; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/model/User.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.model; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | import org.hibernate.annotations.NaturalId; 9 | 10 | import java.util.List; 11 | 12 | @Getter 13 | @Setter 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | @Entity 17 | public class User { 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private Long id; 21 | private String firstName; 22 | private String lastName; 23 | @NaturalId 24 | private String email; 25 | private String password; 26 | 27 | @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) 28 | private Cart cart; 29 | 30 | @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) 31 | private List orders; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/repository/CartItemRepository.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.repository; 2 | 3 | import com.dailycodework.dreamshops.model.CartItem; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface CartItemRepository extends JpaRepository { 7 | void deleteAllByCartId(Long id); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/repository/CartRepository.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.repository; 2 | 3 | import com.dailycodework.dreamshops.model.Cart; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface CartRepository extends JpaRepository { 7 | Cart findByUserId(Long userId); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/repository/CategoryRepository.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.repository; 2 | 3 | import com.dailycodework.dreamshops.model.Category; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface CategoryRepository extends JpaRepository { 7 | Category findByName(String name); 8 | 9 | boolean existsByName(String name); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/repository/ImageRepository.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.repository; 2 | 3 | import com.dailycodework.dreamshops.model.Image; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | 8 | public interface ImageRepository extends JpaRepository { 9 | List findByProductId(Long id); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/repository/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.repository; 2 | 3 | import com.dailycodework.dreamshops.model.Order; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | 8 | public interface OrderRepository extends JpaRepository { 9 | List findByUserId(Long userId); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.repository; 2 | 3 | import com.dailycodework.dreamshops.model.Product; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | 8 | public interface ProductRepository extends JpaRepository { 9 | List findByCategoryName(String category); 10 | 11 | List findByBrand(String brand); 12 | 13 | List findByCategoryNameAndBrand(String category, String brand); 14 | 15 | List findByName(String name); 16 | 17 | List findByBrandAndName(String brand, String name); 18 | 19 | Long countByBrandAndName(String brand, String name); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.repository; 2 | 3 | import com.dailycodework.dreamshops.model.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface UserRepository extends JpaRepository { 7 | boolean existsByEmail(String email); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/request/AddProductRequest.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.request; 2 | 3 | import com.dailycodework.dreamshops.model.Category; 4 | import jakarta.persistence.CascadeType; 5 | import jakarta.persistence.JoinColumn; 6 | import jakarta.persistence.ManyToOne; 7 | import lombok.Data; 8 | 9 | import java.math.BigDecimal; 10 | 11 | @Data 12 | public class AddProductRequest { 13 | private Long id; 14 | private String name; 15 | private String brand; 16 | private BigDecimal price; 17 | private int inventory; 18 | private String description; 19 | private Category category; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/request/CreateUserRequest.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.request; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CreateUserRequest { 7 | private String firstName; 8 | private String lastName; 9 | private String email; 10 | private String password; 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/request/ProductUpdateRequest.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.request; 2 | 3 | import com.dailycodework.dreamshops.model.Category; 4 | import lombok.Data; 5 | 6 | import java.math.BigDecimal; 7 | 8 | @Data 9 | public class ProductUpdateRequest { 10 | private Long id; 11 | private String name; 12 | private String brand; 13 | private BigDecimal price; 14 | private int inventory; 15 | private String description; 16 | private Category category; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/request/UserUpdateRequest.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.request; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class UserUpdateRequest { 7 | private String firstName; 8 | private String lastName; 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/response/ApiResponse.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.response; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @AllArgsConstructor 7 | @Data 8 | public class ApiResponse { 9 | private String message; 10 | private Object data; 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/cart/CartItemService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.cart; 2 | 3 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 4 | import com.dailycodework.dreamshops.model.Cart; 5 | import com.dailycodework.dreamshops.model.CartItem; 6 | import com.dailycodework.dreamshops.model.Product; 7 | import com.dailycodework.dreamshops.repository.CartItemRepository; 8 | import com.dailycodework.dreamshops.repository.CartRepository; 9 | import com.dailycodework.dreamshops.service.product.IProductService; 10 | import lombok.RequiredArgsConstructor; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.math.BigDecimal; 14 | 15 | @Service 16 | @RequiredArgsConstructor 17 | public class CartItemService implements ICartItemService{ 18 | private final CartItemRepository cartItemRepository; 19 | private final CartRepository cartRepository; 20 | private final IProductService productService; 21 | private final ICartService cartService; 22 | 23 | @Override 24 | public void addItemToCart(Long cartId, Long productId, int quantity) { 25 | //1. Get the cart 26 | //2. Get the product 27 | //3. Check if the product already in the cart 28 | //4. If Yes, then increase the quantity with the requested quantity 29 | //5. If No, then initiate a new CartItem entry. 30 | Cart cart = cartService.getCart(cartId); 31 | Product product = productService.getProductById(productId); 32 | CartItem cartItem = cart.getItems() 33 | .stream() 34 | .filter(item -> item.getProduct().getId().equals(productId)) 35 | .findFirst().orElse(new CartItem()); 36 | if (cartItem.getId() == null) { 37 | cartItem.setCart(cart); 38 | cartItem.setProduct(product); 39 | cartItem.setQuantity(quantity); 40 | cartItem.setUnitPrice(product.getPrice()); 41 | } 42 | else { 43 | cartItem.setQuantity(cartItem.getQuantity() + quantity); 44 | } 45 | cartItem.setTotalPrice(); 46 | cart.addItem(cartItem); 47 | cartItemRepository.save(cartItem); 48 | cartRepository.save(cart); 49 | } 50 | 51 | @Override 52 | public void removeItemFromCart(Long cartId, Long productId) { 53 | Cart cart = cartService.getCart(cartId); 54 | CartItem itemToRemove = getCartItem(cartId, productId); 55 | cart.removeItem(itemToRemove); 56 | cartRepository.save(cart); 57 | } 58 | 59 | @Override 60 | public void updateItemQuantity(Long cartId, Long productId, int quantity) { 61 | Cart cart = cartService.getCart(cartId); 62 | cart.getItems() 63 | .stream() 64 | .filter(item -> item.getProduct().getId().equals(productId)) 65 | .findFirst() 66 | .ifPresent(item -> { 67 | item.setQuantity(quantity); 68 | item.setUnitPrice(item.getProduct().getPrice()); 69 | item.setTotalPrice(); 70 | }); 71 | BigDecimal totalAmount = cart.getItems() 72 | .stream().map(CartItem ::getTotalPrice) 73 | .reduce(BigDecimal.ZERO, BigDecimal::add); 74 | 75 | cart.setTotalAmount(totalAmount); 76 | cartRepository.save(cart); 77 | } 78 | 79 | @Override 80 | public CartItem getCartItem(Long cartId, Long productId) { 81 | Cart cart = cartService.getCart(cartId); 82 | return cart.getItems() 83 | .stream() 84 | .filter(item -> item.getProduct().getId().equals(productId)) 85 | .findFirst().orElseThrow(() -> new ResourceNotFoundException("Item not found")); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/cart/CartService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.cart; 2 | 3 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 4 | import com.dailycodework.dreamshops.model.Cart; 5 | import com.dailycodework.dreamshops.repository.CartItemRepository; 6 | import com.dailycodework.dreamshops.repository.CartRepository; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.concurrent.atomic.AtomicLong; 13 | 14 | @Service 15 | @RequiredArgsConstructor 16 | public class CartService implements ICartService{ 17 | private final CartRepository cartRepository; 18 | private final CartItemRepository cartItemRepository; 19 | private final AtomicLong cartIdGenerator = new AtomicLong(0); 20 | 21 | @Override 22 | public Cart getCart(Long id) { 23 | Cart cart = cartRepository.findById(id) 24 | .orElseThrow(() -> new ResourceNotFoundException("Cart not found")); 25 | BigDecimal totalAmount = cart.getTotalAmount(); 26 | cart.setTotalAmount(totalAmount); 27 | return cartRepository.save(cart); 28 | } 29 | 30 | 31 | @Transactional 32 | @Override 33 | public void clearCart(Long id) { 34 | Cart cart = getCart(id); 35 | cartItemRepository.deleteAllByCartId(id); 36 | cart.getItems().clear(); 37 | cartRepository.deleteById(id); 38 | 39 | } 40 | 41 | @Override 42 | public BigDecimal getTotalPrice(Long id) { 43 | Cart cart = getCart(id); 44 | return cart.getTotalAmount(); 45 | } 46 | 47 | @Override 48 | public Long initializeNewCart() { 49 | Cart newCart = new Cart(); 50 | Long newCartId = cartIdGenerator.incrementAndGet(); 51 | newCart.setId(newCartId); 52 | return cartRepository.save(newCart).getId(); 53 | 54 | } 55 | 56 | @Override 57 | public Cart getCartByUserId(Long userId) { 58 | return cartRepository.findByUserId(userId); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/cart/ICartItemService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.cart; 2 | 3 | import com.dailycodework.dreamshops.model.CartItem; 4 | 5 | public interface ICartItemService { 6 | void addItemToCart(Long cartId, Long productId, int quantity); 7 | void removeItemFromCart(Long cartId, Long productId); 8 | void updateItemQuantity(Long cartId, Long productId, int quantity); 9 | 10 | CartItem getCartItem(Long cartId, Long productId); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/cart/ICartService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.cart; 2 | 3 | import com.dailycodework.dreamshops.model.Cart; 4 | 5 | import java.math.BigDecimal; 6 | 7 | public interface ICartService { 8 | Cart getCart(Long id); 9 | void clearCart(Long id); 10 | BigDecimal getTotalPrice(Long id); 11 | 12 | Long initializeNewCart(); 13 | 14 | Cart getCartByUserId(Long userId); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/category/CategoryService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.category; 2 | 3 | import com.dailycodework.dreamshops.exceptions.AlreadyExistsException; 4 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 5 | import com.dailycodework.dreamshops.model.Category; 6 | import com.dailycodework.dreamshops.repository.CategoryRepository; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | import java.util.Optional; 12 | 13 | @Service 14 | @RequiredArgsConstructor 15 | public class CategoryService implements ICategoryService { 16 | private final CategoryRepository categoryRepository; 17 | 18 | @Override 19 | public Category getCategoryById(Long id) { 20 | return categoryRepository.findById(id) 21 | .orElseThrow(()-> new ResourceNotFoundException("Category not found!")); 22 | } 23 | 24 | @Override 25 | public Category getCategoryByName(String name) { 26 | return categoryRepository.findByName(name); 27 | } 28 | 29 | @Override 30 | public List getAllCategories() { 31 | return categoryRepository.findAll(); 32 | } 33 | 34 | @Override 35 | public Category addCategory(Category category) { 36 | return Optional.of(category).filter(c -> !categoryRepository.existsByName(c.getName())) 37 | .map(categoryRepository :: save) 38 | .orElseThrow(() -> new AlreadyExistsException(category.getName()+" already exists")); 39 | } 40 | 41 | @Override 42 | public Category updateCategory(Category category, Long id) { 43 | return Optional.ofNullable(getCategoryById(id)).map(oldCategory -> { 44 | oldCategory.setName(category.getName()); 45 | return categoryRepository.save(oldCategory); 46 | }) .orElseThrow(()-> new ResourceNotFoundException("Category not found!")); 47 | } 48 | 49 | 50 | @Override 51 | public void deleteCategoryById(Long id) { 52 | categoryRepository.findById(id) 53 | .ifPresentOrElse(categoryRepository::delete, () -> { 54 | throw new ResourceNotFoundException("Category not found!"); 55 | }); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/category/ICategoryService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.category; 2 | 3 | import com.dailycodework.dreamshops.model.Category; 4 | 5 | import java.util.List; 6 | 7 | public interface ICategoryService { 8 | Category getCategoryById(Long id); 9 | Category getCategoryByName(String name); 10 | List getAllCategories(); 11 | Category addCategory(Category category); 12 | Category updateCategory(Category category, Long id); 13 | void deleteCategoryById(Long id); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/image/IImageService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.image; 2 | 3 | import com.dailycodework.dreamshops.dto.ImageDto; 4 | import com.dailycodework.dreamshops.model.Image; 5 | import org.springframework.web.multipart.MultipartFile; 6 | 7 | import java.util.List; 8 | 9 | public interface IImageService { 10 | Image getImageById(Long id); 11 | void deleteImageById(Long id); 12 | List saveImages(Long productId, List files); 13 | void updateImage(MultipartFile file, Long imageId); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/image/ImageService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.image; 2 | 3 | import com.dailycodework.dreamshops.dto.ImageDto; 4 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 5 | import com.dailycodework.dreamshops.model.Image; 6 | import com.dailycodework.dreamshops.model.Product; 7 | import com.dailycodework.dreamshops.repository.ImageRepository; 8 | import com.dailycodework.dreamshops.service.product.IProductService; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.stereotype.Service; 11 | import org.springframework.web.multipart.MultipartFile; 12 | 13 | import javax.sql.rowset.serial.SerialBlob; 14 | import java.io.IOException; 15 | import java.sql.SQLException; 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | @Service 20 | @RequiredArgsConstructor 21 | public class ImageService implements IImageService { 22 | private final ImageRepository imageRepository; 23 | private final IProductService productService; 24 | 25 | 26 | @Override 27 | public Image getImageById(Long id) { 28 | return imageRepository.findById(id) 29 | .orElseThrow(() -> new ResourceNotFoundException("No image found with id: " + id)); 30 | } 31 | 32 | @Override 33 | public void deleteImageById(Long id) { 34 | imageRepository.findById(id).ifPresentOrElse(imageRepository::delete, () -> { 35 | throw new ResourceNotFoundException("No image found with id: " + id); 36 | }); 37 | 38 | } 39 | 40 | @Override 41 | public List saveImages( Long productId, List files) { 42 | Product product = productService.getProductById(productId); 43 | 44 | List savedImageDto = new ArrayList<>(); 45 | for (MultipartFile file : files) { 46 | try { 47 | Image image = new Image(); 48 | image.setFileName(file.getOriginalFilename()); 49 | image.setFileType(file.getContentType()); 50 | image.setImage(new SerialBlob(file.getBytes())); 51 | image.setProduct(product); 52 | 53 | String buildDownloadUrl = "/api/v1/images/image/download/"; 54 | String downloadUrl = buildDownloadUrl+image.getId(); 55 | image.setDownloadUrl(downloadUrl); 56 | Image savedImage = imageRepository.save(image); 57 | 58 | savedImage.setDownloadUrl(buildDownloadUrl+savedImage.getId()); 59 | imageRepository.save(savedImage); 60 | 61 | ImageDto imageDto = new ImageDto(); 62 | imageDto.setId(savedImage.getId()); 63 | imageDto.setFileName(savedImage.getFileName()); 64 | imageDto.setDownloadUrl(savedImage.getDownloadUrl()); 65 | savedImageDto.add(imageDto); 66 | 67 | } catch(IOException | SQLException e){ 68 | throw new RuntimeException(e.getMessage()); 69 | } 70 | } 71 | return savedImageDto; 72 | } 73 | 74 | 75 | 76 | @Override 77 | public void updateImage(MultipartFile file, Long imageId) { 78 | Image image = getImageById(imageId); 79 | try { 80 | image.setFileName(file.getOriginalFilename()); 81 | image.setFileType(file.getContentType()); 82 | image.setImage(new SerialBlob(file.getBytes())); 83 | imageRepository.save(image); 84 | } catch (IOException | SQLException e) { 85 | throw new RuntimeException(e.getMessage()); 86 | } 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/order/IOrderService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.order; 2 | 3 | import com.dailycodework.dreamshops.dto.OrderDto; 4 | import com.dailycodework.dreamshops.model.Order; 5 | 6 | import java.util.List; 7 | 8 | public interface IOrderService { 9 | Order placeOrder(Long userId); 10 | OrderDto getOrder(Long orderId); 11 | List getUserOrders(Long userId); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/order/OrderService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.order; 2 | 3 | import com.dailycodework.dreamshops.dto.OrderDto; 4 | import com.dailycodework.dreamshops.enums.OrderStatus; 5 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 6 | import com.dailycodework.dreamshops.model.Cart; 7 | import com.dailycodework.dreamshops.model.Order; 8 | import com.dailycodework.dreamshops.model.OrderItem; 9 | import com.dailycodework.dreamshops.model.Product; 10 | import com.dailycodework.dreamshops.repository.OrderRepository; 11 | import com.dailycodework.dreamshops.repository.ProductRepository; 12 | import com.dailycodework.dreamshops.service.cart.CartService; 13 | import lombok.RequiredArgsConstructor; 14 | import org.modelmapper.ModelMapper; 15 | import org.springframework.stereotype.Service; 16 | import org.springframework.transaction.annotation.Transactional; 17 | 18 | import java.math.BigDecimal; 19 | import java.time.LocalDate; 20 | import java.util.HashSet; 21 | import java.util.List; 22 | 23 | @Service 24 | @RequiredArgsConstructor 25 | public class OrderService implements IOrderService { 26 | private final OrderRepository orderRepository; 27 | private final ProductRepository productRepository; 28 | private final CartService cartService; 29 | private final ModelMapper modelMapper; 30 | 31 | 32 | @Transactional 33 | @Override 34 | public Order placeOrder(Long userId) { 35 | Cart cart = cartService.getCartByUserId(userId); 36 | Order order = createOrder(cart); 37 | List orderItemList = createOrderItems(order, cart); 38 | order.setOrderItems(new HashSet<>(orderItemList)); 39 | order.setTotalAmount(calculateTotalAmount(orderItemList)); 40 | Order savedOrder = orderRepository.save(order); 41 | cartService.clearCart(cart.getId()); 42 | return savedOrder; 43 | } 44 | 45 | private Order createOrder(Cart cart) { 46 | Order order = new Order(); 47 | order.setUser(cart.getUser()); 48 | order.setOrderStatus(OrderStatus.PENDING); 49 | order.setOrderDate(LocalDate.now()); 50 | return order; 51 | } 52 | 53 | private List createOrderItems(Order order, Cart cart) { 54 | return cart.getItems().stream().map(cartItem -> { 55 | Product product = cartItem.getProduct(); 56 | product.setInventory(product.getInventory() - cartItem.getQuantity()); 57 | productRepository.save(product); 58 | return new OrderItem( 59 | order, 60 | product, 61 | cartItem.getQuantity(), 62 | cartItem.getUnitPrice()); 63 | }).toList(); 64 | 65 | } 66 | 67 | private BigDecimal calculateTotalAmount(List orderItemList) { 68 | return orderItemList 69 | .stream() 70 | .map(item -> item.getPrice() 71 | .multiply(new BigDecimal(item.getQuantity()))) 72 | .reduce(BigDecimal.ZERO, BigDecimal::add); 73 | } 74 | 75 | @Override 76 | public OrderDto getOrder(Long orderId) { 77 | return orderRepository.findById(orderId) 78 | .map(this :: convertToDto) 79 | .orElseThrow(() -> new ResourceNotFoundException("Order not found")); 80 | } 81 | 82 | @Override 83 | public List getUserOrders(Long userId) { 84 | List orders = orderRepository.findByUserId(userId); 85 | return orders.stream().map(this :: convertToDto).toList(); 86 | } 87 | 88 | private OrderDto convertToDto(Order order) { 89 | return modelMapper.map(order, OrderDto.class); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/product/IProductService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.product; 2 | import com.dailycodework.dreamshops.dto.ProductDto; 3 | import com.dailycodework.dreamshops.model.Product; 4 | import com.dailycodework.dreamshops.request.AddProductRequest; 5 | import com.dailycodework.dreamshops.request.ProductUpdateRequest; 6 | 7 | 8 | import java.util.List; 9 | 10 | public interface IProductService { 11 | Product addProduct(AddProductRequest product); 12 | Product getProductById(Long id); 13 | void deleteProductById(Long id); 14 | Product updateProduct(ProductUpdateRequest product, Long productId); 15 | List getAllProducts(); 16 | List getProductsByCategory(String category); 17 | List getProductsByBrand(String brand); 18 | List getProductsByCategoryAndBrand(String category, String brand); 19 | List getProductsByName(String name); 20 | List getProductsByBrandAndName(String category, String name); 21 | Long countProductsByBrandAndName(String brand, String name); 22 | 23 | List getConvertedProducts(List products); 24 | 25 | ProductDto convertToDto(Product product); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/product/ProductService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.product; 2 | 3 | import com.dailycodework.dreamshops.dto.ImageDto; 4 | import com.dailycodework.dreamshops.dto.ProductDto; 5 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 6 | import com.dailycodework.dreamshops.model.Category; 7 | import com.dailycodework.dreamshops.model.Image; 8 | import com.dailycodework.dreamshops.model.Product; 9 | import com.dailycodework.dreamshops.repository.CategoryRepository; 10 | import com.dailycodework.dreamshops.repository.ImageRepository; 11 | import com.dailycodework.dreamshops.repository.ProductRepository; 12 | import com.dailycodework.dreamshops.request.AddProductRequest; 13 | import com.dailycodework.dreamshops.request.ProductUpdateRequest; 14 | import lombok.RequiredArgsConstructor; 15 | import org.modelmapper.ModelMapper; 16 | import org.springframework.stereotype.Service; 17 | 18 | import java.util.List; 19 | import java.util.Optional; 20 | 21 | @Service 22 | @RequiredArgsConstructor 23 | public class ProductService implements IProductService { 24 | private final ProductRepository productRepository; 25 | private final CategoryRepository categoryRepository; 26 | private final ModelMapper modelMapper; 27 | private final ImageRepository imageRepository; 28 | 29 | @Override 30 | public Product addProduct(AddProductRequest request) { 31 | // check if the category is found in the DB 32 | // If Yes, set it as the new product category 33 | // If No, the save it as a new category 34 | // The set as the new product category. 35 | 36 | Category category = Optional.ofNullable(categoryRepository.findByName(request.getCategory().getName())) 37 | .orElseGet(() -> { 38 | Category newCategory = new Category(request.getCategory().getName()); 39 | return categoryRepository.save(newCategory); 40 | }); 41 | request.setCategory(category); 42 | return productRepository.save(createProduct(request, category)); 43 | } 44 | 45 | private Product createProduct(AddProductRequest request, Category category) { 46 | return new Product( 47 | request.getName(), 48 | request.getBrand(), 49 | request.getPrice(), 50 | request.getInventory(), 51 | request.getDescription(), 52 | category 53 | ); 54 | } 55 | 56 | 57 | @Override 58 | public Product getProductById(Long id) { 59 | return productRepository.findById(id) 60 | .orElseThrow(()-> new ResourceNotFoundException("Product not found!")); 61 | } 62 | 63 | @Override 64 | public void deleteProductById(Long id) { 65 | productRepository.findById(id) 66 | .ifPresentOrElse(productRepository::delete, 67 | () -> {throw new ResourceNotFoundException("Product not found!");}); 68 | } 69 | 70 | @Override 71 | public Product updateProduct(ProductUpdateRequest request, Long productId) { 72 | return productRepository.findById(productId) 73 | .map(existingProduct -> updateExistingProduct(existingProduct,request)) 74 | .map(productRepository :: save) 75 | .orElseThrow(()-> new ResourceNotFoundException("Product not found!")); 76 | } 77 | 78 | private Product updateExistingProduct(Product existingProduct, ProductUpdateRequest request) { 79 | existingProduct.setName(request.getName()); 80 | existingProduct.setBrand(request.getBrand()); 81 | existingProduct.setPrice(request.getPrice()); 82 | existingProduct.setInventory(request.getInventory()); 83 | existingProduct.setDescription(request.getDescription()); 84 | 85 | Category category = categoryRepository.findByName(request.getCategory().getName()); 86 | existingProduct.setCategory(category); 87 | return existingProduct; 88 | 89 | } 90 | 91 | @Override 92 | public List getAllProducts() { 93 | return productRepository.findAll(); 94 | } 95 | 96 | @Override 97 | public List getProductsByCategory(String category) { 98 | return productRepository.findByCategoryName(category); 99 | } 100 | 101 | @Override 102 | public List getProductsByBrand(String brand) { 103 | return productRepository.findByBrand(brand); 104 | } 105 | 106 | @Override 107 | public List getProductsByCategoryAndBrand(String category, String brand) { 108 | return productRepository.findByCategoryNameAndBrand(category, brand); 109 | } 110 | 111 | @Override 112 | public List getProductsByName(String name) { 113 | return productRepository.findByName(name); 114 | } 115 | 116 | @Override 117 | public List getProductsByBrandAndName(String brand, String name) { 118 | return productRepository.findByBrandAndName(brand, name); 119 | } 120 | 121 | @Override 122 | public Long countProductsByBrandAndName(String brand, String name) { 123 | return productRepository.countByBrandAndName(brand, name); 124 | } 125 | 126 | @Override 127 | public List getConvertedProducts(List products) { 128 | return products.stream().map(this::convertToDto).toList(); 129 | } 130 | 131 | @Override 132 | public ProductDto convertToDto(Product product) { 133 | ProductDto productDto = modelMapper.map(product, ProductDto.class); 134 | List images = imageRepository.findByProductId(product.getId()); 135 | List imageDtos = images.stream() 136 | .map(image -> modelMapper.map(image, ImageDto.class)) 137 | .toList(); 138 | productDto.setImages(imageDtos); 139 | return productDto; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/user/IUserService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.user; 2 | 3 | import com.dailycodework.dreamshops.dto.UserDto; 4 | import com.dailycodework.dreamshops.model.User; 5 | import com.dailycodework.dreamshops.request.CreateUserRequest; 6 | import com.dailycodework.dreamshops.request.UserUpdateRequest; 7 | 8 | public interface IUserService { 9 | 10 | User getUserById(Long userId); 11 | User createUser(CreateUserRequest request); 12 | User updateUser(UserUpdateRequest request, Long userId); 13 | void deleteUser(Long userId); 14 | 15 | UserDto convertUserToDto(User user); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/dailycodework/dreamshops/service/user/UserService.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops.service.user; 2 | 3 | import com.dailycodework.dreamshops.dto.UserDto; 4 | import com.dailycodework.dreamshops.exceptions.AlreadyExistsException; 5 | import com.dailycodework.dreamshops.exceptions.ResourceNotFoundException; 6 | import com.dailycodework.dreamshops.model.User; 7 | import com.dailycodework.dreamshops.repository.UserRepository; 8 | import com.dailycodework.dreamshops.request.CreateUserRequest; 9 | import com.dailycodework.dreamshops.request.UserUpdateRequest; 10 | import lombok.RequiredArgsConstructor; 11 | import org.modelmapper.ModelMapper; 12 | import org.springframework.stereotype.Service; 13 | 14 | import java.util.Optional; 15 | 16 | @Service 17 | @RequiredArgsConstructor 18 | public class UserService implements IUserService { 19 | private final UserRepository userRepository; 20 | private final ModelMapper modelMapper; 21 | 22 | @Override 23 | public User getUserById(Long userId) { 24 | return userRepository.findById(userId) 25 | .orElseThrow(() -> new ResourceNotFoundException("User not found!")); 26 | } 27 | 28 | @Override 29 | public User createUser(CreateUserRequest request) { 30 | return Optional.of(request) 31 | .filter(user -> !userRepository.existsByEmail(request.getEmail())) 32 | .map(req -> { 33 | User user = new User(); 34 | user.setEmail(request.getEmail()); 35 | user.setPassword(request.getPassword()); 36 | user.setFirstName(request.getFirstName()); 37 | user.setLastName(request.getLastName()); 38 | return userRepository.save(user); 39 | }) .orElseThrow(() -> new AlreadyExistsException("Oops!" +request.getEmail() +" already exists!")); 40 | } 41 | 42 | @Override 43 | public User updateUser(UserUpdateRequest request, Long userId) { 44 | return userRepository.findById(userId).map(existingUser ->{ 45 | existingUser.setFirstName(request.getFirstName()); 46 | existingUser.setLastName(request.getLastName()); 47 | return userRepository.save(existingUser); 48 | }).orElseThrow(() -> new ResourceNotFoundException("User not found!")); 49 | 50 | } 51 | 52 | @Override 53 | public void deleteUser(Long userId) { 54 | userRepository.findById(userId).ifPresentOrElse(userRepository :: delete, () ->{ 55 | throw new ResourceNotFoundException("User not found!"); 56 | }); 57 | } 58 | 59 | @Override 60 | public UserDto convertUserToDto(User user) { 61 | return modelMapper.map(user, UserDto.class); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=dream-shops 2 | 3 | server.port=9191 4 | 5 | spring.datasource.url=jdbc:mysql://localhost:3306/dream_shops_db 6 | spring.datasource.username=root 7 | spring.datasource.password=admin 8 | 9 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 10 | spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect 11 | 12 | spring.jpa.show-sql=true 13 | spring.jpa.properties.hibernate.format_sql=true 14 | ##(create, update, create-drop, validate) 15 | spring.jpa.hibernate.ddl-auto=update 16 | 17 | spring.servlet.multipart.max-file-size=5MB 18 | spring.servlet.multipart.max-request-size=5MB 19 | 20 | 21 | 22 | 23 | api.prefix=/api/v1 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/test/java/com/dailycodework/dreamshops/DreamShopsApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.dailycodework.dreamshops; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class DreamShopsApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------