-
Notifications
You must be signed in to change notification settings - Fork 1
[feat]칵테일 댓글 CRUD #124
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
[feat]칵테일 댓글 CRUD #124
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3c242d1
{feat} : domain
lkw9241 6cfd731
{fix}:Cocktail-Wishlist relation
lkw9241 20e9ee7
fix : enums
lkw9241 2df84ab
fix : bug
lkw9241 11994c5
feat : 조회기능, 조init data
lkw9241 fda27df
feat : cocktailSearch
lkw9241 5e93330
feat : controller
lkw9241 7a9c551
feat : cocktailcomment CRUD
lkw9241 25d0b21
feat : cocktailcomment controller
lkw9241 2e9898a
fix : bug
lkw9241 60a7ae5
fix : bug_cocktailcomment service
lkw9241 40e317a
fix : bug
lkw9241 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
107 changes: 107 additions & 0 deletions
107
src/main/java/com/back/domain/cocktail/comment/controller/CocktailCommentController.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,107 @@ | ||
| package com.back.domain.cocktail.comment.controller; | ||
|
|
||
| import com.back.domain.cocktail.comment.dto.CocktailCommentCreateRequestDto; | ||
| import com.back.domain.cocktail.comment.dto.CocktailCommentResponseDto; | ||
| import com.back.domain.cocktail.comment.dto.CocktailCommentUpdateRequestDto; | ||
| import com.back.domain.cocktail.comment.service.CocktailCommentService; | ||
| import com.back.global.rsData.RsData; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/cocktails/{cocktailId}/comments") | ||
| @Tag(name = "ApiCocktailCommentController", description = "API 칵테일댓글 컨트롤러") | ||
| @RequiredArgsConstructor | ||
| public class CocktailCommentController { | ||
|
|
||
| private final CocktailCommentService cocktailCommentService; | ||
|
|
||
| /** | ||
| * 칵테일댓글 작성 API | ||
| * | ||
| * @param cocktailId 칵테일댓글을 작성할 칵테일 ID | ||
| * @param reqBody 칵테일댓글 작성 요청 DTO | ||
| * @return 작성된 칵테일댓글 정보 | ||
| */ | ||
| @PostMapping | ||
| @Operation(summary = "칵테일댓글 작성") | ||
| public RsData<CocktailCommentResponseDto> createCocktailComment( | ||
| @PathVariable Long cocktailId, | ||
| @Valid @RequestBody CocktailCommentCreateRequestDto reqBody | ||
| ) { | ||
| return RsData.successOf(cocktailCommentService.createCocktailComment(cocktailId, reqBody)); // code=200, message="success" | ||
| } | ||
|
|
||
| /** | ||
| * 칵테일댓글 다건조회 API | ||
| * | ||
| * @param cocktailId 칵테일댓글 작성된 게시글 ID | ||
| * @param lastId 마지막으로 조회한 칵테일댓글 ID (페이징 처리용, optional) | ||
| * @return 칵테일댓글 목록 | ||
| */ | ||
| @GetMapping | ||
| @Operation(summary = "댓글 다건 조회") | ||
| public RsData<List<CocktailCommentResponseDto>> getCocktailComments( | ||
| @PathVariable Long cocktailId, | ||
| @RequestParam(required = false) Long lastId | ||
| ) { | ||
| return RsData.successOf(cocktailCommentService.getCocktailComments(cocktailId, lastId)); // code=200, message="success" | ||
| } | ||
|
|
||
| /** | ||
| * 칵테일댓글 단건 조회 API | ||
| * | ||
| * @param cocktailId 칵테일댓글이 작성된 칵테일 ID | ||
| * @param cocktailCommentId 조회할 칵테일댓글 ID | ||
| * @return 해당 ID의 칵테일댓글 정보 | ||
| */ | ||
| @GetMapping("/{cocktailCommentId}") | ||
| @Operation(summary = "칵테일 댓글 단건 조회") | ||
| public RsData<CocktailCommentResponseDto> getCocktailComment( | ||
| @PathVariable Long cocktailId, | ||
| @PathVariable Long cocktailCommentId | ||
| ) { | ||
| return RsData.successOf(cocktailCommentService.getCocktailComment(cocktailId, cocktailCommentId)); // code=200, message="success" | ||
| } | ||
|
|
||
| /** | ||
| * 칵테일댓글 수정 API | ||
| * | ||
| * @param cocktailId 칵테일댓글 작성된 칵테일 ID | ||
| * @param cocktailCommentId 수정할 칵테일댓글 ID | ||
| * @param reqBody 칵테일댓글 수정 요청 DTO | ||
| * @return 수정된 칵테일댓글 정보 | ||
| */ | ||
| @PatchMapping("/{cocktailCommentId}") | ||
| @Operation(summary = "칵테일댓글 수정") | ||
| public RsData<CocktailCommentResponseDto> updateComment( | ||
| @PathVariable Long cocktailId, | ||
| @PathVariable Long cocktailCommentId, | ||
| @Valid @RequestBody CocktailCommentUpdateRequestDto reqBody | ||
| ) { | ||
| return RsData.successOf(cocktailCommentService.updateCocktailComment(cocktailId, cocktailCommentId, reqBody)); // code=200, message="success" | ||
| } | ||
|
|
||
| /** | ||
| * 칵테일댓글 삭제 API | ||
| * | ||
| * @param cocktailId 칵테일댓글 작성된 칵테일 ID | ||
| * @param cocktailCommentId 삭제할 칵테일댓글 ID | ||
| * @return 삭제 성공 메시지 | ||
| */ | ||
| @DeleteMapping("/{cocktailCommentId}") | ||
| @Operation(summary = "댓글 삭제") | ||
| public RsData<Void> deleteComment( | ||
| @PathVariable Long cocktailId, | ||
| @PathVariable Long cocktailCommentId | ||
| ) { | ||
| cocktailCommentService.deleteCocktailComment(cocktailId, cocktailCommentId); | ||
| return RsData.successOf(null); // code=200, message="success" | ||
| } | ||
| } | ||
|
|
9 changes: 7 additions & 2 deletions
9
src/main/java/com/back/domain/cocktail/comment/repository/CocktailCommentRepository.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 |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| package com.back.domain.cocktail.comment.repository; | ||
|
|
||
| import com.back.domain.post.comment.entity.Comment; | ||
| import com.back.domain.cocktail.comment.entity.CocktailComment; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface CocktailCommentRepository extends JpaRepository<Comment, Long> { | ||
| import java.util.List; | ||
|
|
||
| public interface CocktailCommentRepository extends JpaRepository<CocktailComment, Long> { | ||
|
|
||
| List<CocktailComment> findTop10ByCocktailIdOrderByIdDesc(Long cocktailId); | ||
|
|
||
| List<CocktailComment> findTop10ByCocktailIdAndIdLessThanOrderByIdDesc(Long cocktailId, Long lastId); | ||
| } | ||
112 changes: 112 additions & 0 deletions
112
src/main/java/com/back/domain/cocktail/comment/service/CocktailCommentService.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,112 @@ | ||
| package com.back.domain.cocktail.comment.service; | ||
|
|
||
| import com.back.domain.cocktail.comment.dto.CocktailCommentCreateRequestDto; | ||
| import com.back.domain.cocktail.comment.dto.CocktailCommentResponseDto; | ||
| import com.back.domain.cocktail.comment.dto.CocktailCommentUpdateRequestDto; | ||
| import com.back.domain.cocktail.comment.entity.CocktailComment; | ||
| import com.back.domain.cocktail.comment.repository.CocktailCommentRepository; | ||
| import com.back.domain.cocktail.entity.Cocktail; | ||
| import com.back.domain.cocktail.repository.CocktailRepository; | ||
| import com.back.domain.post.comment.enums.CommentStatus; | ||
| import com.back.domain.user.entity.User; | ||
| import com.back.global.rq.Rq; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class CocktailCommentService { | ||
| private final CocktailCommentRepository cocktailCommentRepository; | ||
| private final CocktailRepository cocktailRepository; | ||
| private final Rq rq; | ||
|
|
||
| // 칵테일 댓글 작성 로직 | ||
| @Transactional | ||
| public CocktailCommentResponseDto createCocktailComment(Long cocktailId, CocktailCommentCreateRequestDto reqBody) { | ||
| User user = rq.getActor(); | ||
|
|
||
| Cocktail cocktail = cocktailRepository.findById(cocktailId) | ||
| .orElseThrow(() -> new IllegalArgumentException("칵테일이 존재하지 않습니다. id=" + cocktailId)); | ||
|
|
||
| CocktailComment cocktailComment = CocktailComment.builder() | ||
| .cocktail(cocktail) | ||
| .user(user) | ||
| .content(reqBody.content()) | ||
| .build(); | ||
|
|
||
| return new CocktailCommentResponseDto(cocktailCommentRepository.save(cocktailComment)); | ||
| } | ||
|
|
||
| // 칵테일 댓글 다건 조회 로직 (무한스크롤) | ||
| @Transactional(readOnly = true) | ||
| public List<CocktailCommentResponseDto> getCocktailComments(Long cocktailId, Long lastId) { | ||
| if (lastId == null) { | ||
| return cocktailCommentRepository.findTop10ByCocktailIdOrderByIdDesc(cocktailId) | ||
| .stream() | ||
| .map(CocktailCommentResponseDto::new) | ||
| .toList(); | ||
| } else { | ||
| return cocktailCommentRepository.findTop10ByCocktailIdAndIdLessThanOrderByIdDesc(cocktailId, lastId) | ||
| .stream() | ||
| .map(CocktailCommentResponseDto::new) | ||
| .toList(); | ||
| } | ||
| } | ||
|
|
||
| // 칵테일 댓글 단건 조회 로직 | ||
| @Transactional(readOnly = true) | ||
| public CocktailCommentResponseDto getCocktailComment(Long cocktailId, Long cocktailCommentId) { | ||
| CocktailComment cocktailComment = findByIdAndValidateCocktail(cocktailId, cocktailCommentId); | ||
|
|
||
| return new CocktailCommentResponseDto(cocktailComment); | ||
| } | ||
|
|
||
| // 칵테일댓글 ID로 찾고, 칵테일과의 관계를 검증 | ||
| private CocktailComment findByIdAndValidateCocktail(Long cocktailId, Long cocktailCommentId) { | ||
| CocktailComment cocktailComment = cocktailCommentRepository.findById(cocktailCommentId) | ||
| .orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + cocktailCommentId)); | ||
|
|
||
| if (!cocktailComment.getCocktail().getId().equals(cocktailId)) { | ||
| throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다."); | ||
| } | ||
| return cocktailComment; | ||
| } | ||
|
|
||
| // 칵테일댓글 수정 로직 | ||
| @Transactional | ||
| public CocktailCommentResponseDto updateCocktailComment(Long cocktailId, Long cocktailCommentId, CocktailCommentUpdateRequestDto requestDto) { | ||
| User user = rq.getActor(); | ||
|
|
||
| CocktailComment cocktailComment = findByIdAndValidateCocktail(cocktailId, cocktailCommentId); | ||
|
|
||
| if (!cocktailComment.getUser().getId().equals(user.getId())) { | ||
| throw new IllegalStateException("본인의 댓글만 수정할 수 있습니다."); | ||
| } | ||
|
|
||
| cocktailComment.updateContent(requestDto.content()); | ||
| return new CocktailCommentResponseDto(cocktailComment); | ||
| } | ||
|
|
||
| // 칵테일댓글 삭제 로직 | ||
| @Transactional | ||
| public void deleteCocktailComment(Long cocktailId, Long cocktailCommentId) { | ||
| User user = rq.getActor(); | ||
|
|
||
| CocktailComment cocktailComment = findByIdAndValidateCocktail(cocktailId, cocktailCommentId); | ||
|
|
||
| if (!cocktailComment.getUser().getId().equals(user.getId())) { | ||
| throw new IllegalStateException("본인의 댓글만 삭제할 수 있습니다."); | ||
| } | ||
|
|
||
| cocktailComment.updateStatus(CommentStatus.DELETED); // soft delete 사용. | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
순수한 질문이 있습니다.
@repository 어노테이션이 없어도 상관없나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확인감사합니다 제가 빼먹은것 같아욥
수정해서 다시 PR 올리겠습니다