diff --git a/.gitignore b/.gitignore index 6c0187813..e9cc75f0b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,9 @@ build/ !**/src/main/** !**/src/test/** +### macOS ### +.DS_Store + ### STS ### .apt_generated .classpath @@ -30,3 +33,8 @@ out/ ### VS Code ### .vscode/ + +### Docker ### +docker/db/mysql/data/ +docker/db/mysql/init/* +!docker/db/mysql/init/init.sql diff --git a/README.md b/README.md index ab00ba672..4a91e3f71 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,12 @@ # 키친포스 +## 퀵 스타트 + +```sh +cd docker +docker compose -p kitchenpos up -d +``` + ## 요구 사항 ### 상품 diff --git a/build.gradle b/build.gradle index 854271ed9..6d990e13c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,13 @@ plugins { - id 'org.springframework.boot' version '2.2.1.RELEASE' - id 'io.spring.dependency-management' version '1.0.8.RELEASE' + id 'org.springframework.boot' version '2.7.3' + id 'io.spring.dependency-management' version '1.0.13.RELEASE' + id 'org.flywaydb.flyway' version '8.2.0' id 'java' } group = 'camp.nextstep.edu' version = '0.0.1-SNAPSHOT' -sourceCompatibility = '1.8' +sourceCompatibility = '11' description = 'Kitchenpos project with domain-driven design' repositories { @@ -18,14 +19,20 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' - implementation 'org.flywaydb:flyway-core' - runtimeOnly 'com.h2database:h2' - testImplementation('org.springframework.boot:spring-boot-starter-test') { - exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' - } + implementation 'org.flywaydb:flyway-mysql' + runtimeOnly 'mysql:mysql-connector-java' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'com.h2database:h2' } test { useJUnitPlatform() } + +flyway { + url = 'jdbc:mysql://localhost:33306/kitchenpos' + user = 'user' + password = 'password' +} diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 000000000..f30a839e1 --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,18 @@ +version: "3.9" +services: + db: + image: mysql:8.0.30 + platform: linux/x86_64 + restart: always + ports: + - "33306:3306" + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: kitchenpos + MYSQL_USER: user + MYSQL_PASSWORD: password + TZ: Asia/Seoul + volumes: + - ./db/mysql/data:/var/lib/mysql + - ./db/mysql/config:/etc/mysql/conf.d + - ./db/mysql/init:/docker-entrypoint-initdb.d diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 5c2d1cf01..e708b1c02 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 28ff446a2..ec991f9aa 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 83f2acfdc..1b6c78733 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/bin/sh # -# Copyright 2015 the original author or authors. +# Copyright © 2015-2021 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,78 +17,113 @@ # ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` +APP_BASE_NAME=${0##*/} # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -97,7 +132,7 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" + JAVACMD=java which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the @@ -105,84 +140,95 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. # For Cygwin or MSYS, switch paths to Windows format before running java -if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=$((i+1)) + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=$(save "$@") +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 24467a141..ac1b06f93 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -29,6 +29,9 @@ if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @@ -37,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init +if "%ERRORLEVEL%" == "0" goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -51,7 +54,7 @@ goto fail set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe -if exist "%JAVA_EXE%" goto init +if exist "%JAVA_EXE%" goto execute echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% @@ -61,28 +64,14 @@ echo location of your Java installation. goto fail -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - :execute @rem Setup the command line set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* :end @rem End local scope for the variables with windows NT shell diff --git a/src/main/java/kitchenpos/eatinorders/application/OrderService.java b/src/main/java/kitchenpos/eatinorders/application/OrderService.java index 91ad8e025..c20f52c7f 100644 --- a/src/main/java/kitchenpos/eatinorders/application/OrderService.java +++ b/src/main/java/kitchenpos/eatinorders/application/OrderService.java @@ -86,7 +86,7 @@ public Order create(final Order request) { if (type == OrderType.EAT_IN) { final OrderTable orderTable = orderTableRepository.findById(request.getOrderTableId()) .orElseThrow(NoSuchElementException::new); - if (orderTable.isEmpty()) { + if (!orderTable.isOccupied()) { throw new IllegalStateException(); } order.setOrderTable(orderTable); @@ -171,7 +171,7 @@ public Order complete(final UUID orderId) { final OrderTable orderTable = order.getOrderTable(); if (!orderRepository.existsByOrderTableAndStatusNot(orderTable, OrderStatus.COMPLETED)) { orderTable.setNumberOfGuests(0); - orderTable.setEmpty(true); + orderTable.setOccupied(false); } } return order; diff --git a/src/main/java/kitchenpos/eatinorders/application/OrderTableService.java b/src/main/java/kitchenpos/eatinorders/application/OrderTableService.java index 0c76c89e6..1df7e345f 100644 --- a/src/main/java/kitchenpos/eatinorders/application/OrderTableService.java +++ b/src/main/java/kitchenpos/eatinorders/application/OrderTableService.java @@ -32,7 +32,7 @@ public OrderTable create(final OrderTable request) { orderTable.setId(UUID.randomUUID()); orderTable.setName(name); orderTable.setNumberOfGuests(0); - orderTable.setEmpty(true); + orderTable.setOccupied(false); return orderTableRepository.save(orderTable); } @@ -40,7 +40,7 @@ public OrderTable create(final OrderTable request) { public OrderTable sit(final UUID orderTableId) { final OrderTable orderTable = orderTableRepository.findById(orderTableId) .orElseThrow(NoSuchElementException::new); - orderTable.setEmpty(false); + orderTable.setOccupied(true); return orderTable; } @@ -52,7 +52,7 @@ public OrderTable clear(final UUID orderTableId) { throw new IllegalStateException(); } orderTable.setNumberOfGuests(0); - orderTable.setEmpty(true); + orderTable.setOccupied(false); return orderTable; } @@ -64,7 +64,7 @@ public OrderTable changeNumberOfGuests(final UUID orderTableId, final OrderTable } final OrderTable orderTable = orderTableRepository.findById(orderTableId) .orElseThrow(NoSuchElementException::new); - if (orderTable.isEmpty()) { + if (!orderTable.isOccupied()) { throw new IllegalStateException(); } orderTable.setNumberOfGuests(numberOfGuests); diff --git a/src/main/java/kitchenpos/eatinorders/domain/Order.java b/src/main/java/kitchenpos/eatinorders/domain/Order.java index 36ec39a46..c6e630c90 100644 --- a/src/main/java/kitchenpos/eatinorders/domain/Order.java +++ b/src/main/java/kitchenpos/eatinorders/domain/Order.java @@ -8,7 +8,7 @@ @Table(name = "orders") @Entity public class Order { - @Column(name = "id", columnDefinition = "varbinary(16)") + @Column(name = "id", columnDefinition = "binary(16)") @Id private UUID id; @@ -27,7 +27,7 @@ public class Order { @JoinColumn( name = "order_id", nullable = false, - columnDefinition = "varbinary(16)", + columnDefinition = "binary(16)", foreignKey = @ForeignKey(name = "fk_order_line_item_to_orders") ) private List orderLineItems; @@ -38,7 +38,7 @@ public class Order { @ManyToOne @JoinColumn( name = "order_table_id", - columnDefinition = "varbinary(16)", + columnDefinition = "binary(16)", foreignKey = @ForeignKey(name = "fk_orders_to_order_table") ) private OrderTable orderTable; diff --git a/src/main/java/kitchenpos/eatinorders/domain/OrderLineItem.java b/src/main/java/kitchenpos/eatinorders/domain/OrderLineItem.java index 990bfefa7..e1e037caf 100644 --- a/src/main/java/kitchenpos/eatinorders/domain/OrderLineItem.java +++ b/src/main/java/kitchenpos/eatinorders/domain/OrderLineItem.java @@ -17,7 +17,7 @@ public class OrderLineItem { @ManyToOne(optional = false) @JoinColumn( name = "menu_id", - columnDefinition = "varbinary(16)", + columnDefinition = "binary(16)", foreignKey = @ForeignKey(name = "fk_order_line_item_to_menu") ) private Menu menu; diff --git a/src/main/java/kitchenpos/eatinorders/domain/OrderTable.java b/src/main/java/kitchenpos/eatinorders/domain/OrderTable.java index 1cbe946e2..9e1dc5519 100644 --- a/src/main/java/kitchenpos/eatinorders/domain/OrderTable.java +++ b/src/main/java/kitchenpos/eatinorders/domain/OrderTable.java @@ -9,7 +9,7 @@ @Table(name = "order_table") @Entity public class OrderTable { - @Column(name = "id", columnDefinition = "varbinary(16)") + @Column(name = "id", columnDefinition = "binary(16)") @Id private UUID id; @@ -19,8 +19,8 @@ public class OrderTable { @Column(name = "number_of_guests", nullable = false) private int numberOfGuests; - @Column(name = "empty", nullable = false) - private boolean empty; + @Column(name = "occupied", nullable = false) + private boolean occupied; public OrderTable() { } @@ -49,11 +49,11 @@ public void setNumberOfGuests(final int numberOfGuests) { this.numberOfGuests = numberOfGuests; } - public boolean isEmpty() { - return empty; + public boolean isOccupied() { + return occupied; } - public void setEmpty(final boolean empty) { - this.empty = empty; + public void setOccupied(final boolean occupied) { + this.occupied = occupied; } } diff --git a/src/main/java/kitchenpos/menus/application/MenuService.java b/src/main/java/kitchenpos/menus/application/MenuService.java index ac2c938de..abf913c11 100644 --- a/src/main/java/kitchenpos/menus/application/MenuService.java +++ b/src/main/java/kitchenpos/menus/application/MenuService.java @@ -93,13 +93,16 @@ public Menu changePrice(final UUID menuId, final Menu request) { } final Menu menu = menuRepository.findById(menuId) .orElseThrow(NoSuchElementException::new); + BigDecimal sum = BigDecimal.ZERO; for (final MenuProduct menuProduct : menu.getMenuProducts()) { - final BigDecimal sum = menuProduct.getProduct() - .getPrice() - .multiply(BigDecimal.valueOf(menuProduct.getQuantity())); - if (price.compareTo(sum) > 0) { - throw new IllegalArgumentException(); - } + sum = sum.add( + menuProduct.getProduct() + .getPrice() + .multiply(BigDecimal.valueOf(menuProduct.getQuantity())) + ); + } + if (price.compareTo(sum) > 0) { + throw new IllegalArgumentException(); } menu.setPrice(price); return menu; @@ -109,13 +112,16 @@ public Menu changePrice(final UUID menuId, final Menu request) { public Menu display(final UUID menuId) { final Menu menu = menuRepository.findById(menuId) .orElseThrow(NoSuchElementException::new); + BigDecimal sum = BigDecimal.ZERO; for (final MenuProduct menuProduct : menu.getMenuProducts()) { - final BigDecimal sum = menuProduct.getProduct() - .getPrice() - .multiply(BigDecimal.valueOf(menuProduct.getQuantity())); - if (menu.getPrice().compareTo(sum) > 0) { - throw new IllegalStateException(); - } + sum = sum.add( + menuProduct.getProduct() + .getPrice() + .multiply(BigDecimal.valueOf(menuProduct.getQuantity())) + ); + } + if (menu.getPrice().compareTo(sum) > 0) { + throw new IllegalStateException(); } menu.setDisplayed(true); return menu; diff --git a/src/main/java/kitchenpos/menus/domain/JpaMenuRepository.java b/src/main/java/kitchenpos/menus/domain/JpaMenuRepository.java index 423df175a..796499c30 100644 --- a/src/main/java/kitchenpos/menus/domain/JpaMenuRepository.java +++ b/src/main/java/kitchenpos/menus/domain/JpaMenuRepository.java @@ -8,7 +8,7 @@ import java.util.UUID; public interface JpaMenuRepository extends MenuRepository, JpaRepository { - @Query("select m from Menu m, MenuProduct mp where mp.product.id = :productId") + @Query("select m from Menu m join m.menuProducts mp where mp.product.id = :productId") @Override List findAllByProductId(@Param("productId") UUID productId); } diff --git a/src/main/java/kitchenpos/menus/domain/Menu.java b/src/main/java/kitchenpos/menus/domain/Menu.java index 5e78ee1a2..e923a6e5d 100644 --- a/src/main/java/kitchenpos/menus/domain/Menu.java +++ b/src/main/java/kitchenpos/menus/domain/Menu.java @@ -8,7 +8,7 @@ @Table(name = "menu") @Entity public class Menu { - @Column(name = "id", columnDefinition = "varbinary(16)") + @Column(name = "id", columnDefinition = "binary(16)") @Id private UUID id; @@ -21,7 +21,7 @@ public class Menu { @ManyToOne(optional = false) @JoinColumn( name = "menu_group_id", - columnDefinition = "varbinary(16)", + columnDefinition = "binary(16)", foreignKey = @ForeignKey(name = "fk_menu_to_menu_group") ) private MenuGroup menuGroup; @@ -33,7 +33,7 @@ public class Menu { @JoinColumn( name = "menu_id", nullable = false, - columnDefinition = "varbinary(16)", + columnDefinition = "binary(16)", foreignKey = @ForeignKey(name = "fk_menu_product_to_menu") ) private List menuProducts; diff --git a/src/main/java/kitchenpos/menus/domain/MenuGroup.java b/src/main/java/kitchenpos/menus/domain/MenuGroup.java index 9509c23ff..e65017271 100644 --- a/src/main/java/kitchenpos/menus/domain/MenuGroup.java +++ b/src/main/java/kitchenpos/menus/domain/MenuGroup.java @@ -9,7 +9,7 @@ @Table(name = "menu_group") @Entity public class MenuGroup { - @Column(name = "id", columnDefinition = "varbinary(16)") + @Column(name = "id", columnDefinition = "binary(16)") @Id private UUID id; diff --git a/src/main/java/kitchenpos/menus/domain/MenuProduct.java b/src/main/java/kitchenpos/menus/domain/MenuProduct.java index 9829532cf..2836096e6 100644 --- a/src/main/java/kitchenpos/menus/domain/MenuProduct.java +++ b/src/main/java/kitchenpos/menus/domain/MenuProduct.java @@ -16,7 +16,7 @@ public class MenuProduct { @ManyToOne(optional = false) @JoinColumn( name = "product_id", - columnDefinition = "varbinary(16)", + columnDefinition = "binary(16)", foreignKey = @ForeignKey(name = "fk_menu_product_to_product") ) private Product product; diff --git a/src/main/java/kitchenpos/products/application/ProductService.java b/src/main/java/kitchenpos/products/application/ProductService.java index cb8d62fe3..20cf63996 100644 --- a/src/main/java/kitchenpos/products/application/ProductService.java +++ b/src/main/java/kitchenpos/products/application/ProductService.java @@ -61,9 +61,11 @@ public Product changePrice(final UUID productId, final Product request) { for (final Menu menu : menus) { BigDecimal sum = BigDecimal.ZERO; for (final MenuProduct menuProduct : menu.getMenuProducts()) { - sum = menuProduct.getProduct() - .getPrice() - .multiply(BigDecimal.valueOf(menuProduct.getQuantity())); + sum = sum.add( + menuProduct.getProduct() + .getPrice() + .multiply(BigDecimal.valueOf(menuProduct.getQuantity())) + ); } if (menu.getPrice().compareTo(sum) > 0) { menu.setDisplayed(false); diff --git a/src/main/java/kitchenpos/products/domain/Product.java b/src/main/java/kitchenpos/products/domain/Product.java index 34ff7f4d7..456864e20 100644 --- a/src/main/java/kitchenpos/products/domain/Product.java +++ b/src/main/java/kitchenpos/products/domain/Product.java @@ -10,7 +10,7 @@ @Table(name = "product") @Entity public class Product { - @Column(name = "id", columnDefinition = "varbinary(16)") + @Column(name = "id", columnDefinition = "binary(16)") @Id private UUID id; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e144271bf..2f6b66d6e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,9 +1,9 @@ -spring.datasource.url=jdbc:h2:~/test;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE -spring.datasource.username=sa +spring.datasource.password=password +spring.datasource.url=jdbc:mysql://localhost:33306/kitchenpos +spring.datasource.username=user spring.flyway.enabled=true -spring.h2.console.enabled=true spring.jpa.hibernate.ddl-auto=validate -spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.jpa.properties.hibernate.format_sql=true spring.jpa.show-sql=true logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE diff --git a/src/main/resources/db/migration/V1__Initialize_project_tables.sql b/src/main/resources/db/migration/V1__Initialize_project_tables.sql index b2b867dc6..feb5d03b0 100644 --- a/src/main/resources/db/migration/V1__Initialize_project_tables.sql +++ b/src/main/resources/db/migration/V1__Initialize_project_tables.sql @@ -1,61 +1,61 @@ create table menu ( - id varbinary(16) not null, + id binary(16) not null, displayed bit not null, name varchar(255) not null, price decimal(19, 2) not null, - menu_group_id varbinary(16) not null, + menu_group_id binary(16) not null, primary key (id) ) engine = InnoDB; create table menu_group ( - id varbinary(16) not null, - name varchar(255) not null, + id binary(16) not null, + name varchar(255) not null, primary key (id) ) engine = InnoDB; create table menu_product ( - seq bigint not null auto_increment, - quantity bigint not null, - product_id varbinary(16) not null, - menu_id varbinary(16) not null, + seq bigint not null auto_increment, + quantity bigint not null, + product_id binary(16) not null, + menu_id binary(16) not null, primary key (seq) ) engine = InnoDB; create table order_line_item ( - seq bigint not null auto_increment, - quantity bigint not null, - menu_id varbinary(16) not null, - order_id varbinary(16) not null, + seq bigint not null auto_increment, + quantity bigint not null, + menu_id binary(16) not null, + order_id binary(16) not null, primary key (seq) ) engine = InnoDB; create table order_table ( - id varbinary(16) not null, - empty bit not null, - name varchar(255) not null, - number_of_guests integer not null, + id binary(16) not null, + occupied bit not null, + name varchar(255) not null, + number_of_guests integer not null, primary key (id) ) engine = InnoDB; create table orders ( - id varbinary(16) not null, + id binary(16) not null, delivery_address varchar(255), - order_date_time datetime(6) not null, - status varchar(255) not null, - type varchar(255) not null, - order_table_id varbinary(16), + order_date_time datetime(6) not null, + status varchar(255) not null, + type varchar(255) not null, + order_table_id binary(16), primary key (id) ) engine = InnoDB; create table product ( - id varbinary(16) not null, + id binary(16) not null, name varchar(255) not null, price decimal(19, 2) not null, primary key (id) diff --git a/src/main/resources/db/migration/V2__Insert_default_data.sql b/src/main/resources/db/migration/V2__Insert_default_data.sql index 6c5e0eff9..59ca0714f 100644 --- a/src/main/resources/db/migration/V2__Insert_default_data.sql +++ b/src/main/resources/db/migration/V2__Insert_default_data.sql @@ -46,22 +46,22 @@ values (1, x'0ac16db71b024a87b9c1e7d8f226c48d', x'b9c670b04ef5409083496868df1c7d insert into menu_product (quantity, product_id, menu_id) values (1, x'7de4b8affa0f4391aaa9c61ea9b40f83', x'a64af6cac34d4cd882fe454abf512d1f'); -insert into order_table (id, empty, name, number_of_guests) -values (x'8d71004329b6420e8452233f5a035520', true, '1번', 0); -insert into order_table (id, empty, name, number_of_guests) -values (x'6ab59e8106eb441684e99faabc87c9ca', true, '2번', 0); -insert into order_table (id, empty, name, number_of_guests) -values (x'ae92335ccd264626b7979e4ae8c4efbd', true, '3번', 0); -insert into order_table (id, empty, name, number_of_guests) -values (x'a9858d4b80d0428881f48f41596a23fb', true, '4번', 0); -insert into order_table (id, empty, name, number_of_guests) -values (x'3faec3ab5217405daaa2804f87697f84', true, '5번', 0); -insert into order_table (id, empty, name, number_of_guests) -values (x'815b8395a2ad4e3589dc74c3b2191478', true, '6번', 0); -insert into order_table (id, empty, name, number_of_guests) -values (x'7ce8b3a235454542ab9cb3d493bbd4fb', true, '7번', 0); -insert into order_table (id, empty, name, number_of_guests) -values (x'7bdb1ffde36e4e2b94e3d2c14d391ef3', true, '8번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'8d71004329b6420e8452233f5a035520', false, '1번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'6ab59e8106eb441684e99faabc87c9ca', false, '2번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'ae92335ccd264626b7979e4ae8c4efbd', false, '3번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'a9858d4b80d0428881f48f41596a23fb', false, '4번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'3faec3ab5217405daaa2804f87697f84', false, '5번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'815b8395a2ad4e3589dc74c3b2191478', false, '6번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'7ce8b3a235454542ab9cb3d493bbd4fb', false, '7번', 0); +insert into order_table (id, occupied, name, number_of_guests) +values (x'7bdb1ffde36e4e2b94e3d2c14d391ef3', false, '8번', 0); insert into orders (id, delivery_address, order_date_time, status, type, order_table_id) values (x'69d78f383bff457cbb7226319c985fd8', '서울시 송파구 위례성대로 2', '2021-07-27', 'WAITING', 'DELIVERY', null); diff --git a/src/test/java/kitchenpos/Fixtures.java b/src/test/java/kitchenpos/Fixtures.java index aca62ec47..a17a54ab8 100644 --- a/src/test/java/kitchenpos/Fixtures.java +++ b/src/test/java/kitchenpos/Fixtures.java @@ -101,15 +101,15 @@ public static OrderLineItem orderLineItem() { } public static OrderTable orderTable() { - return orderTable(true, 0); + return orderTable(false, 0); } - public static OrderTable orderTable(final boolean empty, final int numberOfGuests) { + public static OrderTable orderTable(final boolean occupied, final int numberOfGuests) { final OrderTable orderTable = new OrderTable(); orderTable.setId(UUID.randomUUID()); orderTable.setName("1번"); orderTable.setNumberOfGuests(numberOfGuests); - orderTable.setEmpty(empty); + orderTable.setOccupied(occupied); return orderTable; } diff --git a/src/test/java/kitchenpos/eatinorders/application/OrderServiceTest.java b/src/test/java/kitchenpos/eatinorders/application/OrderServiceTest.java index 30b5087f8..96bf0f286 100644 --- a/src/test/java/kitchenpos/eatinorders/application/OrderServiceTest.java +++ b/src/test/java/kitchenpos/eatinorders/application/OrderServiceTest.java @@ -73,7 +73,7 @@ void createTakeoutOrder() { @Test void createEatInOrder() { final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId(); - final UUID orderTableId = orderTableRepository.save(orderTable(false, 4)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(true, 4)).getId(); final Order expected = createOrderRequest(OrderType.EAT_IN, orderTableId, createOrderLineItemRequest(menuId, 19_000L, 3L)); final Order actual = orderService.create(expected); assertThat(actual).isNotNull(); @@ -119,7 +119,7 @@ private static List orderLineItems() { @ParameterizedTest void createEatInOrder(final long quantity) { final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId(); - final UUID orderTableId = orderTableRepository.save(orderTable(false, 4)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(true, 4)).getId(); final Order expected = createOrderRequest( OrderType.EAT_IN, orderTableId, createOrderLineItemRequest(menuId, 19_000L, quantity) ); @@ -154,7 +154,7 @@ OrderType.DELIVERY, deliveryAddress, createOrderLineItemRequest(menuId, 19_000L, @Test void createEmptyTableEatInOrder() { final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId(); - final UUID orderTableId = orderTableRepository.save(orderTable(true, 0)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(false, 0)).getId(); final Order expected = createOrderRequest( OrderType.EAT_IN, orderTableId, createOrderLineItemRequest(menuId, 19_000L, 3L) ); @@ -183,7 +183,7 @@ void createNotMatchedMenuPriceOrder() { @DisplayName("주문을 접수한다.") @Test void accept() { - final UUID orderId = orderRepository.save(order(OrderStatus.WAITING, orderTable(false, 4))).getId(); + final UUID orderId = orderRepository.save(order(OrderStatus.WAITING, orderTable(true, 4))).getId(); final Order actual = orderService.accept(orderId); assertThat(actual.getStatus()).isEqualTo(OrderStatus.ACCEPTED); } @@ -192,7 +192,7 @@ void accept() { @EnumSource(value = OrderStatus.class, names = "WAITING", mode = EnumSource.Mode.EXCLUDE) @ParameterizedTest void accept(final OrderStatus status) { - final UUID orderId = orderRepository.save(order(status, orderTable(false, 4))).getId(); + final UUID orderId = orderRepository.save(order(status, orderTable(true, 4))).getId(); assertThatThrownBy(() -> orderService.accept(orderId)) .isInstanceOf(IllegalStateException.class); } @@ -297,12 +297,12 @@ void completeTakeoutAndEatInOrder(final OrderStatus status) { @DisplayName("주문 테이블의 모든 매장 주문이 완료되면 빈 테이블로 설정한다.") @Test void completeEatInOrder() { - final OrderTable orderTable = orderTableRepository.save(orderTable(false, 4)); + final OrderTable orderTable = orderTableRepository.save(orderTable(true, 4)); final Order expected = orderRepository.save(order(OrderStatus.SERVED, orderTable)); final Order actual = orderService.complete(expected.getId()); assertAll( () -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.COMPLETED), - () -> assertThat(orderTableRepository.findById(orderTable.getId()).get().isEmpty()).isTrue(), + () -> assertThat(orderTableRepository.findById(orderTable.getId()).get().isOccupied()).isFalse(), () -> assertThat(orderTableRepository.findById(orderTable.getId()).get().getNumberOfGuests()).isEqualTo(0) ); } @@ -310,13 +310,13 @@ void completeEatInOrder() { @DisplayName("완료되지 않은 매장 주문이 있는 주문 테이블은 빈 테이블로 설정하지 않는다.") @Test void completeNotTable() { - final OrderTable orderTable = orderTableRepository.save(orderTable(false, 4)); + final OrderTable orderTable = orderTableRepository.save(orderTable(true, 4)); orderRepository.save(order(OrderStatus.ACCEPTED, orderTable)); final Order expected = orderRepository.save(order(OrderStatus.SERVED, orderTable)); final Order actual = orderService.complete(expected.getId()); assertAll( () -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.COMPLETED), - () -> assertThat(orderTableRepository.findById(orderTable.getId()).get().isEmpty()).isFalse(), + () -> assertThat(orderTableRepository.findById(orderTable.getId()).get().isOccupied()).isTrue(), () -> assertThat(orderTableRepository.findById(orderTable.getId()).get().getNumberOfGuests()).isEqualTo(4) ); } @@ -324,7 +324,7 @@ void completeNotTable() { @DisplayName("주문의 목록을 조회할 수 있다.") @Test void findAll() { - final OrderTable orderTable = orderTableRepository.save(orderTable(false, 4)); + final OrderTable orderTable = orderTableRepository.save(orderTable(true, 4)); orderRepository.save(order(OrderStatus.SERVED, orderTable)); orderRepository.save(order(OrderStatus.DELIVERED, "서울시 송파구 위례성대로 2")); final List actual = orderService.findAll(); diff --git a/src/test/java/kitchenpos/eatinorders/application/OrderTableServiceTest.java b/src/test/java/kitchenpos/eatinorders/application/OrderTableServiceTest.java index ee02b78d4..01551a8d4 100644 --- a/src/test/java/kitchenpos/eatinorders/application/OrderTableServiceTest.java +++ b/src/test/java/kitchenpos/eatinorders/application/OrderTableServiceTest.java @@ -42,7 +42,7 @@ void create() { () -> assertThat(actual.getId()).isNotNull(), () -> assertThat(actual.getName()).isEqualTo(expected.getName()), () -> assertThat(actual.getNumberOfGuests()).isZero(), - () -> assertThat(actual.isEmpty()).isTrue() + () -> assertThat(actual.isOccupied()).isFalse() ); } @@ -58,26 +58,26 @@ void create(final String name) { @DisplayName("빈 테이블을 해지할 수 있다.") @Test void sit() { - final UUID orderTableId = orderTableRepository.save(orderTable(true, 0)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(false, 0)).getId(); final OrderTable actual = orderTableService.sit(orderTableId); - assertThat(actual.isEmpty()).isFalse(); + assertThat(actual.isOccupied()).isTrue(); } @DisplayName("빈 테이블로 설정할 수 있다.") @Test void clear() { - final UUID orderTableId = orderTableRepository.save(orderTable(false, 4)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(true, 4)).getId(); final OrderTable actual = orderTableService.clear(orderTableId); assertAll( () -> assertThat(actual.getNumberOfGuests()).isZero(), - () -> assertThat(actual.isEmpty()).isTrue() + () -> assertThat(actual.isOccupied()).isFalse() ); } @DisplayName("완료되지 않은 주문이 있는 주문 테이블은 빈 테이블로 설정할 수 없다.") @Test void clearWithUncompletedOrders() { - final OrderTable orderTable = orderTableRepository.save(orderTable(false, 4)); + final OrderTable orderTable = orderTableRepository.save(orderTable(true, 4)); final UUID orderTableId = orderTable.getId(); orderRepository.save(order(OrderStatus.ACCEPTED, orderTable)); assertThatThrownBy(() -> orderTableService.clear(orderTableId)) @@ -87,7 +87,7 @@ void clearWithUncompletedOrders() { @DisplayName("방문한 손님 수를 변경할 수 있다.") @Test void changeNumberOfGuests() { - final UUID orderTableId = orderTableRepository.save(orderTable(false, 0)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(true, 0)).getId(); final OrderTable expected = changeNumberOfGuestsRequest(4); final OrderTable actual = orderTableService.changeNumberOfGuests(orderTableId, expected); assertThat(actual.getNumberOfGuests()).isEqualTo(4); @@ -97,7 +97,7 @@ void changeNumberOfGuests() { @ValueSource(ints = -1) @ParameterizedTest void changeNumberOfGuests(final int numberOfGuests) { - final UUID orderTableId = orderTableRepository.save(orderTable(false, 0)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(true, 0)).getId(); final OrderTable expected = changeNumberOfGuestsRequest(numberOfGuests); assertThatThrownBy(() -> orderTableService.changeNumberOfGuests(orderTableId, expected)) .isInstanceOf(IllegalArgumentException.class); @@ -106,7 +106,7 @@ void changeNumberOfGuests(final int numberOfGuests) { @DisplayName("빈 테이블은 방문한 손님 수를 변경할 수 없다.") @Test void changeNumberOfGuestsInEmptyTable() { - final UUID orderTableId = orderTableRepository.save(orderTable(true, 0)).getId(); + final UUID orderTableId = orderTableRepository.save(orderTable(false, 0)).getId(); final OrderTable expected = changeNumberOfGuestsRequest(4); assertThatThrownBy(() -> orderTableService.changeNumberOfGuests(orderTableId, expected)) .isInstanceOf(IllegalStateException.class); diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 000000000..604118678 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,6 @@ +spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE +spring.datasource.username=sa +spring.flyway.enabled=false +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.show-sql=true +logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE