From c61c67d324d8050555297a23fa097f3a483cf1d0 Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Mon, 15 Jul 2024 18:33:25 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=EC=A0=84=EC=B2=B4=EC=A0=81=EC=9D=B8=20?= =?UTF-8?q?=EC=B6=A9=EB=8F=8C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/roomescape/controller/ReservationController.java | 2 ++ .../roomescape/controller/ReservationTimeController.java | 2 +- src/main/java/roomescape/controller/ThemeController.java | 6 ++++++ src/main/java/roomescape/domain/ReservationTime.java | 1 + src/main/java/roomescape/domain/Theme.java | 1 - .../dto/reservation/create/ReservationCreateResponse.java | 1 + src/main/java/roomescape/exception/ErrorCodeResponse.java | 3 +-- .../java/roomescape/service/ReservationTimeService.java | 1 + src/main/resources/application.properties | 2 +- .../roomescape/controller/ReservationControllerTest.java | 1 - .../controller/ReservationTimeControllerTest.java | 4 ---- .../java/roomescape/controller/ThemeControllerTest.java | 5 ----- .../java/roomescape/service/ReservationServiceTest.java | 1 - .../roomescape/service/ReservationTimeServiceTest.java | 2 -- src/test/java/roomescape/service/ThemeServiceTest.java | 7 ++++++- 15 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java index b4fd7b77..2b277133 100644 --- a/src/main/java/roomescape/controller/ReservationController.java +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -1,11 +1,13 @@ package roomescape.controller; import jakarta.validation.Valid; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import roomescape.dto.reservation.ReservationsResponse; import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.dto.reservation.create.ReservationCreateResponse; +import roomescape.exception.ErrorCodeResponse; import roomescape.service.ReservationService; import java.net.URI; diff --git a/src/main/java/roomescape/controller/ReservationTimeController.java b/src/main/java/roomescape/controller/ReservationTimeController.java index fbc8c5d8..9e6473c4 100644 --- a/src/main/java/roomescape/controller/ReservationTimeController.java +++ b/src/main/java/roomescape/controller/ReservationTimeController.java @@ -40,7 +40,7 @@ public ResponseEntity delete(@PathVariable Long id) { reservationTimeService.deleteTime(id); return ResponseEntity.ok().build(); } - + //예약 가능시간 조회 @GetMapping("/available") public ResponseEntity> availableTime(@RequestParam String date, diff --git a/src/main/java/roomescape/controller/ThemeController.java b/src/main/java/roomescape/controller/ThemeController.java index c298adda..00b64f0b 100644 --- a/src/main/java/roomescape/controller/ThemeController.java +++ b/src/main/java/roomescape/controller/ThemeController.java @@ -23,6 +23,12 @@ public ThemeController(ThemeService themeService) { this.themeService = themeService; } + /** + * 테마 관리 페이지의 테마 + * 순서 제목 설명 썸네일URL + * + * @return + */ @GetMapping public ResponseEntity> findThemes() { return ResponseEntity.ok().body(themeService.findThemes()); diff --git a/src/main/java/roomescape/domain/ReservationTime.java b/src/main/java/roomescape/domain/ReservationTime.java index bcfba35f..3247c449 100644 --- a/src/main/java/roomescape/domain/ReservationTime.java +++ b/src/main/java/roomescape/domain/ReservationTime.java @@ -4,6 +4,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; + import java.time.LocalTime; public class ReservationTime { diff --git a/src/main/java/roomescape/domain/Theme.java b/src/main/java/roomescape/domain/Theme.java index 34032510..d5cf19ae 100644 --- a/src/main/java/roomescape/domain/Theme.java +++ b/src/main/java/roomescape/domain/Theme.java @@ -1,6 +1,5 @@ package roomescape.domain; -import roomescape.dto.theme.create.ThemeCreateRequest; import roomescape.exception.ErrorCode; import roomescape.exception.custom.InvalidInputException; diff --git a/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java b/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java index fe2ebdd9..e2c8633a 100644 --- a/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java +++ b/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java @@ -12,6 +12,7 @@ public class ReservationCreateResponse { private LocalDate date; private String name; private ReservationTimeResponse time; + private ThemeResponse theme; public ReservationCreateResponse() { diff --git a/src/main/java/roomescape/exception/ErrorCodeResponse.java b/src/main/java/roomescape/exception/ErrorCodeResponse.java index 39d444c1..93e82315 100644 --- a/src/main/java/roomescape/exception/ErrorCodeResponse.java +++ b/src/main/java/roomescape/exception/ErrorCodeResponse.java @@ -10,9 +10,8 @@ public ErrorCodeResponse() { } public ErrorCodeResponse(ErrorCode errorCode, String message) { - this.status = errorCode.getStatus().value(); this.errorCode = errorCode; - this.message = message; + this.status = errorCode.getStatus().value(); } public int getStatus() { diff --git a/src/main/java/roomescape/service/ReservationTimeService.java b/src/main/java/roomescape/service/ReservationTimeService.java index 433fa004..7f4e32e3 100644 --- a/src/main/java/roomescape/service/ReservationTimeService.java +++ b/src/main/java/roomescape/service/ReservationTimeService.java @@ -30,6 +30,7 @@ public ReservationTimeService(ReservationRepository reservationRepository, this.reservationRepository = reservationRepository; this.reservationTimeRepository = reservationTimeRepository; this.themeRepository = themeRepository; + } public List findTimes() { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6a99295f..f5220184 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,4 +3,4 @@ spring.h2.console.path=/h2-console spring.datasource.url=jdbc:h2:mem:database security.jwt.token.secret-key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c -security.jwt.token.expire=3600000 \ No newline at end of file +security.jwt.token.expire=3600000 diff --git a/src/test/java/roomescape/controller/ReservationControllerTest.java b/src/test/java/roomescape/controller/ReservationControllerTest.java index 016f5520..053d6a9c 100644 --- a/src/test/java/roomescape/controller/ReservationControllerTest.java +++ b/src/test/java/roomescape/controller/ReservationControllerTest.java @@ -8,7 +8,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpStatus; import org.springframework.test.annotation.DirtiesContext; -import roomescape.domain.Reservation; import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.dto.theme.create.ThemeCreateRequest; import roomescape.dto.time.ReservationTimeRequest; diff --git a/src/test/java/roomescape/controller/ReservationTimeControllerTest.java b/src/test/java/roomescape/controller/ReservationTimeControllerTest.java index fc0349b4..49d32f09 100644 --- a/src/test/java/roomescape/controller/ReservationTimeControllerTest.java +++ b/src/test/java/roomescape/controller/ReservationTimeControllerTest.java @@ -9,14 +9,10 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpStatus; import org.springframework.test.annotation.DirtiesContext; -import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.dto.theme.create.ThemeCreateRequest; import roomescape.dto.time.ReservationTimeRequest; -import roomescape.service.ReservationService; import roomescape.service.ReservationTimeService; - import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) class ReservationTimeControllerTest { diff --git a/src/test/java/roomescape/controller/ThemeControllerTest.java b/src/test/java/roomescape/controller/ThemeControllerTest.java index 06e05646..c4b168a3 100644 --- a/src/test/java/roomescape/controller/ThemeControllerTest.java +++ b/src/test/java/roomescape/controller/ThemeControllerTest.java @@ -2,9 +2,6 @@ import io.restassured.RestAssured; import io.restassured.http.ContentType; -import io.restassured.response.ExtractableResponse; -import io.restassured.response.Response; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -12,9 +9,7 @@ import org.springframework.http.HttpStatus; import org.springframework.test.annotation.DirtiesContext; import roomescape.dto.theme.create.ThemeCreateRequest; - import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) class ThemeControllerTest { diff --git a/src/test/java/roomescape/service/ReservationServiceTest.java b/src/test/java/roomescape/service/ReservationServiceTest.java index ad3a307e..583caf37 100644 --- a/src/test/java/roomescape/service/ReservationServiceTest.java +++ b/src/test/java/roomescape/service/ReservationServiceTest.java @@ -6,7 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.Rollback; import org.springframework.transaction.annotation.Transactional; import roomescape.dto.reservation.ReservationsResponse; import roomescape.dto.reservation.create.ReservationCreateRequest; diff --git a/src/test/java/roomescape/service/ReservationTimeServiceTest.java b/src/test/java/roomescape/service/ReservationTimeServiceTest.java index 29340310..7bc9d818 100644 --- a/src/test/java/roomescape/service/ReservationTimeServiceTest.java +++ b/src/test/java/roomescape/service/ReservationTimeServiceTest.java @@ -8,8 +8,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.MethodArgumentNotValidException; -import roomescape.domain.ReservationTime; import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.dto.theme.create.ThemeCreateRequest; import roomescape.dto.theme.create.ThemeCreateResponse; diff --git a/src/test/java/roomescape/service/ThemeServiceTest.java b/src/test/java/roomescape/service/ThemeServiceTest.java index 10c05571..611ecfe3 100644 --- a/src/test/java/roomescape/service/ThemeServiceTest.java +++ b/src/test/java/roomescape/service/ThemeServiceTest.java @@ -45,6 +45,11 @@ void findThemes() { //then assertThat(themes).isNotEmpty(); assertThat(themes).hasSize(1); + assertThat(themes).allSatisfy((themeResponse -> { + assertThat(themes.get(0).getId()).isEqualTo(1); + assertThat(themes.get(0).getName()).isEqualTo("hello"); + assertThat(themes.get(0).getThumbnail()).isEqualTo("테마이미지"); + })); } @Test @@ -71,7 +76,6 @@ void delete() { //given ThemeCreateRequest request = new ThemeCreateRequest("hello", "첫번째 테마입니다.Hello 첫번째 테마입니다.Hello 첫번째 테마입니다.Hello", "테마이미지"); ThemeCreateResponse response = themeService.createTheme(request); - assertThat(response.getId()).isEqualTo(1L); //when @@ -85,6 +89,7 @@ void delete() { @Test @DisplayName("중복 테마 등록시 예외 발생") void checkDuplicatedThemeName() { + ThemeCreateRequest request = new ThemeCreateRequest("테마1", "첫번째 테마입니다.Hello 첫번째 테마입니다.Hello 첫번째 테마입니다.Hello", "테마이미지"); ThemeCreateResponse response = themeService.createTheme(request); Assertions.assertThrows(DuplicatedThemeNameException.class, () -> { From 52f045d3a7f57bb004bd447f72406816cf288333 Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Mon, 15 Jul 2024 18:40:34 +0900 Subject: [PATCH 2/9] =?UTF-8?q?feat=20:=202-2=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/templates/admin/reservation.html | 4 ++-- src/main/resources/templates/reservation.html | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/resources/templates/admin/reservation.html b/src/main/resources/templates/admin/reservation.html index f9c751fa..196599f4 100644 --- a/src/main/resources/templates/admin/reservation.html +++ b/src/main/resources/templates/admin/reservation.html @@ -79,7 +79,7 @@

방탈출 예약 페이지

TODO: [2단계] 예약 생성 기능 변경 - 관리자 reservation.js 파일 대신 reservation-with-member.js 파일 사용 --> - - + + diff --git a/src/main/resources/templates/reservation.html b/src/main/resources/templates/reservation.html index 9b8e1619..5e89d409 100644 --- a/src/main/resources/templates/reservation.html +++ b/src/main/resources/templates/reservation.html @@ -80,11 +80,6 @@

시간 선택

TODO: [2단계] 예약 생성 기능 변경 - 사용자 적용 후 아래에 있는 User Name Input 삭제 --> - -
- 예약자명 - -
From 18134388cdb1d4f4a991639ace36dbecdc45b810 Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Wed, 17 Jul 2024 23:03:19 +0900 Subject: [PATCH 3/9] =?UTF-8?q?2-2=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20HandlerMethodArgumentRes?= =?UTF-8?q?olver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LoginArgumentResolver.java | 40 +++++++++++++++++++ .../argumentresolver/LoginUser.java | 11 +++++ .../java/roomescape/config/WebConfig.java | 24 +++++++++++ .../roomescape/controller/AuthController.java | 25 +++++------- src/main/java/roomescape/domain/Role.java | 5 +++ src/main/java/roomescape/domain/User.java | 20 ++++++++++ .../java/roomescape/service/AuthService.java | 9 ++++- src/main/resources/data.sql | 13 +++++- .../roomescape/service/AuthServiceTest.java | 3 +- 9 files changed, 130 insertions(+), 20 deletions(-) create mode 100644 src/main/java/roomescape/argumentresolver/LoginArgumentResolver.java create mode 100644 src/main/java/roomescape/argumentresolver/LoginUser.java create mode 100644 src/main/java/roomescape/config/WebConfig.java create mode 100644 src/main/java/roomescape/domain/Role.java diff --git a/src/main/java/roomescape/argumentresolver/LoginArgumentResolver.java b/src/main/java/roomescape/argumentresolver/LoginArgumentResolver.java new file mode 100644 index 00000000..9f5f1689 --- /dev/null +++ b/src/main/java/roomescape/argumentresolver/LoginArgumentResolver.java @@ -0,0 +1,40 @@ +package roomescape.argumentresolver; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.core.MethodParameter; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.ModelAndViewContainer; +import roomescape.dto.auth.AuthCheckResponse; +import roomescape.exception.ErrorCode; +import roomescape.exception.custom.AuthorizationException; +import roomescape.jwt.JwtTokenProvider; +import roomescape.service.AuthService; +import roomescape.util.CookieUtil; + +import java.util.Optional; + + +public class LoginArgumentResolver implements HandlerMethodArgumentResolver { + + private final AuthService authService; + public LoginArgumentResolver(AuthService authService) { + this.authService = authService; + } + + @Override + public boolean supportsParameter(MethodParameter parameter) { + return parameter.hasParameterAnnotation(LoginUser.class); + } + + @Override + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { + + HttpServletRequest request = (HttpServletRequest)webRequest.getNativeRequest(); + String token = CookieUtil.extractTokenFromCookie(request.getCookies()) + .orElseThrow(() -> new AuthorizationException(ErrorCode.UNAUTHORIZED_USER, "로그인한 유저만 이용가능합니다.")); + return authService.findUserFromToken(token); + } +} diff --git a/src/main/java/roomescape/argumentresolver/LoginUser.java b/src/main/java/roomescape/argumentresolver/LoginUser.java new file mode 100644 index 00000000..cebdc74b --- /dev/null +++ b/src/main/java/roomescape/argumentresolver/LoginUser.java @@ -0,0 +1,11 @@ +package roomescape.argumentresolver; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +public @interface LoginUser { +} diff --git a/src/main/java/roomescape/config/WebConfig.java b/src/main/java/roomescape/config/WebConfig.java new file mode 100644 index 00000000..c152c53f --- /dev/null +++ b/src/main/java/roomescape/config/WebConfig.java @@ -0,0 +1,24 @@ +package roomescape.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import roomescape.argumentresolver.LoginArgumentResolver; +import roomescape.jwt.JwtTokenProvider; +import roomescape.service.AuthService; + +import java.util.List; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + private final AuthService authService; + + public WebConfig(JwtTokenProvider jwtTokenProvider, AuthService authService) { + this.authService = authService; + } + + @Override + public void addArgumentResolvers(List resolvers) { + resolvers.add(new LoginArgumentResolver(authService)); + } +} diff --git a/src/main/java/roomescape/controller/AuthController.java b/src/main/java/roomescape/controller/AuthController.java index a750f953..bf4a93be 100644 --- a/src/main/java/roomescape/controller/AuthController.java +++ b/src/main/java/roomescape/controller/AuthController.java @@ -7,6 +7,8 @@ import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import roomescape.argumentresolver.LoginUser; +import roomescape.domain.User; import roomescape.dto.auth.AuthCheckResponse; import roomescape.dto.auth.AuthLoginRequest; import roomescape.exception.ErrorCode; @@ -32,7 +34,6 @@ public AuthController(AuthService authService) { public ResponseEntity authLogin(@RequestBody AuthLoginRequest request, HttpServletResponse response) { String token = authService.authLogin(request); - System.out.println(token); Cookie cookie = CookieUtil.createCookie(token); response.addCookie(cookie); return ResponseEntity.ok().build(); @@ -40,24 +41,16 @@ public ResponseEntity authLogin(@RequestBody AuthLoginRequest request, } @GetMapping("/login/check") - public ResponseEntity checkLogin(HttpServletRequest request) { - Cookie[] cookies = request.getCookies(); - String accessToken = CookieUtil.extractTokenFromCookie(cookies) - .orElseThrow(() -> new AuthorizationException(ErrorCode.UNAUTHORIZED_USER, "다시 로그인 해주세요.")); - AuthCheckResponse userResponse = authService.findUserFromToken(accessToken); - return ResponseEntity.ok(userResponse); + public ResponseEntity checkLogin(@LoginUser User user) { + return ResponseEntity.ok(authService.checkUser(user)); } @PostMapping("/logout") - public ResponseEntity logout(HttpServletRequest request, HttpServletResponse response) { - Cookie findCookie = Arrays.stream(request.getCookies()) - .filter(cookie -> cookie.getName().equals("token")) - .findFirst() - .orElseThrow(()-> new CookieNotFoundException(ErrorCode.COOKIE_NOT_FOUND, "로그인이 되어 있지 않습니다.")); - - findCookie.setMaxAge(0); - response.addCookie(findCookie); - return ResponseEntity.ok(findCookie); + public ResponseEntity logout(@LoginUser User user, HttpServletResponse response) { + Cookie cookie = CookieUtil.createCookie(""); + cookie.setMaxAge(0); + response.addCookie(cookie); + return ResponseEntity.ok().build(); } } diff --git a/src/main/java/roomescape/domain/Role.java b/src/main/java/roomescape/domain/Role.java new file mode 100644 index 00000000..7e747cd0 --- /dev/null +++ b/src/main/java/roomescape/domain/Role.java @@ -0,0 +1,5 @@ +package roomescape.domain; + +public enum Role { + USER, ADMIN +} diff --git a/src/main/java/roomescape/domain/User.java b/src/main/java/roomescape/domain/User.java index e9773f93..b0211b9a 100644 --- a/src/main/java/roomescape/domain/User.java +++ b/src/main/java/roomescape/domain/User.java @@ -3,12 +3,15 @@ import roomescape.exception.ErrorCode; import roomescape.exception.custom.AuthorizationException; +import java.util.Optional; + public class User { private Long id; private String name; private String email; private String password; + private Role role; public void checkPassword(String password) { if (!this.password.equals(password)) { @@ -16,12 +19,26 @@ public void checkPassword(String password) { } } + public void checkUser(User user) { + if(Optional.ofNullable(user).isEmpty()){ + throw new AuthorizationException(ErrorCode.UNAUTHORIZED_USER, "다시 로그인해주세요."); + } + } + public User(String name, String email, String password) { this.name = name; this.email = email; this.password = password; } + public User(String name, String email, String password, Role role) { + this.name = name; + this.email = email; + this.password = password; + this.role = role; + } + + public Long getId() { return id; } @@ -38,4 +55,7 @@ public String getPassword() { return password; } + public Role getRole() { + return role; + } } diff --git a/src/main/java/roomescape/service/AuthService.java b/src/main/java/roomescape/service/AuthService.java index 2219b733..b64ed24e 100644 --- a/src/main/java/roomescape/service/AuthService.java +++ b/src/main/java/roomescape/service/AuthService.java @@ -4,11 +4,11 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import roomescape.domain.Role; import roomescape.domain.User; import roomescape.dto.auth.AuthCheckResponse; import roomescape.dto.auth.AuthLoginRequest; import roomescape.exception.ErrorCode; -import roomescape.exception.custom.AuthorizationException; import roomescape.exception.custom.UserNotFoundException; import roomescape.jwt.JwtTokenProvider; import roomescape.repository.AuthRepository; @@ -34,10 +34,15 @@ public String authLogin(AuthLoginRequest request) { } - public AuthCheckResponse findUserFromToken(String accessToken) { + public User findUserFromToken(String accessToken) { String email = jwtTokenProvider.getEmailByToken(accessToken); User user = authRepository.findUserByEmail(email) .orElseThrow(() -> new UserNotFoundException(ErrorCode.USER_NOT_FOUND, "유저가 존재하지 않습니다.")); + return new User(user.getName(), user.getEmail(), user.getPassword(), Role.USER); + } + + public AuthCheckResponse checkUser(User user) { + user.checkUser(user); return new AuthCheckResponse(user.getName()); } } \ No newline at end of file diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 6fe0d0c7..0056a094 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1 +1,12 @@ -INSERT INTO users(name, email, password) values('modric', 'modric@gmail.com', 'asdf1234'); \ No newline at end of file +INSERT INTO users(name, email, password) values('modric', 'modric@gmail.com', 'asdf1234'); + +INSERT INTO reservation_time(start_at) values('12:00'); +INSERT INTO reservation_time(start_at) values('13:00'); +INSERT INTO reservation_time(start_at) values('14:00'); + +INSERT INTO theme(name, description, thumbnail) values('THE HOLE', + '강원도 깊은 산골에서 캠핑을 하던중 사라진 일행을 찾기 위해 주변을 찾아다니다가 움푹 꺼진 땅굴을 발견하고 혹시나 하는 마음에 동굴로 들어간 사람들….순간 번쩍이는 불빛에 기절하고 마는데..불빛이 사라지고 겨우 정신을 차리고 눈을 떠 주위를 살펴보니 여기는 동굴이 아니다....', + '???????????') + +--INSERT INTO reservation(name, date, time_id, theme_id) values('modric', '2024-12-31', 1, 1); + diff --git a/src/test/java/roomescape/service/AuthServiceTest.java b/src/test/java/roomescape/service/AuthServiceTest.java index 79c37de1..e31476ba 100644 --- a/src/test/java/roomescape/service/AuthServiceTest.java +++ b/src/test/java/roomescape/service/AuthServiceTest.java @@ -8,6 +8,7 @@ import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.test.annotation.DirtiesContext; import org.springframework.transaction.annotation.Transactional; +import roomescape.domain.User; import roomescape.dto.auth.AuthCheckResponse; import roomescape.dto.auth.AuthLoginRequest; import roomescape.exception.custom.AuthorizationException; @@ -39,7 +40,7 @@ void login() { String email = "modric@gmail.com"; String token = authService.authLogin(new AuthLoginRequest(email, "asdf1234")); String findEmail = jwtTokenProvider.getEmailByToken(token); - AuthCheckResponse response = authService.findUserFromToken(token); + User response = authService.findUserFromToken(token); org.junit.jupiter.api.Assertions.assertEquals(email, findEmail, "똑같은 이메일"); assertThat(token).isNotEmpty(); } From ad85cdd4c769b63d468cef1e903acd7df2ccfbd6 Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Wed, 17 Jul 2024 23:05:34 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=ED=85=8C=EB=A7=88=20=EB=8D=94=EB=AF=B8=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=B6=94=EA=B0=80=EB=A1=9C=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ThemeControllerTest.java | 2 +- .../roomescape/service/ThemeServiceTest.java | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/test/java/roomescape/controller/ThemeControllerTest.java b/src/test/java/roomescape/controller/ThemeControllerTest.java index c4b168a3..0f0bdcd3 100644 --- a/src/test/java/roomescape/controller/ThemeControllerTest.java +++ b/src/test/java/roomescape/controller/ThemeControllerTest.java @@ -59,7 +59,7 @@ void delete() { .given().log().all() // .body() .contentType(ContentType.JSON) - .when().delete("themes/1") + .when().delete("themes/2") .then().log().all().extract(); assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); diff --git a/src/test/java/roomescape/service/ThemeServiceTest.java b/src/test/java/roomescape/service/ThemeServiceTest.java index 611ecfe3..3b3116a0 100644 --- a/src/test/java/roomescape/service/ThemeServiceTest.java +++ b/src/test/java/roomescape/service/ThemeServiceTest.java @@ -44,11 +44,11 @@ void findThemes() { //then assertThat(themes).isNotEmpty(); - assertThat(themes).hasSize(1); + assertThat(themes).hasSize(2); assertThat(themes).allSatisfy((themeResponse -> { - assertThat(themes.get(0).getId()).isEqualTo(1); - assertThat(themes.get(0).getName()).isEqualTo("hello"); - assertThat(themes.get(0).getThumbnail()).isEqualTo("테마이미지"); + assertThat(themes.get(1).getId()).isEqualTo(2); + assertThat(themes.get(1).getName()).isEqualTo("hello"); + assertThat(themes.get(1).getThumbnail()).isEqualTo("테마이미지"); })); } @@ -64,7 +64,7 @@ void create() { ThemeCreateResponse response = themeService.createTheme(request); //then - assertThat(response.getId()).isEqualTo(1L); + assertThat(response.getId()).isEqualTo(2L); assertThat(response.getName()).isEqualTo("hello"); assertThat(response.getDescription()).isEqualTo(request.getDescription()); assertThat(response.getThumbnail()).isEqualTo(request.getThumbnail()); @@ -76,14 +76,13 @@ void delete() { //given ThemeCreateRequest request = new ThemeCreateRequest("hello", "첫번째 테마입니다.Hello 첫번째 테마입니다.Hello 첫번째 테마입니다.Hello", "테마이미지"); ThemeCreateResponse response = themeService.createTheme(request); - assertThat(response.getId()).isEqualTo(1L); + assertThat(response.getId()).isEqualTo(2L); //when - themeService.deleteTheme(1L); + themeService.deleteTheme(2L); //then - assertThat(themeService.findThemes()).hasSize(0); - assertThat(themeService.findThemes()).isEmpty(); + assertThat(themeService.findThemes()).hasSize(1); } @Test From 81f969944baf98d1c1a1c0676367068f7d09577d Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Wed, 17 Jul 2024 23:13:07 +0900 Subject: [PATCH 5/9] =?UTF-8?q?=EC=98=88=EC=95=BD=EC=8B=9C=EA=B0=84=20?= =?UTF-8?q?=EB=8D=94=EB=AF=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EB=A1=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReservationTimeControllerTest.java | 4 ++-- .../service/ReservationTimeServiceTest.java | 24 ++++++++----------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/test/java/roomescape/controller/ReservationTimeControllerTest.java b/src/test/java/roomescape/controller/ReservationTimeControllerTest.java index 49d32f09..0ea4d66b 100644 --- a/src/test/java/roomescape/controller/ReservationTimeControllerTest.java +++ b/src/test/java/roomescape/controller/ReservationTimeControllerTest.java @@ -22,14 +22,14 @@ class ReservationTimeControllerTest { @BeforeEach void init() { - reservationTimeService.createTime(new ReservationTimeRequest("12:00")); + //reservationTimeService.createTime(new ReservationTimeRequest("12:00")); } @Test void create() { var response = RestAssured .given().log().all() - .body(new ReservationTimeRequest("13:00")) + .body(new ReservationTimeRequest("19:00")) .contentType(ContentType.JSON) .when().post("/times") .then().log().all().extract(); diff --git a/src/test/java/roomescape/service/ReservationTimeServiceTest.java b/src/test/java/roomescape/service/ReservationTimeServiceTest.java index 7bc9d818..ad0e8254 100644 --- a/src/test/java/roomescape/service/ReservationTimeServiceTest.java +++ b/src/test/java/roomescape/service/ReservationTimeServiceTest.java @@ -40,8 +40,6 @@ class ReservationTimeServiceTest { @BeforeEach void init() { - reservationTimeService.createTime(new ReservationTimeRequest("12:00")); - reservationTimeService.createTime(new ReservationTimeRequest("13:00")); } @@ -52,18 +50,18 @@ void findTimes() { .map(t -> t.getStartAt().toString()) .collect(Collectors.toList()); - assertThat(collect.size()).isEqualTo(2); - assertThat(collect).contains("12:00", "13:00"); + //더미 데이터 3개 + assertThat(collect.size()).isEqualTo(3); } @Test @DisplayName("예약 시간 추가") void createTime() { //given - ReservationTimeRequest createTime = new ReservationTimeRequest("14:00"); + ReservationTimeRequest createTime = new ReservationTimeRequest("16:00"); ReservationTimeRequest createTime2 = new ReservationTimeRequest("15:00"); - //when @BeforeEach init 메서드는 주석 처리 + //기존의 더미 데이터 3개 //현재 두개의 예약시간만 등록한다는 가정 ReservationTimeCreateResponse time = reservationTimeService.createTime(createTime); ReservationTimeCreateResponse time2 = reservationTimeService.createTime(createTime2); @@ -71,8 +69,8 @@ void createTime() { //then List list = reservationTimeService.findTimes(); - assertThat(list.size()).isEqualTo(4); - assertThat(list.get(2).getStartAt()).isEqualTo(createTime.getStartAt()); + assertThat(list.size()).isEqualTo(5); + assertThat(list.get(3).getStartAt()).isEqualTo(createTime.getStartAt()); } @Test @@ -81,8 +79,7 @@ void deleteTime() { reservationTimeService.deleteTime(1L); List times = reservationTimeService.findTimes(); - assertThat(times.size()).isEqualTo(1); - assertThat(times).isNotEqualTo(2); + assertThat(times.size()).isEqualTo(2); } @Test @@ -108,7 +105,7 @@ void findAvailableReservationTime() { reservationTimeService.findAvailableReservationTime(date, theme.getId()); //then - assertThat(response.size()).isEqualTo(2); + assertThat(response.size()).isEqualTo(3); assertThat(response.get(0).getStartAt()).isEqualTo("12:00"); } @@ -118,15 +115,14 @@ void failAvailableReservationTime() { //given String date = "2024-09-24"; Long themeId = 1L; - themeService.createTheme(new ThemeCreateRequest("무서운 이야기", - "너무 무서움ㄷㄷ너무 무서움ㄷㄷ너무 무서움ㄷㄷ너무 무서움ㄷㄷ너무 무서움ㄷㄷ너무 무서움ㄷㄷ", "GOOD")); reservationService.createReservation(new ReservationCreateRequest(date, "brown", 1L, 1L)); reservationService.createReservation(new ReservationCreateRequest(date, "brown", 2L, 1L)); + reservationService.createReservation(new ReservationCreateRequest(date, "brown", 3L, 1L)); //이미 예약이 차서 가능한 시간대 미존재 //when List response = - reservationTimeService.findAvailableReservationTime(date, 1L); + reservationTimeService.findAvailableReservationTime(date, themeId); //then assertThat(response.size()).isEqualTo(0); From 7df7c1cf95f43fd52f196c6b0a28caf0f871ef5e Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Wed, 17 Jul 2024 23:16:28 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roomescape/controller/ReservationControllerTest.java | 3 --- .../roomescape/controller/ReservationTimeControllerTest.java | 5 ----- src/test/java/roomescape/controller/ThemeControllerTest.java | 1 - src/test/java/roomescape/service/ReservationServiceTest.java | 2 -- .../java/roomescape/service/ReservationTimeServiceTest.java | 5 ----- src/test/java/roomescape/service/ThemeServiceTest.java | 5 ----- 6 files changed, 21 deletions(-) diff --git a/src/test/java/roomescape/controller/ReservationControllerTest.java b/src/test/java/roomescape/controller/ReservationControllerTest.java index 053d6a9c..7ea98ed7 100644 --- a/src/test/java/roomescape/controller/ReservationControllerTest.java +++ b/src/test/java/roomescape/controller/ReservationControllerTest.java @@ -30,9 +30,6 @@ class ReservationControllerTest { @BeforeEach void init() { - themeService.createTheme(new ThemeCreateRequest("엄청 무서운 이야기", - "설명은 없습니다.설명은 없습니다.설명은 없습니다.설명은 없습니다.설명은 없습니다.설명은 없습니다.", "https://gg")); - reservationTimeService.createTime(new ReservationTimeRequest("12:00")); reservationService.createReservation(new ReservationCreateRequest("2024-07-23", "brown", 1L, 1L)); } diff --git a/src/test/java/roomescape/controller/ReservationTimeControllerTest.java b/src/test/java/roomescape/controller/ReservationTimeControllerTest.java index 0ea4d66b..b75903e7 100644 --- a/src/test/java/roomescape/controller/ReservationTimeControllerTest.java +++ b/src/test/java/roomescape/controller/ReservationTimeControllerTest.java @@ -20,11 +20,6 @@ class ReservationTimeControllerTest { @Autowired ReservationTimeService reservationTimeService; - @BeforeEach - void init() { - //reservationTimeService.createTime(new ReservationTimeRequest("12:00")); - } - @Test void create() { var response = RestAssured diff --git a/src/test/java/roomescape/controller/ThemeControllerTest.java b/src/test/java/roomescape/controller/ThemeControllerTest.java index 0f0bdcd3..204ea2c1 100644 --- a/src/test/java/roomescape/controller/ThemeControllerTest.java +++ b/src/test/java/roomescape/controller/ThemeControllerTest.java @@ -17,7 +17,6 @@ class ThemeControllerTest { @Autowired ThemeController themeController; - @BeforeEach void init() { themeController.createTheme(new ThemeCreateRequest( diff --git a/src/test/java/roomescape/service/ReservationServiceTest.java b/src/test/java/roomescape/service/ReservationServiceTest.java index 583caf37..25f7c21d 100644 --- a/src/test/java/roomescape/service/ReservationServiceTest.java +++ b/src/test/java/roomescape/service/ReservationServiceTest.java @@ -31,8 +31,6 @@ class ReservationServiceTest { @BeforeEach void init() { - reservationTimeService.createTime(new ReservationTimeRequest("12:00")); - themeService.createTheme(new ThemeCreateRequest("테마1", "테마1의 설명은 비밀입니다.테마1의 설명은 비밀입니다.테마1의 설명은 비밀입니다.", "http://")); reservationService.createReservation(new ReservationCreateRequest("2024-08-22", "hello", 1L, 1L)); } diff --git a/src/test/java/roomescape/service/ReservationTimeServiceTest.java b/src/test/java/roomescape/service/ReservationTimeServiceTest.java index ad0e8254..424ad92e 100644 --- a/src/test/java/roomescape/service/ReservationTimeServiceTest.java +++ b/src/test/java/roomescape/service/ReservationTimeServiceTest.java @@ -38,11 +38,6 @@ class ReservationTimeServiceTest { @Autowired ReservationService reservationService; - @BeforeEach - void init() { - } - - @Test @DisplayName("예약 시간 리스트 테스트") void findTimes() { diff --git a/src/test/java/roomescape/service/ThemeServiceTest.java b/src/test/java/roomescape/service/ThemeServiceTest.java index 3b3116a0..9dac10c7 100644 --- a/src/test/java/roomescape/service/ThemeServiceTest.java +++ b/src/test/java/roomescape/service/ThemeServiceTest.java @@ -26,11 +26,6 @@ class ThemeServiceTest { @Autowired ThemeService themeService; - @BeforeEach - void init() { - // themeService.createTheme(new ThemeCreateRequest("테마1", "테마1의 설명은 비밀입니다.", "http://")); - } - @Test @DisplayName("테마의 리스트 테스트") void findThemes() { From 192cb01b55d6eae940f45e7eca65225b36682421 Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Fri, 19 Jul 2024 01:01:28 +0900 Subject: [PATCH 7/9] =?UTF-8?q?2-2=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81=20=EC=98=88=EC=95=BD=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?-=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/roomescape/controller/ReservationController.java | 6 +++++- .../dto/reservation/create/ReservationCreateRequest.java | 7 ++++++- src/main/resources/static/js/reservation-with-member.js | 2 +- src/main/resources/static/js/user-reservation.js | 4 ++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java index 2b277133..c4fac314 100644 --- a/src/main/java/roomescape/controller/ReservationController.java +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -4,6 +4,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import roomescape.argumentresolver.LoginUser; +import roomescape.domain.User; import roomescape.dto.reservation.ReservationsResponse; import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.dto.reservation.create.ReservationCreateResponse; @@ -30,7 +32,9 @@ public ResponseEntity> findReservations() { } @PostMapping - public ResponseEntity create(@Valid @RequestBody ReservationCreateRequest dto) { + public ResponseEntity create(@Valid @RequestBody ReservationCreateRequest dto, + @LoginUser User user) { + dto.addName(user.getName()); ReservationCreateResponse reservation = reservationService.createReservation(dto); return ResponseEntity.ok().body(reservation); } diff --git a/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java b/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java index 9b9289d7..113add92 100644 --- a/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java +++ b/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java @@ -1,12 +1,13 @@ package roomescape.dto.reservation.create; import jakarta.validation.constraints.*; +import roomescape.domain.User; public class ReservationCreateRequest { @NotBlank(message = "날짜를 제대로 입력해주세요.") private String date; - @NotBlank(message = "예약자는 필수항목입니다.") +// @NotBlank(message = "예약자는 필수항목입니다.") private String name; @NotNull(message = "시간을 선택해주세요.") @@ -15,6 +16,10 @@ public class ReservationCreateRequest { @NotNull(message = "테마을 선택해주세요.") private Long themeId; + public void addName(String userName) { + this.name = userName; + } + public ReservationCreateRequest() { } diff --git a/src/main/resources/static/js/reservation-with-member.js b/src/main/resources/static/js/reservation-with-member.js index a989bb35..9f8280bf 100644 --- a/src/main/resources/static/js/reservation-with-member.js +++ b/src/main/resources/static/js/reservation-with-member.js @@ -27,7 +27,7 @@ function render(data) { const row = tableBody.insertRow(); row.insertCell(0).textContent = item.id; // 예약 id - row.insertCell(1).textContent = item.member.name; // 사용자 name + row.insertCell(1).textContent = item.name; // 사용자 name row.insertCell(2).textContent = item.theme.name; // 테마 name row.insertCell(3).textContent = item.date; // date row.insertCell(4).textContent = item.time.startAt; // 예약 시간 startAt diff --git a/src/main/resources/static/js/user-reservation.js b/src/main/resources/static/js/user-reservation.js index 001469cb..e9f5ced7 100644 --- a/src/main/resources/static/js/user-reservation.js +++ b/src/main/resources/static/js/user-reservation.js @@ -153,14 +153,14 @@ function onReservationButtonClick() { const selectedDate = document.getElementById("datepicker").value; const selectedThemeId = document.querySelector('.theme-slot.active')?.getAttribute('data-theme-id'); const selectedTimeId = document.querySelector('.time-slot.active')?.getAttribute('data-time-id'); - const name = document.getElementById('user-name').value; +// const name = document.getElementById('user-name').value; if (selectedDate && selectedThemeId && selectedTimeId) { const reservationData = { date: selectedDate, themeId: selectedThemeId, timeId: selectedTimeId, - name: name +// name: name }; fetch('/reservations', { From a457e02c04b94a293455969d7c63521b11122a31 Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Sun, 21 Jul 2024 00:23:03 +0900 Subject: [PATCH 8/9] =?UTF-8?q?feat=20:=20=EC=A0=84=EC=B2=B4=EC=A0=81?= =?UTF-8?q?=EC=9D=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ReservationController.java | 28 +++++++++++---- .../java/roomescape/domain/Reservation.java | 9 ++--- src/main/java/roomescape/domain/User.java | 9 +++-- .../roomescape/dto/auth/AuthUserResponse.java | 34 +++++++++++++++++++ .../dto/reservation/ReservationsResponse.java | 2 ++ .../create/ReservationCreateRequest.java | 7 ++-- .../create/ReservationCreateResponse.java | 5 +-- .../java/roomescape/exception/ErrorCode.java | 1 + .../repository/ReservationRepository.java | 1 + .../repository/ReservationRepositoryImpl.java | 3 +- .../java/roomescape/service/AuthService.java | 5 ++- .../service/ReservationService.java | 19 +++++++---- src/main/resources/data.sql | 1 + src/main/resources/schema.sql | 19 ++++++----- 14 files changed, 103 insertions(+), 40 deletions(-) create mode 100644 src/main/java/roomescape/dto/auth/AuthUserResponse.java diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java index c4fac314..5d0aa657 100644 --- a/src/main/java/roomescape/controller/ReservationController.java +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -5,18 +5,19 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import roomescape.argumentresolver.LoginUser; +import roomescape.domain.Role; import roomescape.domain.User; import roomescape.dto.reservation.ReservationsResponse; import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.dto.reservation.create.ReservationCreateResponse; -import roomescape.exception.ErrorCodeResponse; +import roomescape.exception.ErrorCode; +import roomescape.exception.custom.AuthorizationException; import roomescape.service.ReservationService; -import java.net.URI; import java.util.List; @RestController -@RequestMapping("/reservations") +@RequestMapping public class ReservationController { private final ReservationService reservationService; @@ -25,23 +26,36 @@ public ReservationController(ReservationService reservationService) { this.reservationService = reservationService; } - @GetMapping + @GetMapping("/reservations") public ResponseEntity> findReservations() { List list = reservationService.findReservations(); return ResponseEntity.ok().body(list); } - @PostMapping + @PostMapping("/reservations") public ResponseEntity create(@Valid @RequestBody ReservationCreateRequest dto, @LoginUser User user) { - dto.addName(user.getName()); + dto.addUserName(user.getName()); ReservationCreateResponse reservation = reservationService.createReservation(dto); return ResponseEntity.ok().body(reservation); } - @DeleteMapping("/{id}") + @DeleteMapping("/reservations/{id}") public ResponseEntity delete(@PathVariable Long id) { reservationService.deleteReservation(id); return ResponseEntity.ok().build(); } + + @PostMapping("/admin/reservations") + public ResponseEntity createAdminReservation( + @Valid @RequestBody ReservationCreateRequest requestDto, + @LoginUser User user){ + + if(user.getRole() != Role.ADMIN){ + throw new AuthorizationException(ErrorCode.UNAUTHORIZED_ADMIN, "관리자 권한이 필요합니다."); + } + + ReservationCreateResponse reservation = reservationService.createReservation(requestDto); + return ResponseEntity.status(HttpStatus.CREATED).body(reservation); + } } diff --git a/src/main/java/roomescape/domain/Reservation.java b/src/main/java/roomescape/domain/Reservation.java index 1b36b79e..133442f0 100644 --- a/src/main/java/roomescape/domain/Reservation.java +++ b/src/main/java/roomescape/domain/Reservation.java @@ -9,21 +9,22 @@ public class Reservation { private LocalDate date; private ReservationTime time; private Theme theme; +// private User user; public Reservation() { } - public Reservation(Long id, String name, String date, Long timeId, String startAt, Long themeId, String themeName) { + public Reservation(Long id, String userName, String date, Long timeId, String startAt, Long themeId, String themeName) { this.id = id; - this.name = name; + this.name = userName; this.date = LocalDate.parse(date); this.time = new ReservationTime(timeId, startAt); this.theme = new Theme(themeId, themeName); } - public Reservation(Long id, String name, String date, ReservationTime time, Theme theme) { + public Reservation(Long id, String userName, String date, ReservationTime time, Theme theme) { this.id = id; - this.name = name; + this.name = userName; this.date = LocalDate.parse(date); this.time = new ReservationTime(time.getId(), time.getStartAt().toString()); this.theme = new Theme(theme.getId(), theme.getName(), theme.getDescription(), theme.getThumbnail()); diff --git a/src/main/java/roomescape/domain/User.java b/src/main/java/roomescape/domain/User.java index b0211b9a..dd39c257 100644 --- a/src/main/java/roomescape/domain/User.java +++ b/src/main/java/roomescape/domain/User.java @@ -1,5 +1,6 @@ package roomescape.domain; +import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.exception.ErrorCode; import roomescape.exception.custom.AuthorizationException; @@ -19,12 +20,16 @@ public void checkPassword(String password) { } } - public void checkUser(User user) { - if(Optional.ofNullable(user).isEmpty()){ + public void checkUser() { + if(Optional.ofNullable(this).isEmpty()) { throw new AuthorizationException(ErrorCode.UNAUTHORIZED_USER, "다시 로그인해주세요."); } } + public User(Long id) { + this.id = id; + } + public User(String name, String email, String password) { this.name = name; this.email = email; diff --git a/src/main/java/roomescape/dto/auth/AuthUserResponse.java b/src/main/java/roomescape/dto/auth/AuthUserResponse.java new file mode 100644 index 00000000..788cc77b --- /dev/null +++ b/src/main/java/roomescape/dto/auth/AuthUserResponse.java @@ -0,0 +1,34 @@ +package roomescape.dto.auth; + +import roomescape.domain.Role; + +public class AuthUserResponse { + + private String name; + private String email; + private String password; + private Role role; + + public AuthUserResponse(String name, String email, String password, Role role) { + this.name = name; + this.email = email; + this.password = password; + this.role = role; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public String getPassword() { + return password; + } + + public Role getRole() { + return role; + } +} diff --git a/src/main/java/roomescape/dto/reservation/ReservationsResponse.java b/src/main/java/roomescape/dto/reservation/ReservationsResponse.java index 43b22391..86304cc3 100644 --- a/src/main/java/roomescape/dto/reservation/ReservationsResponse.java +++ b/src/main/java/roomescape/dto/reservation/ReservationsResponse.java @@ -1,6 +1,8 @@ package roomescape.dto.reservation; import roomescape.domain.Reservation; +import roomescape.domain.User; +import roomescape.dto.auth.AuthUserResponse; import roomescape.dto.theme.ThemeResponse; import roomescape.dto.time.ReservationTimeResponse; diff --git a/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java b/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java index 113add92..f7ffbe59 100644 --- a/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java +++ b/src/main/java/roomescape/dto/reservation/create/ReservationCreateRequest.java @@ -7,17 +7,14 @@ public class ReservationCreateRequest { @NotBlank(message = "날짜를 제대로 입력해주세요.") private String date; -// @NotBlank(message = "예약자는 필수항목입니다.") private String name; - @NotNull(message = "시간을 선택해주세요.") private Long timeId; - @NotNull(message = "테마을 선택해주세요.") private Long themeId; - public void addName(String userName) { - this.name = userName; + public void addUserName(String name) { + this.name = name; } public ReservationCreateRequest() { diff --git a/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java b/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java index e2c8633a..2d3b2a58 100644 --- a/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java +++ b/src/main/java/roomescape/dto/reservation/create/ReservationCreateResponse.java @@ -1,6 +1,7 @@ package roomescape.dto.reservation.create; import roomescape.domain.Reservation; +import roomescape.domain.User; import roomescape.dto.theme.ThemeResponse; import roomescape.dto.time.ReservationTimeResponse; @@ -18,10 +19,10 @@ public class ReservationCreateResponse { public ReservationCreateResponse() { } - public ReservationCreateResponse(Long id, LocalDate date, String name, ReservationTimeResponse time, ThemeResponse theme) { + public ReservationCreateResponse(Long id, LocalDate date, String userName, ReservationTimeResponse time, ThemeResponse theme) { this.id = id; this.date = date; - this.name = name; + this.name = userName; this.time = time; this.theme = theme; } diff --git a/src/main/java/roomescape/exception/ErrorCode.java b/src/main/java/roomescape/exception/ErrorCode.java index d7edba8d..5c80c40d 100644 --- a/src/main/java/roomescape/exception/ErrorCode.java +++ b/src/main/java/roomescape/exception/ErrorCode.java @@ -11,6 +11,7 @@ public enum ErrorCode { USER_NOT_FOUND(HttpStatus.NOT_FOUND), UNAUTHORIZED_USER(HttpStatus.UNAUTHORIZED), + UNAUTHORIZED_ADMIN(HttpStatus.UNAUTHORIZED), INVALID_THEME_NAME(HttpStatus.BAD_REQUEST), diff --git a/src/main/java/roomescape/repository/ReservationRepository.java b/src/main/java/roomescape/repository/ReservationRepository.java index d96f1454..18b7e010 100644 --- a/src/main/java/roomescape/repository/ReservationRepository.java +++ b/src/main/java/roomescape/repository/ReservationRepository.java @@ -3,6 +3,7 @@ import roomescape.domain.Reservation; import roomescape.domain.ReservationTime; import roomescape.domain.Theme; +import roomescape.domain.User; import roomescape.dto.reservation.create.ReservationCreateRequest; import java.util.List; diff --git a/src/main/java/roomescape/repository/ReservationRepositoryImpl.java b/src/main/java/roomescape/repository/ReservationRepositoryImpl.java index e702be14..ec79ba7e 100644 --- a/src/main/java/roomescape/repository/ReservationRepositoryImpl.java +++ b/src/main/java/roomescape/repository/ReservationRepositoryImpl.java @@ -7,6 +7,7 @@ import roomescape.domain.Reservation; import roomescape.domain.ReservationTime; import roomescape.domain.Theme; +import roomescape.domain.User; import roomescape.dto.reservation.create.ReservationCreateRequest; import java.sql.PreparedStatement; @@ -45,7 +46,7 @@ public List findReservations() { @Override public Reservation createReservation(ReservationCreateRequest dto, ReservationTime time, Theme theme) { - String sql = "insert into reservation(name, date, time_id, theme_Id) values(?,?,?,?)"; + String sql = "insert into reservation(name, date, time_id, theme_id) values(?,?,?,?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(con -> { PreparedStatement ps = con.prepareStatement( diff --git a/src/main/java/roomescape/service/AuthService.java b/src/main/java/roomescape/service/AuthService.java index b64ed24e..ff1f2915 100644 --- a/src/main/java/roomescape/service/AuthService.java +++ b/src/main/java/roomescape/service/AuthService.java @@ -4,7 +4,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import roomescape.domain.Role; import roomescape.domain.User; import roomescape.dto.auth.AuthCheckResponse; import roomescape.dto.auth.AuthLoginRequest; @@ -38,11 +37,11 @@ public User findUserFromToken(String accessToken) { String email = jwtTokenProvider.getEmailByToken(accessToken); User user = authRepository.findUserByEmail(email) .orElseThrow(() -> new UserNotFoundException(ErrorCode.USER_NOT_FOUND, "유저가 존재하지 않습니다.")); - return new User(user.getName(), user.getEmail(), user.getPassword(), Role.USER); + return new User(user.getName(), user.getEmail(), user.getPassword(), user.getRole()); } public AuthCheckResponse checkUser(User user) { - user.checkUser(user); + user.checkUser(); return new AuthCheckResponse(user.getName()); } } \ No newline at end of file diff --git a/src/main/java/roomescape/service/ReservationService.java b/src/main/java/roomescape/service/ReservationService.java index 0af1f3f3..a7626dc7 100644 --- a/src/main/java/roomescape/service/ReservationService.java +++ b/src/main/java/roomescape/service/ReservationService.java @@ -1,24 +1,21 @@ package roomescape.service; import org.springframework.stereotype.Service; -import roomescape.domain.Reservation; -import roomescape.domain.ReservationTime; -import roomescape.domain.Theme; +import roomescape.domain.*; import roomescape.dto.reservation.ReservationsResponse; import roomescape.dto.reservation.create.ReservationCreateRequest; import roomescape.dto.reservation.create.ReservationCreateResponse; -import roomescape.dto.time.ReservationTimeResponse; import roomescape.exception.ErrorCode; import roomescape.exception.custom.InvalidReservationTimeException; import roomescape.exception.custom.ThemeNotFoundException; +import roomescape.exception.custom.UserNotFoundException; +import roomescape.repository.AuthRepository; import roomescape.repository.ReservationRepository; import roomescape.repository.ReservationTimeRepository; import roomescape.repository.ThemeRepository; import java.time.LocalDate; -import java.time.LocalTime; import java.util.List; -import java.util.stream.Collectors; @Service public class ReservationService { @@ -26,12 +23,14 @@ public class ReservationService { private final ReservationRepository reservationRepository; private final ReservationTimeRepository reservationTimeRepository; private final ThemeRepository themeRepository; + private final AuthRepository authRepository; public ReservationService(ReservationRepository reservationRepository, ReservationTimeRepository reservationTimeRepository, - ThemeRepository themeRepository) { + ThemeRepository themeRepository, AuthRepository authRepository) { this.reservationRepository = reservationRepository; this.reservationTimeRepository = reservationTimeRepository; this.themeRepository = themeRepository; + this.authRepository = authRepository; } public List findReservations() { @@ -46,6 +45,12 @@ public ReservationCreateResponse createReservation(ReservationCreateRequest requ ReservationTime time = reservationTimeRepository.findReservationTimeById(request.getTimeId()); Theme theme = themeRepository.findThemeById(request.getThemeId()) .orElseThrow(() -> new ThemeNotFoundException(ErrorCode.THEME_NOT_FOUND, "해당 테마가 존재하지 않습니다.")); + +// if(user.getRole() == Role.ADMIN) { +// User user = authRepository.findById(request.getUserId()) +// .orElseThrow(() -> new UserNotFoundException(ErrorCode.USER_NOT_FOUND, "해당 회원이 존재하지 않습니다.")); +// } + Reservation reservation = reservationRepository.createReservation(request, time, theme); return ReservationCreateResponse.fromEntity(reservation); } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 0056a094..59166215 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,4 +1,5 @@ INSERT INTO users(name, email, password) values('modric', 'modric@gmail.com', 'asdf1234'); +INSERT INTO users(name, email, password, role) values('toni', 'toni@gmail.com', 'asdf1234', 'ADMIN'); INSERT INTO reservation_time(start_at) values('12:00'); INSERT INTO reservation_time(start_at) values('13:00'); diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 626e4730..917c1c51 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -1,3 +1,13 @@ +CREATE TABLE users +( + id BIGINT NOT NULL AUTO_INCREMENT, + name VARCHAR(21) NOT NULL, + email VARCHAR(40) NOT NULL, + password VARCHAR(16) NOT NULL, + role varchar(5) NOT NULL default 'USER', + PRIMARY KEY (id) +); + CREATE TABLE reservation_time ( id BIGINT NOT NULL AUTO_INCREMENT, @@ -25,12 +35,3 @@ CREATE TABLE reservation FOREIGN KEY (time_id) REFERENCES reservation_time (id), FOREIGN KEY (theme_id) REFERENCES theme (id) -- 외래키 추가 ); - -CREATE TABLE users -( - id BIGINT NOT NULL AUTO_INCREMENT, - name VARCHAR(21) NOT NULL, - email VARCHAR(40) NOT NULL, - password VARCHAR(16) NOT NULL, - PRIMARY KEY (id) -); \ No newline at end of file From 20f10d231d73ae4fe3fc0c52ab9b7ec9d9addd5f Mon Sep 17 00:00:00 2001 From: dyrlqhffo Date: Sun, 21 Jul 2024 01:02:07 +0900 Subject: [PATCH 9/9] =?UTF-8?q?feat=20:=202-2=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81=20=EC=98=88?= =?UTF-8?q?=EC=95=BD=20=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20-=20=EA=B4=80=EB=A6=AC=EC=9E=90=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roomescape/controller/AuthController.java | 8 +++++ src/main/java/roomescape/domain/User.java | 6 ++++ .../roomescape/dto/auth/AuthUserResponse.java | 2 +- .../roomescape/dto/auth/UserResponse.java | 34 +++++++++++++++++++ .../roomescape/repository/AuthRepository.java | 3 ++ .../repository/AuthRepositoryImpl.java | 17 ++++++++++ .../java/roomescape/service/AuthService.java | 10 ++++++ .../static/js/reservation-with-member.js | 4 +-- 8 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/main/java/roomescape/dto/auth/UserResponse.java diff --git a/src/main/java/roomescape/controller/AuthController.java b/src/main/java/roomescape/controller/AuthController.java index bf4a93be..7e00dec3 100644 --- a/src/main/java/roomescape/controller/AuthController.java +++ b/src/main/java/roomescape/controller/AuthController.java @@ -11,6 +11,8 @@ import roomescape.domain.User; import roomescape.dto.auth.AuthCheckResponse; import roomescape.dto.auth.AuthLoginRequest; +import roomescape.dto.auth.AuthUserResponse; +import roomescape.dto.auth.UserResponse; import roomescape.exception.ErrorCode; import roomescape.exception.custom.AuthorizationException; import roomescape.exception.custom.CookieNotFoundException; @@ -18,6 +20,7 @@ import roomescape.util.CookieUtil; import java.util.Arrays; +import java.util.List; import java.util.Optional; @RestController @@ -30,6 +33,11 @@ public AuthController(AuthService authService) { this.authService = authService; } + @GetMapping("/users") + public ResponseEntity> findUsers() { + return ResponseEntity.ok().body(authService.findUsers()); + } + @PostMapping("/login") public ResponseEntity authLogin(@RequestBody AuthLoginRequest request, HttpServletResponse response) { diff --git a/src/main/java/roomescape/domain/User.java b/src/main/java/roomescape/domain/User.java index dd39c257..d739cb5a 100644 --- a/src/main/java/roomescape/domain/User.java +++ b/src/main/java/roomescape/domain/User.java @@ -43,6 +43,12 @@ public User(String name, String email, String password, Role role) { this.role = role; } + public User(long id, String name, String email, Role role) { + this.id = id; + this.name = name; + this.email = email; + this.role = role; + } public Long getId() { return id; diff --git a/src/main/java/roomescape/dto/auth/AuthUserResponse.java b/src/main/java/roomescape/dto/auth/AuthUserResponse.java index 788cc77b..a08647b6 100644 --- a/src/main/java/roomescape/dto/auth/AuthUserResponse.java +++ b/src/main/java/roomescape/dto/auth/AuthUserResponse.java @@ -1,9 +1,9 @@ package roomescape.dto.auth; import roomescape.domain.Role; +import roomescape.domain.User; public class AuthUserResponse { - private String name; private String email; private String password; diff --git a/src/main/java/roomescape/dto/auth/UserResponse.java b/src/main/java/roomescape/dto/auth/UserResponse.java new file mode 100644 index 00000000..15ebaf1d --- /dev/null +++ b/src/main/java/roomescape/dto/auth/UserResponse.java @@ -0,0 +1,34 @@ +package roomescape.dto.auth; + +import roomescape.domain.Role; +import roomescape.domain.User; + +public class UserResponse { + private Long id; + private String name; + private String email; + private Role role; + + public UserResponse(User user) { + this.id = user.getId(); + this.name = user.getName(); + this.email = user.getEmail(); + this.role = user.getRole(); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public Role getRole() { + return role; + } +} diff --git a/src/main/java/roomescape/repository/AuthRepository.java b/src/main/java/roomescape/repository/AuthRepository.java index 21735b94..4500f31d 100644 --- a/src/main/java/roomescape/repository/AuthRepository.java +++ b/src/main/java/roomescape/repository/AuthRepository.java @@ -2,6 +2,7 @@ import roomescape.domain.User; +import java.util.List; import java.util.Optional; public interface AuthRepository { @@ -9,4 +10,6 @@ public interface AuthRepository { Optional findUserByEmail(String email); Optional findById(Long userId); + + List findUsers(); } diff --git a/src/main/java/roomescape/repository/AuthRepositoryImpl.java b/src/main/java/roomescape/repository/AuthRepositoryImpl.java index d6e66a4b..69f15f36 100644 --- a/src/main/java/roomescape/repository/AuthRepositoryImpl.java +++ b/src/main/java/roomescape/repository/AuthRepositoryImpl.java @@ -2,8 +2,10 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; +import roomescape.domain.Role; import roomescape.domain.User; +import java.util.List; import java.util.Optional; @Repository public class AuthRepositoryImpl implements AuthRepository{ @@ -36,4 +38,19 @@ public Optional findById(Long userId) { rs.getString("password") ), userId)); } + + @Override + public List findUsers() { + String sql = "select id, name, email, role from users"; + return jdbcTemplate.query( + sql, (rs, rowNum) -> { + User user = new User( + rs.getLong("id"), + rs.getString("name"), + rs.getString("email"), + Role.valueOf(rs.getString("role")) + ); + return user; + }); + } } diff --git a/src/main/java/roomescape/service/AuthService.java b/src/main/java/roomescape/service/AuthService.java index ff1f2915..e08a9b31 100644 --- a/src/main/java/roomescape/service/AuthService.java +++ b/src/main/java/roomescape/service/AuthService.java @@ -7,11 +7,15 @@ import roomescape.domain.User; import roomescape.dto.auth.AuthCheckResponse; import roomescape.dto.auth.AuthLoginRequest; +import roomescape.dto.auth.AuthUserResponse; +import roomescape.dto.auth.UserResponse; import roomescape.exception.ErrorCode; import roomescape.exception.custom.UserNotFoundException; import roomescape.jwt.JwtTokenProvider; import roomescape.repository.AuthRepository; +import java.util.List; + @Service @Transactional public class AuthService { @@ -44,4 +48,10 @@ public AuthCheckResponse checkUser(User user) { user.checkUser(); return new AuthCheckResponse(user.getName()); } + + public List findUsers() { + return authRepository.findUsers().stream() + .map(UserResponse::new) + .toList(); + } } \ No newline at end of file diff --git a/src/main/resources/static/js/reservation-with-member.js b/src/main/resources/static/js/reservation-with-member.js index 9f8280bf..246377ab 100644 --- a/src/main/resources/static/js/reservation-with-member.js +++ b/src/main/resources/static/js/reservation-with-member.js @@ -2,7 +2,7 @@ let isEditing = false; const RESERVATION_API_ENDPOINT = '/reservations'; const TIME_API_ENDPOINT = '/times'; const THEME_API_ENDPOINT = '/themes'; -const MEMBER_API_ENDPOINT = '/members'; +const MEMBER_API_ENDPOINT = '/users'; const timesOptions = []; const themesOptions = []; const membersOptions = []; @@ -58,7 +58,7 @@ function fetchMembers() { requestRead(MEMBER_API_ENDPOINT) .then(data => { membersOptions.push(...data); - populateSelect('member', membersOptions, 'name'); + populateSelect('user', membersOptions, 'name'); }) .catch(error => console.error('Error fetching member:', error)); }