Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] 맛집 상세정보, 맛집 평가 생성 API 수정 #20

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateReviewResponseDto> createReview(@PathVariable Long storeId, @RequestBody CreateReviewRequestDto requestDto) {
CreateReviewResponseDto responseDto = reviewService.createReview(storeId, requestDto);
public ResponseEntity<CreateReviewResponseDto> 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);
}
}
5 changes: 5 additions & 0 deletions src/main/java/wanted/ribbon/store/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import lombok.*;
import wanted.ribbon.user.domain.User;

@Entity
@Getter
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -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) {
}
13 changes: 11 additions & 2 deletions src/main/java/wanted/ribbon/store/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public StoreDetailResponseDto getStoreDetail(Long storeId) {
.orElseThrow(() -> new NotFoundException(ErrorCode.STORE_NOT_FOUND));
List<Review> reviewList = reviewRepository.findByStore_StoreId(storeId);
List<ReviewListResponseDto> 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(
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/wanted/ribbon/user/config/TokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down