diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..c2065bc2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..8d52aebc --- /dev/null +++ b/build.gradle @@ -0,0 +1,34 @@ +plugins { + id 'org.springframework.boot' version '3.1.0' + id 'io.spring.dependency-management' version '1.1.0' + id 'java' +} + +group = 'nextstep' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '17' + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-starter-jdbc' + + implementation 'dev.akkinoc.spring.boot:logback-access-spring-boot-starter:4.0.0' + + implementation 'io.jsonwebtoken:jjwt-api:0.11.2' + implementation 'io.jsonwebtoken:jjwt-impl:0.11.2' + implementation 'io.jsonwebtoken:jjwt-gson:0.11.2' + + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'io.rest-assured:rest-assured:5.3.1' + + runtimeOnly 'com.h2database:h2' +} + +test { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..c1962a79 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..aa991fce --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 00000000..aeb74cbb --- /dev/null +++ b/gradlew @@ -0,0 +1,245 @@ +#!/bin/sh + +# +# 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. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# 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/HEAD/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 +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 + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +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 ;; #( + 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 + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + 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 +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +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" || "$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 + 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 + # 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 +fi + + +# 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"' + +# 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. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +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 new file mode 100644 index 00000000..93e3f59f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +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" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +: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 %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/roomescape/ExceptionController.java b/src/main/java/roomescape/ExceptionController.java new file mode 100644 index 00000000..4e2450f9 --- /dev/null +++ b/src/main/java/roomescape/ExceptionController.java @@ -0,0 +1,14 @@ +package roomescape; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class ExceptionController { + @ExceptionHandler(Exception.class) + public ResponseEntity handleRuntimeException(Exception e) { + e.printStackTrace(); + return ResponseEntity.badRequest().build(); + } +} diff --git a/src/main/java/roomescape/PageController.java b/src/main/java/roomescape/PageController.java new file mode 100644 index 00000000..ac8ef940 --- /dev/null +++ b/src/main/java/roomescape/PageController.java @@ -0,0 +1,47 @@ +package roomescape; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class PageController { + @GetMapping("/admin") + public String admin() { + return "admin/index"; + } + + @GetMapping("/admin/reservation") + public String adminReservation() { + return "admin/reservation"; + } + + @GetMapping("/admin/theme") + public String adminTheme() { + return "admin/theme"; + } + + @GetMapping("/admin/time") + public String adminTime() { + return "admin/time"; + } + + @GetMapping("/") + public String reservation() { + return "reservation"; + } + + @GetMapping("/reservation-mine") + public String myReservation() { + return "reservation-mine"; + } + + @GetMapping("/login") + public String login() { + return "login"; + } + + @GetMapping("/signup") + public String signup() { + return "signup"; + } +} diff --git a/src/main/java/roomescape/RoomescapeApplication.java b/src/main/java/roomescape/RoomescapeApplication.java new file mode 100644 index 00000000..2ca0f743 --- /dev/null +++ b/src/main/java/roomescape/RoomescapeApplication.java @@ -0,0 +1,11 @@ +package roomescape; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class RoomescapeApplication { + public static void main(String[] args) { + SpringApplication.run(RoomescapeApplication.class, args); + } +} diff --git a/src/main/java/roomescape/member/Member.java b/src/main/java/roomescape/member/Member.java new file mode 100644 index 00000000..903aaa9b --- /dev/null +++ b/src/main/java/roomescape/member/Member.java @@ -0,0 +1,43 @@ +package roomescape.member; + +public class Member { + private Long id; + private String name; + private String email; + private String password; + private String role; + + public Member(Long id, String name, String email, String role) { + this.id = id; + this.name = name; + this.email = email; + this.role = role; + } + + public Member(String name, String email, String password, String role) { + this.name = name; + this.email = email; + this.password = password; + this.role = role; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public String getPassword() { + return password; + } + + public String getRole() { + return role; + } +} diff --git a/src/main/java/roomescape/member/MemberController.java b/src/main/java/roomescape/member/MemberController.java new file mode 100644 index 00000000..881ae5e0 --- /dev/null +++ b/src/main/java/roomescape/member/MemberController.java @@ -0,0 +1,37 @@ +package roomescape.member; + +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.net.URI; + +@RestController +public class MemberController { + private MemberService memberService; + + public MemberController(MemberService memberService) { + this.memberService = memberService; + } + + @PostMapping("/members") + public ResponseEntity createMember(@RequestBody MemberRequest memberRequest) { + MemberResponse member = memberService.createMember(memberRequest); + return ResponseEntity.created(URI.create("/members/" + member.getId())).body(member); + } + + @PostMapping("/logout") + public ResponseEntity logout(HttpServletResponse response) { + Cookie cookie = new Cookie("token", ""); + cookie.setHttpOnly(true); + cookie.setPath("/"); + cookie.setMaxAge(0); + response.addCookie(cookie); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/roomescape/member/MemberDao.java b/src/main/java/roomescape/member/MemberDao.java new file mode 100644 index 00000000..81f77f4c --- /dev/null +++ b/src/main/java/roomescape/member/MemberDao.java @@ -0,0 +1,55 @@ +package roomescape.member; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +@Repository +public class MemberDao { + private JdbcTemplate jdbcTemplate; + + public MemberDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public Member save(Member member) { + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcTemplate.update(connection -> { + var ps = connection.prepareStatement("INSERT INTO member(name, email, password, role) VALUES (?, ?, ?, ?)", new String[]{"id"}); + ps.setString(1, member.getName()); + ps.setString(2, member.getEmail()); + ps.setString(3, member.getPassword()); + ps.setString(4, member.getRole()); + return ps; + }, keyHolder); + + return new Member(keyHolder.getKey().longValue(), member.getName(), member.getEmail(), "USER"); + } + + public Member findByEmailAndPassword(String email, String password) { + return jdbcTemplate.queryForObject( + "SELECT id, name, email, role FROM member WHERE email = ? AND password = ?", + (rs, rowNum) -> new Member( + rs.getLong("id"), + rs.getString("name"), + rs.getString("email"), + rs.getString("role") + ), + email, password + ); + } + + public Member findByName(String name) { + return jdbcTemplate.queryForObject( + "SELECT id, name, email, role FROM member WHERE name = ?", + (rs, rowNum) -> new Member( + rs.getLong("id"), + rs.getString("name"), + rs.getString("email"), + rs.getString("role") + ), + name + ); + } +} diff --git a/src/main/java/roomescape/member/MemberRequest.java b/src/main/java/roomescape/member/MemberRequest.java new file mode 100644 index 00000000..cafb79f1 --- /dev/null +++ b/src/main/java/roomescape/member/MemberRequest.java @@ -0,0 +1,19 @@ +package roomescape.member; + +public class MemberRequest { + private String name; + private String email; + private String password; + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public String getPassword() { + return password; + } +} diff --git a/src/main/java/roomescape/member/MemberResponse.java b/src/main/java/roomescape/member/MemberResponse.java new file mode 100644 index 00000000..b9fa3b97 --- /dev/null +++ b/src/main/java/roomescape/member/MemberResponse.java @@ -0,0 +1,25 @@ +package roomescape.member; + +public class MemberResponse { + private Long id; + private String name; + private String email; + + public MemberResponse(Long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } +} diff --git a/src/main/java/roomescape/member/MemberService.java b/src/main/java/roomescape/member/MemberService.java new file mode 100644 index 00000000..ccaa8cba --- /dev/null +++ b/src/main/java/roomescape/member/MemberService.java @@ -0,0 +1,17 @@ +package roomescape.member; + +import org.springframework.stereotype.Service; + +@Service +public class MemberService { + private MemberDao memberDao; + + public MemberService(MemberDao memberDao) { + this.memberDao = memberDao; + } + + public MemberResponse createMember(MemberRequest memberRequest) { + Member member = memberDao.save(new Member(memberRequest.getName(), memberRequest.getEmail(), memberRequest.getPassword(), "USER")); + return new MemberResponse(member.getId(), member.getName(), member.getEmail()); + } +} diff --git a/src/main/java/roomescape/reservation/Reservation.java b/src/main/java/roomescape/reservation/Reservation.java new file mode 100644 index 00000000..83a7edf1 --- /dev/null +++ b/src/main/java/roomescape/reservation/Reservation.java @@ -0,0 +1,51 @@ +package roomescape.reservation; + +import roomescape.theme.Theme; +import roomescape.time.Time; + +public class Reservation { + private Long id; + private String name; + private String date; + private Time time; + private Theme theme; + + public Reservation(Long id, String name, String date, Time time, Theme theme) { + this.id = id; + this.name = name; + this.date = date; + this.time = time; + this.theme = theme; + } + + public Reservation(String name, String date, Time time, Theme theme) { + this.name = name; + this.date = date; + this.time = time; + this.theme = theme; + } + + public Reservation() { + + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDate() { + return date; + } + + public Time getTime() { + return time; + } + + public Theme getTheme() { + return theme; + } +} diff --git a/src/main/java/roomescape/reservation/ReservationController.java b/src/main/java/roomescape/reservation/ReservationController.java new file mode 100644 index 00000000..b3bef399 --- /dev/null +++ b/src/main/java/roomescape/reservation/ReservationController.java @@ -0,0 +1,46 @@ +package roomescape.reservation; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.net.URI; +import java.util.List; + +@RestController +public class ReservationController { + + private final ReservationService reservationService; + + public ReservationController(ReservationService reservationService) { + this.reservationService = reservationService; + } + + @GetMapping("/reservations") + public List list() { + return reservationService.findAll(); + } + + @PostMapping("/reservations") + public ResponseEntity create(@RequestBody ReservationRequest reservationRequest) { + if (reservationRequest.getName() == null + || reservationRequest.getDate() == null + || reservationRequest.getTheme() == null + || reservationRequest.getTime() == null) { + return ResponseEntity.badRequest().build(); + } + ReservationResponse reservation = reservationService.save(reservationRequest); + + return ResponseEntity.created(URI.create("/reservations/" + reservation.getId())).body(reservation); + } + + @DeleteMapping("/reservations/{id}") + public ResponseEntity delete(@PathVariable Long id) { + reservationService.deleteById(id); + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/roomescape/reservation/ReservationDao.java b/src/main/java/roomescape/reservation/ReservationDao.java new file mode 100644 index 00000000..a4972430 --- /dev/null +++ b/src/main/java/roomescape/reservation/ReservationDao.java @@ -0,0 +1,127 @@ +package roomescape.reservation; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; +import roomescape.theme.Theme; +import roomescape.time.Time; + +import java.sql.PreparedStatement; +import java.util.List; + +@Repository +public class ReservationDao { + + private final JdbcTemplate jdbcTemplate; + + public ReservationDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List findAll() { + return jdbcTemplate.query( + "SELECT r.id AS reservation_id, r.name as reservation_name, r.date as reservation_date, " + + "t.id AS theme_id, t.name AS theme_name, t.description AS theme_description, " + + "ti.id AS time_id, ti.time_value AS time_value " + + "FROM reservation r " + + "JOIN theme t ON r.theme_id = t.id " + + "JOIN time ti ON r.time_id = ti.id", + + (rs, rowNum) -> new Reservation( + rs.getLong("reservation_id"), + rs.getString("reservation_name"), + rs.getString("reservation_date"), + new Time( + rs.getLong("time_id"), + rs.getString("time_value") + ), + new Theme( + rs.getLong("theme_id"), + rs.getString("theme_name"), + rs.getString("theme_description") + ))); + } + + public Reservation save(ReservationRequest reservationRequest) { + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcTemplate.update(connection -> { + PreparedStatement ps = connection.prepareStatement("INSERT INTO reservation(date, name, theme_id, time_id) VALUES (?, ?, ?, ?)", new String[]{"id"}); + ps.setString(1, reservationRequest.getDate()); + ps.setString(2, reservationRequest.getName()); + ps.setLong(3, reservationRequest.getTheme()); + ps.setLong(4, reservationRequest.getTime()); + return ps; + }, keyHolder); + + Time time = jdbcTemplate.queryForObject("SELECT * FROM time WHERE id = ?", + (rs, rowNum) -> new Time(rs.getLong("id"), rs.getString("time_value")), + reservationRequest.getTime()); + + Theme theme = jdbcTemplate.queryForObject("SELECT * FROM theme WHERE id = ?", + (rs, rowNum) -> new Theme(rs.getLong("id"), rs.getString("name"), rs.getString("description")), + reservationRequest.getTheme()); + + return new Reservation( + keyHolder.getKey().longValue(), + reservationRequest.getName(), + reservationRequest.getDate(), + time, + theme + ); + } + + public void deleteById(Long id) { + jdbcTemplate.update("DELETE FROM reservation WHERE id = ?", id); + } + + public List findReservationsByDateAndTheme(String date, Long themeId) { + return jdbcTemplate.query( + "SELECT r.id AS reservation_id, r.name as reservation_name, r.date as reservation_date, " + + "t.id AS theme_id, t.name AS theme_name, t.description AS theme_description, " + + "ti.id AS time_id, ti.time_value AS time_value " + + "FROM reservation r " + + "JOIN theme t ON r.theme_id = t.id " + + "JOIN time ti ON r.time_id = ti.id" + + "WHERE r.date = ? AND r.theme_id = ?", + new Object[]{date, themeId}, + (rs, rowNum) -> new Reservation( + rs.getLong("reservation_id"), + rs.getString("reservation_name"), + rs.getString("reservation_date"), + new Time( + rs.getLong("time_id"), + rs.getString("time_value") + ), + new Theme( + rs.getLong("theme_id"), + rs.getString("theme_name"), + rs.getString("theme_description") + ))); + } + + public List findByDateAndThemeId(String date, Long themeId) { + return jdbcTemplate.query( + "SELECT r.id AS reservation_id, r.name as reservation_name, r.date as reservation_date, " + + "t.id AS theme_id, t.name AS theme_name, t.description AS theme_description, " + + "ti.id AS time_id, ti.time_value AS time_value " + + "FROM reservation r " + + "JOIN theme t ON r.theme_id = t.id " + + "JOIN time ti ON r.time_id = ti.id " + + "WHERE r.date = ? AND r.theme_id = ?", + new Object[]{date, themeId}, + (rs, rowNum) -> new Reservation( + rs.getLong("reservation_id"), + rs.getString("reservation_name"), + rs.getString("reservation_date"), + new Time( + rs.getLong("time_id"), + rs.getString("time_value") + ), + new Theme( + rs.getLong("theme_id"), + rs.getString("theme_name"), + rs.getString("theme_description") + ))); + } +} diff --git a/src/main/java/roomescape/reservation/ReservationRequest.java b/src/main/java/roomescape/reservation/ReservationRequest.java new file mode 100644 index 00000000..19f44124 --- /dev/null +++ b/src/main/java/roomescape/reservation/ReservationRequest.java @@ -0,0 +1,24 @@ +package roomescape.reservation; + +public class ReservationRequest { + private String name; + private String date; + private Long theme; + private Long time; + + public String getName() { + return name; + } + + public String getDate() { + return date; + } + + public Long getTheme() { + return theme; + } + + public Long getTime() { + return time; + } +} diff --git a/src/main/java/roomescape/reservation/ReservationResponse.java b/src/main/java/roomescape/reservation/ReservationResponse.java new file mode 100644 index 00000000..41360a36 --- /dev/null +++ b/src/main/java/roomescape/reservation/ReservationResponse.java @@ -0,0 +1,37 @@ +package roomescape.reservation; + +public class ReservationResponse { + private Long id; + private String name; + private String theme; + private String date; + private String time; + + public ReservationResponse(Long id, String name, String theme, String date, String time) { + this.id = id; + this.name = name; + this.theme = theme; + this.date = date; + this.time = time; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getTheme() { + return theme; + } + + public String getDate() { + return date; + } + + public String getTime() { + return time; + } +} diff --git a/src/main/java/roomescape/reservation/ReservationService.java b/src/main/java/roomescape/reservation/ReservationService.java new file mode 100644 index 00000000..bd331332 --- /dev/null +++ b/src/main/java/roomescape/reservation/ReservationService.java @@ -0,0 +1,30 @@ +package roomescape.reservation; + +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ReservationService { + private ReservationDao reservationDao; + + public ReservationService(ReservationDao reservationDao) { + this.reservationDao = reservationDao; + } + + public ReservationResponse save(ReservationRequest reservationRequest) { + Reservation reservation = reservationDao.save(reservationRequest); + + return new ReservationResponse(reservation.getId(), reservationRequest.getName(), reservation.getTheme().getName(), reservation.getDate(), reservation.getTime().getValue()); + } + + public void deleteById(Long id) { + reservationDao.deleteById(id); + } + + public List findAll() { + return reservationDao.findAll().stream() + .map(it -> new ReservationResponse(it.getId(), it.getName(), it.getTheme().getName(), it.getDate(), it.getTime().getValue())) + .toList(); + } +} diff --git a/src/main/java/roomescape/theme/Theme.java b/src/main/java/roomescape/theme/Theme.java new file mode 100644 index 00000000..430a6239 --- /dev/null +++ b/src/main/java/roomescape/theme/Theme.java @@ -0,0 +1,33 @@ +package roomescape.theme; + +public class Theme { + private Long id; + private String name; + private String description; + + public Theme() { + } + + public Theme(Long id, String name, String description) { + this.id = id; + this.name = name; + this.description = description; + } + + public Theme(String name, String description) { + this.name = name; + this.description = description; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } +} diff --git a/src/main/java/roomescape/theme/ThemeController.java b/src/main/java/roomescape/theme/ThemeController.java new file mode 100644 index 00000000..03bca41a --- /dev/null +++ b/src/main/java/roomescape/theme/ThemeController.java @@ -0,0 +1,38 @@ +package roomescape.theme; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.net.URI; +import java.util.List; + +@RestController +public class ThemeController { + private ThemeDao themeDao; + + public ThemeController(ThemeDao themeDao) { + this.themeDao = themeDao; + } + + @PostMapping("/themes") + public ResponseEntity createTheme(@RequestBody Theme theme) { + Theme newTheme = themeDao.save(theme); + return ResponseEntity.created(URI.create("/themes/" + newTheme.getId())).body(newTheme); + } + + @GetMapping("/themes") + public ResponseEntity> list() { + return ResponseEntity.ok(themeDao.findAll()); + } + + @DeleteMapping("/themes/{id}") + public ResponseEntity deleteTheme(@PathVariable Long id) { + themeDao.deleteById(id); + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/roomescape/theme/ThemeDao.java b/src/main/java/roomescape/theme/ThemeDao.java new file mode 100644 index 00000000..945341d8 --- /dev/null +++ b/src/main/java/roomescape/theme/ThemeDao.java @@ -0,0 +1,41 @@ +package roomescape.theme; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class ThemeDao { + private JdbcTemplate jdbcTemplate; + + public ThemeDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List findAll() { + return jdbcTemplate.query("SELECT * FROM theme where deleted = false", (rs, rowNum) -> new Theme( + rs.getLong("id"), + rs.getString("name"), + rs.getString("description") + )); + } + + public Theme save(Theme theme) { + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcTemplate.update(connection -> { + var ps = connection.prepareStatement("INSERT INTO theme(name, description) VALUES (?, ?)", new String[]{"id"}); + ps.setString(1, theme.getName()); + ps.setString(2, theme.getDescription()); + return ps; + }, keyHolder); + + return new Theme(keyHolder.getKey().longValue(), theme.getName(), theme.getDescription()); + } + + public void deleteById(Long id) { + jdbcTemplate.update("UPDATE theme SET deleted = true WHERE id = ?", id); + } +} diff --git a/src/main/java/roomescape/time/AvailableTime.java b/src/main/java/roomescape/time/AvailableTime.java new file mode 100644 index 00000000..33acef7e --- /dev/null +++ b/src/main/java/roomescape/time/AvailableTime.java @@ -0,0 +1,25 @@ +package roomescape.time; + +public class AvailableTime { + private Long timeId; + private String time; + private boolean booked; + + public AvailableTime(Long timeId, String time, boolean booked) { + this.timeId = timeId; + this.time = time; + this.booked = booked; + } + + public Long getTimeId() { + return timeId; + } + + public String getTime() { + return time; + } + + public boolean isBooked() { + return booked; + } +} diff --git a/src/main/java/roomescape/time/Time.java b/src/main/java/roomescape/time/Time.java new file mode 100644 index 00000000..008ed93c --- /dev/null +++ b/src/main/java/roomescape/time/Time.java @@ -0,0 +1,27 @@ +package roomescape.time; + +public class Time { + private Long id; + private String value; + + public Time(Long id, String value) { + this.id = id; + this.value = value; + } + + public Time(String value) { + this.value = value; + } + + public Time() { + + } + + public Long getId() { + return id; + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/roomescape/time/TimeController.java b/src/main/java/roomescape/time/TimeController.java new file mode 100644 index 00000000..2343114d --- /dev/null +++ b/src/main/java/roomescape/time/TimeController.java @@ -0,0 +1,48 @@ +package roomescape.time; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.net.URI; +import java.util.List; + +@RestController +public class TimeController { + private TimeService timeService; + + public TimeController(TimeService timeService) { + this.timeService = timeService; + } + + @GetMapping("/times") + public List