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..48fcc978 --- /dev/null +++ b/build.gradle @@ -0,0 +1,34 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '2.7.9' + id 'io.spring.dependency-management' version '1.0.15.RELEASE' +} + +group = 'com.example' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '11' + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-web' + compileOnly 'org.projectlombok:lombok' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + runtimeOnly 'com.mysql:mysql-connector-j' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.springdoc:springdoc-openapi-ui:1.6.8' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..249e5832 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..070cb702 --- /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.6-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 00000000..a69d9cb6 --- /dev/null +++ b/gradlew @@ -0,0 +1,240 @@ +#!/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/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 +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 + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +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 + +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*) + 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 + +# 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 + +# 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..f127cfd4 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,91 @@ +@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=. +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/settings.gradle b/settings.gradle new file mode 100644 index 00000000..d3c254e6 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'techeer' diff --git a/src/main/java/com/techeer/hackathon/TecheerApplication.java b/src/main/java/com/techeer/hackathon/TecheerApplication.java new file mode 100644 index 00000000..511312db --- /dev/null +++ b/src/main/java/com/techeer/hackathon/TecheerApplication.java @@ -0,0 +1,15 @@ +package com.techeer.hackathon; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@SpringBootApplication +@EnableJpaAuditing +public class TecheerApplication { + + public static void main(String[] args) { + SpringApplication.run(TecheerApplication.class, args); + } + +} diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/controller/RestaurantController.java b/src/main/java/com/techeer/hackathon/domain/restaurant/controller/RestaurantController.java new file mode 100644 index 00000000..2576a0c9 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/controller/RestaurantController.java @@ -0,0 +1,74 @@ +package com.techeer.hackathon.domain.restaurant.controller; + +import com.techeer.hackathon.domain.restaurant.dto.RestaurantCreateDto; +import com.techeer.hackathon.domain.restaurant.dto.RestaurantResponseDto; +import com.techeer.hackathon.domain.restaurant.dto.RestaurantUpdateDto; +import com.techeer.hackathon.domain.restaurant.dto.mapper.RestaurantMapper; +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import com.techeer.hackathon.domain.restaurant.service.RestaurantService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; + +@RequestMapping("/api/v1/restaurant") +@RestController +@RequiredArgsConstructor +public class RestaurantController { + private final RestaurantService R_Service; + private final RestaurantMapper R_Mapper; + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public ResponseEntity createRestaurant( + @RequestBody @Valid RestaurantCreateDto restaurantCreateDto) { + Restaurant insertRestaurant = R_Service.insertRestaurant(restaurantCreateDto); + return new ResponseEntity(R_Mapper.DtoFromEntity(insertRestaurant), HttpStatus.CREATED); + } + +// @GetMapping +// @ResponseStatus(HttpStatus.OK) +// public ResponseEntity> findAllRestaurant(){ //List +// List restaurants = R_Service.findAllRestaurant(); +// //for문 사용 +//// return new ResponseEntity<>( +//// restaurants.stream().map(R_Mapper::DtoFromEntity).collect(Collectors.toList()) +//// , HttpStatus.OK); +// return new ResponseEntity<>(restaurants, HttpStatus.OK); +// } + + @GetMapping("/page") + public ResponseEntity> getAllRestaurant( + @RequestParam(defaultValue = "5") Integer size, @RequestParam(defaultValue = "0") Integer offset) { + return ResponseEntity.ok(R_Service.findAllRestaurant(size, offset)); + } +// + @GetMapping("/category") + public ResponseEntity getRestaurantByCategory( + @RequestParam("category") String category, @RequestParam(defaultValue = "10") Integer size, + @RequestParam(defaultValue = "0") Integer offset) { + return ResponseEntity.ok(R_Service.findRestaurantsByCategory(category, size, offset)); + } + + @GetMapping("/{id}") + @ResponseStatus(HttpStatus.OK) + public ResponseEntity findOneRestaurant(@PathVariable Long id){ + Restaurant restaurant = R_Service.findOneRestaurant(id); + return new ResponseEntity<>(restaurant, HttpStatus.OK); + } + + @PutMapping + @ResponseStatus(HttpStatus.OK) + public ResponseEntity updateRestaurant(@Valid @RequestBody RestaurantUpdateDto restaurantUpdate) { + Restaurant updateRestaurant = R_Service.updateRestaurant(restaurantUpdate); + return new ResponseEntity<>(R_Mapper.DtoFromEntity(updateRestaurant), HttpStatus.OK); } + + @DeleteMapping("/{id}") + public void deleteRestaurant(@PathVariable Long id) { + R_Service.deleteRestaurant(id); + } + +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantCreateDto.java b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantCreateDto.java new file mode 100644 index 00000000..a4df36bb --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantCreateDto.java @@ -0,0 +1,16 @@ +package com.techeer.hackathon.domain.restaurant.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import javax.validation.constraints.NotNull; + +@Getter +@RequiredArgsConstructor +public class RestaurantCreateDto { + @NotNull + private String name; + + @NotNull + private String category; +} diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantResponseDto.java b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantResponseDto.java new file mode 100644 index 00000000..8c8636e0 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantResponseDto.java @@ -0,0 +1,15 @@ +package com.techeer.hackathon.domain.restaurant.dto; + +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDateTime; + +@Builder +@Getter +public class RestaurantResponseDto { + private Long id; + private String name; + private String category; + private LocalDateTime createdAt; +} diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantUpdateDto.java b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantUpdateDto.java new file mode 100644 index 00000000..ca6424aa --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/RestaurantUpdateDto.java @@ -0,0 +1,16 @@ +package com.techeer.hackathon.domain.restaurant.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import javax.validation.constraints.NotNull; + +@Getter +@RequiredArgsConstructor +public class RestaurantUpdateDto { + @NotNull + private Long id; + + @NotNull + private String category; +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/dto/mapper/RestaurantMapper.java b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/mapper/RestaurantMapper.java new file mode 100644 index 00000000..cd23f952 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/dto/mapper/RestaurantMapper.java @@ -0,0 +1,37 @@ +package com.techeer.hackathon.domain.restaurant.dto.mapper; + +import com.techeer.hackathon.domain.restaurant.dto.RestaurantCreateDto; +import com.techeer.hackathon.domain.restaurant.dto.RestaurantResponseDto; +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.stream.Collectors; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Component +public class RestaurantMapper { + public Restaurant DtoToEntity(RestaurantCreateDto restaurantCreateDto) { + return Restaurant.builder() + .name(restaurantCreateDto.getName()) + .category(restaurantCreateDto.getCategory()) + .build(); + } + + public RestaurantResponseDto DtoFromEntity(Restaurant restaurant) { + return RestaurantResponseDto.builder() + .id(restaurant.getId()) + .name(restaurant.getName()) + .category(restaurant.getCategory()) + .createdAt(restaurant.getCreatedAt()) + .build(); + } + + public List restaurantResponseListFromEntity(Page restaurantPage){ + List restaurantResponseList = restaurantPage.stream().map(this::DtoFromEntity).collect(Collectors.toList()); + return restaurantResponseList; + } +} diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/entity/Restaurant.java b/src/main/java/com/techeer/hackathon/domain/restaurant/entity/Restaurant.java new file mode 100644 index 00000000..d8b14687 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/entity/Restaurant.java @@ -0,0 +1,41 @@ +package com.techeer.hackathon.domain.restaurant.entity; + +import com.techeer.hackathon.domain.restaurant.dto.RestaurantUpdateDto; +import com.techeer.hackathon.global.BaseEntity; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Entity +@Getter +@NoArgsConstructor +@Table(name="restaurant") +public class Restaurant extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "category", nullable = false) + private String category; + + @Column(name = "name", nullable = false) + private String name; + + @Builder + public Restaurant(String category, String name) { + this.category = category; + this.name = name; + this.activated = true; + } + + public void update(RestaurantUpdateDto restaurantUpdate){ + this.category = restaurantUpdate.getCategory(); + } + + public void delete() { //soft delete + this.activated = false; + } +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/repository/RestaurantRepository.java b/src/main/java/com/techeer/hackathon/domain/restaurant/repository/RestaurantRepository.java new file mode 100644 index 00000000..18381166 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/repository/RestaurantRepository.java @@ -0,0 +1,20 @@ +package com.techeer.hackathon.domain.restaurant.repository; + +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import org.springframework.data.domain.Page; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import org.springframework.data.domain.Pageable; +import java.util.List; + +@Repository +public interface RestaurantRepository extends JpaRepository { + + @Query("select r from Restaurant r where r.activated is true") + Page findRestaurants(Pageable pageable); + @Query("select r from Restaurant r where r.activated is true and r.category = :category") + Page findByCategory(@Param("category")String category, Pageable pageable); +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/restaurant/service/RestaurantService.java b/src/main/java/com/techeer/hackathon/domain/restaurant/service/RestaurantService.java new file mode 100644 index 00000000..aa5974cb --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/restaurant/service/RestaurantService.java @@ -0,0 +1,61 @@ +package com.techeer.hackathon.domain.restaurant.service; + +import com.techeer.hackathon.domain.restaurant.dto.RestaurantCreateDto; +import com.techeer.hackathon.domain.restaurant.dto.RestaurantResponseDto; +import com.techeer.hackathon.domain.restaurant.dto.RestaurantUpdateDto; +import com.techeer.hackathon.domain.restaurant.dto.mapper.RestaurantMapper; +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import com.techeer.hackathon.domain.restaurant.repository.RestaurantRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.crossstore.ChangeSetPersister; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class RestaurantService { + private final RestaurantRepository R_Repository; + private final RestaurantMapper R_Mapper; + + public Restaurant insertRestaurant(RestaurantCreateDto restaurantCreateDto) { + return R_Repository.save(R_Mapper.DtoToEntity(restaurantCreateDto)); + } + + + + public Restaurant findOneRestaurant(Long id) { + return R_Repository.findById(id).orElseThrow(RuntimeException::new); + } + + public Restaurant updateRestaurant(RestaurantUpdateDto restaurantUpdate) { + Restaurant restaurant = IsRestaurantExisted(restaurantUpdate.getId()); + restaurant.update(restaurantUpdate); + return R_Repository.save(restaurant); + } + + @Transactional + public void deleteRestaurant(Long id) { + Restaurant restaurant = IsRestaurantExisted(id); + restaurant.delete(); + } + + private Restaurant IsRestaurantExisted(Long id){ + Restaurant foundRestaurant = R_Repository.findById(id).orElseThrow(NullPointerException::new); + return foundRestaurant; + } + + public List findAllRestaurant(Integer size, Integer offSet){ + Pageable pageable = PageRequest.of(offSet, size); + return R_Mapper.restaurantResponseListFromEntity(R_Repository.findRestaurants(pageable)); + } + + public Object findRestaurantsByCategory(String category, Integer size, Integer offset) { + Pageable pageable = PageRequest.of(offset, size); + return R_Mapper.restaurantResponseListFromEntity(R_Repository.findByCategory(category,pageable)); + } +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/review/controller/ReviewController.java b/src/main/java/com/techeer/hackathon/domain/review/controller/ReviewController.java new file mode 100644 index 00000000..53c453a2 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/controller/ReviewController.java @@ -0,0 +1,76 @@ +package com.techeer.hackathon.domain.review.controller; + +import com.techeer.hackathon.domain.restaurant.dto.RestaurantCreateDto; +import com.techeer.hackathon.domain.restaurant.dto.RestaurantUpdateDto; +import com.techeer.hackathon.domain.restaurant.dto.mapper.RestaurantMapper; +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import com.techeer.hackathon.domain.restaurant.service.RestaurantService; +import com.techeer.hackathon.domain.review.dto.ReviewCreateDto; +import com.techeer.hackathon.domain.review.dto.ReviewResponseDto; +import com.techeer.hackathon.domain.review.dto.ReviewUpdateDto; +import com.techeer.hackathon.domain.review.dto.mapper.ReviewMapper; +import com.techeer.hackathon.domain.review.entity.Review; +import com.techeer.hackathon.domain.review.service.ReviewService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; + +@RequestMapping("/api/v1/review") +@RestController +@RequiredArgsConstructor +public class ReviewController { + private final ReviewService R_Service; + private final ReviewMapper R_Mapper; + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public ResponseEntity createReview( + @RequestBody @Valid ReviewCreateDto reviewCreateDto){ + Review insertReview = R_Service.insertReview(reviewCreateDto); + return new ResponseEntity(R_Mapper.DtoFromEntity(insertReview), HttpStatus.CREATED); + } + + @PutMapping + @ResponseStatus(HttpStatus.OK) + public ResponseEntity updateReview( + @Valid @RequestBody ReviewUpdateDto reviewUpdate){ + Review updateReview = R_Service.updateReview(reviewUpdate); + return new ResponseEntity<>(R_Mapper.DtoFromEntity(updateReview), HttpStatus.OK); + } + + @DeleteMapping("/{id}") + @ResponseStatus(HttpStatus.OK) + public void deleteReview(@PathVariable Long id) { + R_Service.deleteReview(id); + } + + @GetMapping("/{id}") + public ResponseEntity findOneReview(@PathVariable Long id) { + Review findReview = R_Service.findOneReview(id); + return new ResponseEntity<>(R_Mapper.DtoFromEntity(findReview), HttpStatus.OK); + } + + @GetMapping("/search") + public ResponseEntity findReviewsBySearch( + @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size, + @RequestParam(required = false) String title, @RequestParam(required = false) String content) { + return ResponseEntity.ok(R_Service.findReviewsBySearch(page, size, title, content)); + } + + @GetMapping("/createdAt") + public ResponseEntity> findAllReviewsByCreatedAt() { + List reviews = R_Service.findReviewsByCreatedAt(); + return ResponseEntity.ok(reviews); + } + + @GetMapping("/createdAtDesc") + public ResponseEntity> findAllReviewsByCreatedAtDesc() { + List reviews = R_Service.findReviewsByCreatedAtDesc(); + return ResponseEntity.ok(reviews); + } + +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewCreateDto.java b/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewCreateDto.java new file mode 100644 index 00000000..51bf89a4 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewCreateDto.java @@ -0,0 +1,20 @@ +package com.techeer.hackathon.domain.review.dto; + +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import javax.validation.constraints.NotNull; + +@Getter +@RequiredArgsConstructor +public class ReviewCreateDto { + @NotNull + private String title; + + @NotNull + private String content; + + @NotNull + private Long restaurantId; +} diff --git a/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewResponseDto.java b/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewResponseDto.java new file mode 100644 index 00000000..95b83581 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewResponseDto.java @@ -0,0 +1,19 @@ +package com.techeer.hackathon.domain.review.dto; + +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Builder +@Getter +@AllArgsConstructor +public class ReviewResponseDto { + private Long id; + private String restaurantName; + private String title; + private String content; +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewUpdateDto.java b/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewUpdateDto.java new file mode 100644 index 00000000..f6d14362 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/dto/ReviewUpdateDto.java @@ -0,0 +1,19 @@ +package com.techeer.hackathon.domain.review.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import javax.validation.constraints.NotNull; + +@Getter +@RequiredArgsConstructor +public class ReviewUpdateDto { + @NotNull + private Long id; + + @NotNull + private String title; + + @NotNull + private String content; +} diff --git a/src/main/java/com/techeer/hackathon/domain/review/dto/mapper/ReviewMapper.java b/src/main/java/com/techeer/hackathon/domain/review/dto/mapper/ReviewMapper.java new file mode 100644 index 00000000..ca1d3a37 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/dto/mapper/ReviewMapper.java @@ -0,0 +1,45 @@ +package com.techeer.hackathon.domain.review.dto.mapper; + +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import com.techeer.hackathon.domain.restaurant.repository.RestaurantRepository; +import com.techeer.hackathon.domain.review.dto.ReviewCreateDto; +import com.techeer.hackathon.domain.review.dto.ReviewResponseDto; +import com.techeer.hackathon.domain.review.entity.Review; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.stream.Collectors; + +//@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Component +@RequiredArgsConstructor +public class ReviewMapper { + private final RestaurantRepository Rest_Repository; + + public Review DtoToEntity(ReviewCreateDto reviewCreateDto) { + Restaurant restaurant = Rest_Repository.findById(reviewCreateDto.getRestaurantId()).orElseThrow(RuntimeException::new); + return Review.builder() + .title(reviewCreateDto.getTitle()) + .content(reviewCreateDto.getContent()) + .restaurant(restaurant) + .build(); + } + + public ReviewResponseDto DtoFromEntity(Review review) { + return ReviewResponseDto.builder() + .id(review.getId()) + .title(review.getTitle()) + .content(review.getContent()) + .restaurantName(review.getRestaurant().getName()) + .build(); + } + + public List reviewResponseListFromEntity(Page reviewPage){ + List reviewResponseList = reviewPage.stream().map(this::DtoFromEntity).collect(Collectors.toList()); + return reviewResponseList; + } +} diff --git a/src/main/java/com/techeer/hackathon/domain/review/entity/Review.java b/src/main/java/com/techeer/hackathon/domain/review/entity/Review.java new file mode 100644 index 00000000..da7aafaf --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/entity/Review.java @@ -0,0 +1,46 @@ +package com.techeer.hackathon.domain.review.entity; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import com.techeer.hackathon.domain.review.dto.ReviewUpdateDto; +import com.techeer.hackathon.global.BaseEntity; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Entity +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Table(name="review") +public class Review extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "restaurant_id", nullable = false) + @JsonIgnore //json response에서 figure를 제외하고 보내기 + private Restaurant restaurant; + + @Column(name = "title", nullable = false) + private String title; + + @Column(name = "content", nullable = false) + private String content; + + @Builder + public Review(String title, String content, Restaurant restaurant){ + this.title = title; + this.content = content; + this.restaurant = restaurant; + } + + public void update(ReviewUpdateDto reviewUpdate) { + this.title = reviewUpdate.getTitle(); + this.content = reviewUpdate.getContent(); + } +} \ No newline at end of file diff --git a/src/main/java/com/techeer/hackathon/domain/review/repository/ReviewRepository.java b/src/main/java/com/techeer/hackathon/domain/review/repository/ReviewRepository.java new file mode 100644 index 00000000..578ee293 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/repository/ReviewRepository.java @@ -0,0 +1,31 @@ +package com.techeer.hackathon.domain.review.repository; + +import com.techeer.hackathon.domain.restaurant.entity.Restaurant; +import com.techeer.hackathon.domain.review.entity.Review; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ReviewRepository extends JpaRepository { + + @Query(value = "SELECT r\n " + + "FROM Review r\n " + + "WHERE (r.title = '' OR r.title Like CONCAT('%', :title, '%'))\n " + + "AND (r.content = '' OR r.content Like CONCAT('%', :content, '%'))\n") + Page findAllBySearch(String title, String content, Pageable pageable); + +// @Query( "SELECT r FROM Review r WHERE r.title LIKE %:title% OR r.content LIKE %:content%" ) +// Page findAllBySearch(String title, String content, Pageable pageable); + + @Query( "SELECT r FROM Review r ORDER BY r.createdAt" ) + List findAllByCreatedAt(); + + @Query( "SELECT r FROM Review r ORDER BY r.createdAt DESC" ) + List findAllByCreatedAtDesc(); +} diff --git a/src/main/java/com/techeer/hackathon/domain/review/service/ReviewService.java b/src/main/java/com/techeer/hackathon/domain/review/service/ReviewService.java new file mode 100644 index 00000000..727cd2b7 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/domain/review/service/ReviewService.java @@ -0,0 +1,55 @@ +package com.techeer.hackathon.domain.review.service; + +import com.techeer.hackathon.domain.review.dto.ReviewCreateDto; +import com.techeer.hackathon.domain.review.dto.ReviewUpdateDto; +import com.techeer.hackathon.domain.review.dto.mapper.ReviewMapper; +import com.techeer.hackathon.domain.review.entity.Review; +import com.techeer.hackathon.domain.review.repository.ReviewRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class ReviewService { + private final ReviewRepository R_Repository; + private final ReviewMapper R_Mapper; + + public Review insertReview(ReviewCreateDto reviewCreateDto) { + return R_Repository.save(R_Mapper.DtoToEntity(reviewCreateDto)); + } + + public Review updateReview(ReviewUpdateDto reviewUpdate) { + Review updateReview = R_Repository.findById(reviewUpdate.getId()).orElseThrow(NullPointerException::new); + updateReview.update(reviewUpdate); + return R_Repository.save(updateReview); + } + + public void deleteReview(Long id) { + Review review = R_Repository.findById(id).orElseThrow(NullPointerException::new); + R_Repository.deleteById(review.getId()); + } + + public Review findOneReview(Long id) { + return R_Repository.findById(id).orElseThrow(RuntimeException::new); + } + + + public Object findReviewsBySearch(Integer page, Integer size, String title, String content) { + Pageable pageable = PageRequest.of(page, size); + return R_Mapper.reviewResponseListFromEntity(R_Repository.findAllBySearch(title, content ,pageable)); + + } + + public List findReviewsByCreatedAt() { + return R_Repository.findAllByCreatedAt(); + } + + + public List findReviewsByCreatedAtDesc() { + return R_Repository.findAllByCreatedAtDesc(); + } +} diff --git a/src/main/java/com/techeer/hackathon/global/BaseEntity.java b/src/main/java/com/techeer/hackathon/global/BaseEntity.java new file mode 100644 index 00000000..700e9507 --- /dev/null +++ b/src/main/java/com/techeer/hackathon/global/BaseEntity.java @@ -0,0 +1,25 @@ +package com.techeer.hackathon.global; + +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.Column; +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) // Auditing(자동으로 값 매핑) +@Getter +public class BaseEntity { + @CreatedDate + private LocalDateTime createdAt; + + @LastModifiedDate + private LocalDateTime updatedAt; + + @Column(name = "activated", nullable = false) + protected boolean activated = true; +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 00000000..9ac5c9b3 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,21 @@ +spring: + jpa: + database: "mysql" + show-sql: "true" + hibernate: + ddl-auto: "update" + properties: + hibernate: + format_sql: true + generate-ddl: true + + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: "jdbc:mysql://localhost:3306/techeerdb?useSSL=false&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Seoul&characterEncoding=UTF-8&autoReconnect=true&createDatabaseIfNotExist=true" + username: root + password: mysql0824 + +logging: + level: + org: + hibernate: info \ No newline at end of file diff --git a/src/test/java/com/techeer/hackathon/TecheerApplicationTests.java b/src/test/java/com/techeer/hackathon/TecheerApplicationTests.java new file mode 100644 index 00000000..a504a07a --- /dev/null +++ b/src/test/java/com/techeer/hackathon/TecheerApplicationTests.java @@ -0,0 +1,13 @@ +package com.techeer.hackathon; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class TecheerApplicationTests { + + @Test + void contextLoads() { + } + +}