From d27a4d527e152d5b91b2ec260c6babd2b926b0cd Mon Sep 17 00:00:00 2001 From: LeeJiWon Date: Mon, 2 Sep 2024 21:35:52 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=A7=9B=EC=A7=91=20=EC=83=81?= =?UTF-8?q?=EC=84=B8=EC=A0=95=EB=B3=B4,=20=EB=A7=9B=EC=A7=91=20=ED=8F=89?= =?UTF-8?q?=EA=B0=80=20=EC=83=9D=EC=84=B1=20API=20=EC=88=98=EC=A0=95=20-?= =?UTF-8?q?=20=EB=A7=9B=EC=A7=91=20=EC=83=81=EC=84=B8=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=ED=8F=89=EA=B0=80=20?= =?UTF-8?q?=EC=83=81=EC=84=B8=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=EC=97=90=20?= =?UTF-8?q?=ED=8F=89=EA=B0=80=20=EC=9E=91=EC=84=B1=ED=95=9C=20=ED=9A=8C?= =?UTF-8?q?=EC=9B=90=20id=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80=20-=20?= =?UTF-8?q?=EB=A7=9B=EC=A7=91=20=ED=8F=89=EA=B0=80=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?API=EC=97=90=20=ED=9A=8C=EC=9B=90=20=EC=97=94=ED=8B=B0=ED=8B=B0?= =?UTF-8?q?=20=EC=97=B0=EA=B2=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ribbon/store/controller/ReviewController.java | 10 ++++++++-- .../java/wanted/ribbon/store/domain/Review.java | 5 +++++ .../ribbon/store/dto/CreateReviewResponseDto.java | 2 +- .../ribbon/store/dto/ReviewListResponseDto.java | 2 +- .../wanted/ribbon/store/service/ReviewService.java | 13 +++++++++++-- .../wanted/ribbon/store/service/StoreService.java | 2 +- .../wanted/ribbon/user/config/TokenProvider.java | 6 +++--- .../ribbon/user/config/WebSecurityConfig.java | 2 +- 8 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/main/java/wanted/ribbon/store/controller/ReviewController.java b/src/main/java/wanted/ribbon/store/controller/ReviewController.java index 4518618..b9bce35 100644 --- a/src/main/java/wanted/ribbon/store/controller/ReviewController.java +++ b/src/main/java/wanted/ribbon/store/controller/ReviewController.java @@ -7,16 +7,22 @@ import wanted.ribbon.store.dto.CreateReviewRequestDto; import wanted.ribbon.store.dto.CreateReviewResponseDto; import wanted.ribbon.store.service.ReviewService; +import wanted.ribbon.user.config.TokenProvider; + +import java.util.UUID; @RestController @RequestMapping("/api/reviews") @RequiredArgsConstructor public class ReviewController { private final ReviewService reviewService; + private final TokenProvider tokenProvider; @PostMapping("/{storeId}") - public ResponseEntity createReview(@PathVariable Long storeId, @RequestBody CreateReviewRequestDto requestDto) { - CreateReviewResponseDto responseDto = reviewService.createReview(storeId, requestDto); + public ResponseEntity createReview(@PathVariable Long storeId, @RequestHeader(value = "Authorization")String token, @RequestBody CreateReviewRequestDto requestDto) { + String accessToken = token.split("Bearer ")[1]; + String id = tokenProvider.getId(accessToken); // 액세스토큰으로 id 반환 + CreateReviewResponseDto responseDto = reviewService.createReview(storeId, id, requestDto); return ResponseEntity.status(HttpStatus.CREATED).body(responseDto); } } diff --git a/src/main/java/wanted/ribbon/store/domain/Review.java b/src/main/java/wanted/ribbon/store/domain/Review.java index af8313e..e131a93 100644 --- a/src/main/java/wanted/ribbon/store/domain/Review.java +++ b/src/main/java/wanted/ribbon/store/domain/Review.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; import lombok.*; +import wanted.ribbon.user.domain.User; @Entity @Getter @@ -24,4 +25,8 @@ public class Review { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "store_id", nullable = false) private Store store; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private User user; } diff --git a/src/main/java/wanted/ribbon/store/dto/CreateReviewResponseDto.java b/src/main/java/wanted/ribbon/store/dto/CreateReviewResponseDto.java index 6dd01df..8949949 100644 --- a/src/main/java/wanted/ribbon/store/dto/CreateReviewResponseDto.java +++ b/src/main/java/wanted/ribbon/store/dto/CreateReviewResponseDto.java @@ -1,4 +1,4 @@ package wanted.ribbon.store.dto; -public record CreateReviewResponseDto(int score, String content) { +public record CreateReviewResponseDto(String id, int score, String content) { } diff --git a/src/main/java/wanted/ribbon/store/dto/ReviewListResponseDto.java b/src/main/java/wanted/ribbon/store/dto/ReviewListResponseDto.java index 4e90906..d227ec5 100644 --- a/src/main/java/wanted/ribbon/store/dto/ReviewListResponseDto.java +++ b/src/main/java/wanted/ribbon/store/dto/ReviewListResponseDto.java @@ -1,4 +1,4 @@ package wanted.ribbon.store.dto; -public record ReviewListResponseDto(int score, String content) { +public record ReviewListResponseDto(String id, int score, String content) { } diff --git a/src/main/java/wanted/ribbon/store/service/ReviewService.java b/src/main/java/wanted/ribbon/store/service/ReviewService.java index 1ea54e7..93641c0 100644 --- a/src/main/java/wanted/ribbon/store/service/ReviewService.java +++ b/src/main/java/wanted/ribbon/store/service/ReviewService.java @@ -11,29 +11,38 @@ import wanted.ribbon.store.dto.CreateReviewResponseDto; import wanted.ribbon.store.repository.ReviewRepository; import wanted.ribbon.store.repository.StoreRepository; +import wanted.ribbon.user.domain.User; +import wanted.ribbon.user.repository.UserRepository; import java.util.List; +import java.util.UUID; @Service @RequiredArgsConstructor public class ReviewService { private final ReviewRepository reviewRepository; private final StoreRepository storeRepository; + private final UserRepository userRepository; @Transactional - public CreateReviewResponseDto createReview(Long storeId, CreateReviewRequestDto requestDto) { + public CreateReviewResponseDto createReview(Long storeId, String id, CreateReviewRequestDto requestDto) { Store store = storeRepository.findByStoreId(storeId) .orElseThrow(() -> new NotFoundException(ErrorCode.STORE_NOT_FOUND)); + User user = userRepository.findById(id) + .orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND)); + Review review = Review.builder() .score(requestDto.score()) .content(requestDto.content()) .store(store) + .user(user) .build(); reviewRepository.save(review); + // 점수 입력되면 해당 맛집의 평점 update updateRating(store); - CreateReviewResponseDto responseDto = new CreateReviewResponseDto(requestDto.score(), requestDto.content()); + CreateReviewResponseDto responseDto = new CreateReviewResponseDto(id, requestDto.score(), requestDto.content()); return responseDto; } diff --git a/src/main/java/wanted/ribbon/store/service/StoreService.java b/src/main/java/wanted/ribbon/store/service/StoreService.java index 537c6e8..161d967 100644 --- a/src/main/java/wanted/ribbon/store/service/StoreService.java +++ b/src/main/java/wanted/ribbon/store/service/StoreService.java @@ -26,7 +26,7 @@ public StoreDetailResponseDto getStoreDetail(Long storeId) { .orElseThrow(() -> new NotFoundException(ErrorCode.STORE_NOT_FOUND)); List reviewList = reviewRepository.findByStore_StoreId(storeId); List reviewListResponseDto = reviewList.stream() - .map(list -> new ReviewListResponseDto(list.getScore(), list.getContent())) + .map(list -> new ReviewListResponseDto(list.getUser().getId(), list.getScore(), list.getContent())) .collect(Collectors.toList()); StoreDetailResponseDto responseDto = new StoreDetailResponseDto( diff --git a/src/main/java/wanted/ribbon/user/config/TokenProvider.java b/src/main/java/wanted/ribbon/user/config/TokenProvider.java index 63491f6..1384f34 100644 --- a/src/main/java/wanted/ribbon/user/config/TokenProvider.java +++ b/src/main/java/wanted/ribbon/user/config/TokenProvider.java @@ -60,10 +60,10 @@ public Authentication getAuthentication(String token){ return new UsernamePasswordAuthenticationToken(new org.springframework.security.core.userdetails.User(claims.getSubject(), "", authorities), token, authorities); } - // 토큰 기반으로 유저 ID를 가져오는 메서드 - public UUID getUserId(String token){ + // 토큰 기반으로 회원의 ID를 가져오는 메서드 + public String getId(String token){ Claims claims = getClaims(token); - return claims.get("userId", UUID.class); + return claims.getSubject(); } private Claims getClaims(String token){ diff --git a/src/main/java/wanted/ribbon/user/config/WebSecurityConfig.java b/src/main/java/wanted/ribbon/user/config/WebSecurityConfig.java index 003df2c..0b848bb 100644 --- a/src/main/java/wanted/ribbon/user/config/WebSecurityConfig.java +++ b/src/main/java/wanted/ribbon/user/config/WebSecurityConfig.java @@ -25,7 +25,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .httpBasic(AbstractHttpConfigurer::disable) .csrf(AbstractHttpConfigurer::disable) .authorizeRequests(auth -> auth - .requestMatchers("/api/user/login", "/api/user/signup").permitAll() + .requestMatchers("/api/users/login", "/api/users/signup").permitAll() .anyRequest().authenticated()) .formLogin(AbstractHttpConfigurer::disable) .addFilterBefore(new TokenAuthenticationFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class);