Skip to content

Commit

Permalink
feat: 리뷰 삭제 API 추가 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdohyung committed Mar 25, 2024
1 parent 2cd83ca commit 534ed1e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/main/java/com/jisungin/api/review/ReviewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import com.jisungin.application.review.response.ReviewResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/v1/reviews")
@RequiredArgsConstructor
Expand All @@ -21,10 +18,16 @@ public class ReviewController {
private final ReviewService reviewService;

@PostMapping
public ApiResponse<ReviewResponse> createReview(
@Valid @RequestBody ReviewCreateRequest request,
@Auth AuthContext authContext) {
public ApiResponse<ReviewResponse> createReview(@Valid @RequestBody ReviewCreateRequest request,
@Auth AuthContext authContext) {
return ApiResponse.ok(reviewService.createReview(request.toServiceRequest(), authContext.getUserId()));
}

@DeleteMapping("/{reviewId}")
public ApiResponse<Void> deleteReview(@PathVariable Long reviewId,
@Auth AuthContext authContext) {
reviewService.deleteReview(reviewId, authContext.getUserId());
return ApiResponse.ok(null);
}

}
15 changes: 15 additions & 0 deletions src/main/java/com/jisungin/application/review/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ public ReviewResponse createReview(ReviewCreateServiceRequest request, Long user
return ReviewResponse.of(savedReview.getBook(), savedReview.getContent(), savedReview.getRating());
}

@Transactional
public void deleteReview(Long reviewId, Long userId) {
Review deleteReview = reviewRepository.findById(reviewId)
.orElseThrow(() -> new BusinessException(REVIEW_NOT_FOUND));

User reviewUser = deleteReview.getUser();
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(USER_NOT_FOUND));

if (!user.isSame(reviewUser.getId())) {
throw new BusinessException(UNAUTHORIZED_REQUEST);
}
reviewRepository.delete(deleteReview);
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/jisungin/domain/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ public boolean isMe(Long userId) {
return this.id.equals(userId);
}

public boolean isSame(Long userId) {
return this.id.equals(userId);
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/jisungin/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public enum ErrorCode {
OAUTH_TYPE_NOT_FOUND(404, "지원하지 않는 소셜 로그인입니다."),
TALK_ROOM_NOT_FOUND(400, "토크방을 찾을 수 없습니다."),
UNAUTHORIZED_REQUEST(400, "권한이 없는 사용자입니다."),
COMMENT_NOT_FOUND(404, "의견을 찾을 수 없습니다.");
COMMENT_NOT_FOUND(404, "의견을 찾을 수 없습니다."),
REVIEW_NOT_FOUND(404, "리뷰를 찾을 수 없습니다.");


private final int code;
Expand Down

0 comments on commit 534ed1e

Please sign in to comment.