-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] 도서 리뷰 구현, 회원가입 시 랜덤 닉네임 등록 구현 #34
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/main/java/com/moongeul/backend/api/book/controller/ReviewController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package com.moongeul.backend.api.book.controller; | ||
|
|
||
| import com.moongeul.backend.api.book.dto.ReviewRequestDTO; | ||
| import com.moongeul.backend.api.book.dto.ReviewResponseDTO; | ||
| import com.moongeul.backend.api.book.service.ReviewService; | ||
| import com.moongeul.backend.common.response.ApiResponse; | ||
| import com.moongeul.backend.common.response.SuccessStatus; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.Min; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.security.core.userdetails.UserDetails; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @Tag(name = "Book Review", description = "책 리뷰 관련 API 입니다.") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/v2/book/review") | ||
| @Validated | ||
| public class ReviewController { | ||
|
|
||
| private final ReviewService reviewService; | ||
|
|
||
| @Operation( | ||
| summary = "리뷰 작성 API", | ||
| description = "도서에 대한 리뷰를 작성하는 API 입니다. 한 사용자는 한 도서에 대해 하나의 리뷰만 작성할 수 있습니다." | ||
| ) | ||
| @ApiResponses({ | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "리뷰 작성 성공"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "이미 리뷰를 작성한 도서입니다."), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 도서를 찾을 수 없습니다.") | ||
| }) | ||
| @PostMapping("/{isbn}") | ||
| public ResponseEntity<ApiResponse<Void>> createReview( | ||
| @AuthenticationPrincipal UserDetails userDetails, | ||
| @PathVariable @NotBlank(message = "ISBN은 필수입니다.") String isbn, | ||
| @Valid @RequestBody ReviewRequestDTO reviewRequestDTO) { | ||
|
|
||
| reviewService.createReview(isbn, reviewRequestDTO, userDetails.getUsername()); | ||
| return ApiResponse.success_only(SuccessStatus.CREATE_BOOK_REVIEW_SUCCESS); | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "리뷰 조회 API", | ||
| description = "도서에 대한 리뷰를 최신순으로 조회하는 API 입니다. 전체 리뷰 평균 평점도 함께 반환됩니다." | ||
| ) | ||
| @ApiResponses({ | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "리뷰 조회 성공"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 도서를 찾을 수 없습니다.") | ||
| }) | ||
| @GetMapping("/{isbn}") | ||
| public ResponseEntity<ApiResponse<ReviewResponseDTO>> getReviews( | ||
| @PathVariable @NotBlank(message = "ISBN은 필수입니다.") String isbn, | ||
| @RequestParam(required = false, defaultValue = "1") @Min(value = 1, message = "페이지는 1 이상이어야 합니다.") Integer page, | ||
| @RequestParam(required = false, defaultValue = "10") @Min(value = 1, message = "한 페이지당 개수는 1 이상이어야 합니다.") Integer size) { | ||
|
|
||
| ReviewResponseDTO response = reviewService.getReviews(isbn, page, size); | ||
| return ApiResponse.success(SuccessStatus.GET_BOOK_REVIEWS_SUCCESS, response); | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "리뷰 수정 API", | ||
| description = "작성한 리뷰를 수정하는 API 입니다." | ||
| ) | ||
| @ApiResponses({ | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "리뷰 수정 성공"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 리뷰를 찾을 수 없습니다.") | ||
| }) | ||
| @PutMapping("/{isbn}") | ||
| public ResponseEntity<ApiResponse<Void>> updateReview( | ||
| @AuthenticationPrincipal UserDetails userDetails, | ||
| @PathVariable @NotBlank(message = "ISBN은 필수입니다.") String isbn, | ||
| @Valid @RequestBody ReviewRequestDTO reviewRequestDTO) { | ||
|
|
||
| reviewService.updateReview(isbn, reviewRequestDTO, userDetails.getUsername()); | ||
| return ApiResponse.success_only(SuccessStatus.UPDATE_BOOK_REVIEW_SUCCESS); | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "리뷰 삭제 API", | ||
| description = "작성한 리뷰를 삭제하는 API 입니다." | ||
| ) | ||
| @ApiResponses({ | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "리뷰 삭제 성공"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 리뷰를 찾을 수 없습니다.") | ||
| }) | ||
| @DeleteMapping("/{isbn}") | ||
| public ResponseEntity<ApiResponse<Void>> deleteReview( | ||
| @AuthenticationPrincipal UserDetails userDetails, | ||
| @PathVariable @NotBlank(message = "ISBN은 필수입니다.") String isbn) { | ||
|
|
||
| reviewService.deleteReview(isbn, userDetails.getUsername()); | ||
| return ApiResponse.success_only(SuccessStatus.DELETE_BOOK_REVIEW_SUCCESS); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/moongeul/backend/api/book/dto/ReviewItemDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.moongeul.backend.api.book.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReviewItemDTO { | ||
| private String nickname; // 사용자 닉네임 | ||
| private Integer rating; // 별점 | ||
| private String content; // 내용 | ||
| } | ||
|
|
24 changes: 24 additions & 0 deletions
24
src/main/java/com/moongeul/backend/api/book/dto/ReviewRequestDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.moongeul.backend.api.book.dto; | ||
|
|
||
| import jakarta.validation.constraints.Max; | ||
| import jakarta.validation.constraints.Min; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReviewRequestDTO { | ||
|
|
||
| @NotNull(message = "평점은 필수입니다") | ||
| @Min(value = 1, message = "평점은 1 이상이어야 합니다") | ||
| @Max(value = 5, message = "평점은 5 이하이어야 합니다") | ||
| private Integer rating; // 평점 (1-5) | ||
|
|
||
| private String content; // 리뷰 내용 | ||
| } | ||
|
|
23 changes: 23 additions & 0 deletions
23
src/main/java/com/moongeul/backend/api/book/dto/ReviewResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.moongeul.backend.api.book.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReviewResponseDTO { | ||
| private Double reviewAverageRating; // 전체 리뷰 평균 평점 | ||
| private Long total; // 전체 리뷰 개수 | ||
| private Integer page; // 현재 페이지 | ||
| private Integer size; // 페이지당 개수 | ||
| private Integer totalPages; // 전체 페이지 수 | ||
| private Boolean isLast; // 마지막 페이지 여부 | ||
| private List<ReviewItemDTO> data; // 리뷰 목록 | ||
| } | ||
|
|
46 changes: 46 additions & 0 deletions
46
src/main/java/com/moongeul/backend/api/book/entity/Review.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.moongeul.backend.api.book.entity; | ||
|
|
||
| import com.moongeul.backend.api.member.entity.Member; | ||
| import com.moongeul.backend.common.entity.BaseTimeEntity; | ||
| import jakarta.persistence.*; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Table(name = "review", uniqueConstraints = { | ||
| @UniqueConstraint(columnNames = {"member_id", "book_isbn"}) | ||
| }) | ||
| public class Review extends BaseTimeEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "review_id") | ||
| private Long reviewId; | ||
|
|
||
| @Column(name = "rating", nullable = false) | ||
| private Integer rating; // 평점 (1-5) | ||
|
|
||
| @Column(name = "content", columnDefinition = "TEXT") | ||
| private String content; // 리뷰 내용 | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "member_id", nullable = false) | ||
| private Member member; // 회원 | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "book_isbn", nullable = false) | ||
| private Book book; // 책 | ||
|
|
||
| // 리뷰 수정 | ||
| public void update(Integer rating, String content) { | ||
| this.rating = rating; | ||
| this.content = content; | ||
| } | ||
| } | ||
|
|
30 changes: 30 additions & 0 deletions
30
src/main/java/com/moongeul/backend/api/book/repository/ReviewRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.moongeul.backend.api.book.repository; | ||
|
|
||
| import com.moongeul.backend.api.book.entity.Book; | ||
| import com.moongeul.backend.api.book.entity.Review; | ||
| import com.moongeul.backend.api.member.entity.Member; | ||
| 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 java.util.Optional; | ||
|
|
||
| public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
|
|
||
| // 회원과 책으로 리뷰 조회 (중복 체크용) | ||
| Optional<Review> findByMemberAndBook(Member member, Book book); | ||
|
|
||
| // 책의 리뷰를 최신순으로 조회 | ||
| @Query("SELECT r FROM Review r WHERE r.book = :book ORDER BY r.createdAt DESC") | ||
| Page<Review> findByBookOrderByCreatedAtDesc(@Param("book") Book book, Pageable pageable); | ||
|
|
||
| // 책의 리뷰 평균 평점 계산 | ||
| @Query("SELECT AVG(r.rating) FROM Review r WHERE r.book = :book") | ||
| Double calculateAverageRating(@Param("book") Book book); | ||
|
|
||
| // 책의 리뷰 개수 | ||
| Long countByBook(Book book); | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.