From 25c5ab8385ca1f852122c954fb96f70d1b36a1c5 Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sat, 6 Jul 2024 20:22:06 +0900 Subject: [PATCH 1/7] =?UTF-8?q?:truck:=20Rename:=20Talk=20->=20Opinion=20D?= =?UTF-8?q?B=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/mypage/dto/MyQuestionDto.java | 6 +- .../mypage/repository/QuestionRepository.java | 6 +- .../server/mypage/service/MyPageService.java | 4 +- .../controller/OpinionController.java} | 47 ++++++------ .../{talk => opinion}/domain/Comment.java | 12 +-- .../{talk => opinion}/domain/Heart.java | 2 +- .../Talk.java => opinion/domain/Opinion.java} | 14 ++-- .../{talk => opinion}/domain/type/Order.java | 2 +- .../dto/request/CommentUploadRequest.java | 10 +-- .../dto/response/CommentResponse.java | 4 +- .../dto/response/CommentShortResponse.java | 4 +- .../dto/response/HeartResponse.java | 6 +- .../dto/response/OpinionResponse.java} | 10 +-- .../dto/response/OpinionResponseList.java | 15 ++++ .../exception/CommentNotFoundException.java | 2 +- .../exception/OpinionNotFoundException.java} | 4 +- .../errorcode/OpinionErrorCode.java} | 6 +- .../repository/CommentRepository.java | 10 +-- .../repository/HeartRepository.java | 6 +- .../opinion/repository/OpinionRepository.java | 23 ++++++ .../repository/init/CommentInitializer.java | 37 +++++---- .../repository/init/OpinionInitializer.java} | 76 +++++++++---------- .../service/OpinionService.java} | 76 +++++++++---------- .../post/repository/PostRepository.java | 1 - .../talk/dto/response/TalkResponseList.java | 15 ---- .../talk/repository/TalkRepository.java | 23 ------ .../com/ticle/server/user/domain/User.java | 5 +- 27 files changed, 210 insertions(+), 216 deletions(-) rename src/main/java/com/ticle/server/{talk/controller/TalkController.java => opinion/controller/OpinionController.java} (67%) rename src/main/java/com/ticle/server/{talk => opinion}/domain/Comment.java (77%) rename src/main/java/com/ticle/server/{talk => opinion}/domain/Heart.java (94%) rename src/main/java/com/ticle/server/{talk/domain/Talk.java => opinion/domain/Opinion.java} (74%) rename src/main/java/com/ticle/server/{talk => opinion}/domain/type/Order.java (87%) rename src/main/java/com/ticle/server/{talk => opinion}/dto/request/CommentUploadRequest.java (62%) rename src/main/java/com/ticle/server/{talk => opinion}/dto/response/CommentResponse.java (83%) rename src/main/java/com/ticle/server/{talk => opinion}/dto/response/CommentShortResponse.java (79%) rename src/main/java/com/ticle/server/{talk => opinion}/dto/response/HeartResponse.java (68%) rename src/main/java/com/ticle/server/{talk/dto/response/TalkResponse.java => opinion/dto/response/OpinionResponse.java} (77%) create mode 100644 src/main/java/com/ticle/server/opinion/dto/response/OpinionResponseList.java rename src/main/java/com/ticle/server/{talk => opinion}/exception/CommentNotFoundException.java (85%) rename src/main/java/com/ticle/server/{talk/exception/TalkNotFoundException.java => opinion/exception/OpinionNotFoundException.java} (64%) rename src/main/java/com/ticle/server/{talk/exception/errorcode/TalkErrorCode.java => opinion/exception/errorcode/OpinionErrorCode.java} (67%) rename src/main/java/com/ticle/server/{talk => opinion}/repository/CommentRepository.java (63%) rename src/main/java/com/ticle/server/{talk => opinion}/repository/HeartRepository.java (74%) create mode 100644 src/main/java/com/ticle/server/opinion/repository/OpinionRepository.java rename src/main/java/com/ticle/server/{talk => opinion}/repository/init/CommentInitializer.java (82%) rename src/main/java/com/ticle/server/{talk/repository/init/TalkInitializer.java => opinion/repository/init/OpinionInitializer.java} (70%) rename src/main/java/com/ticle/server/{talk/service/TalkService.java => opinion/service/OpinionService.java} (50%) delete mode 100644 src/main/java/com/ticle/server/talk/dto/response/TalkResponseList.java delete mode 100644 src/main/java/com/ticle/server/talk/repository/TalkRepository.java diff --git a/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java b/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java index cde76cf..1231511 100644 --- a/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java +++ b/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java @@ -1,6 +1,6 @@ package com.ticle.server.mypage.dto; -import com.ticle.server.talk.domain.Talk; +import com.ticle.server.opinion.domain.Opinion; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -14,9 +14,9 @@ public class MyQuestionDto { private Long view; private Long commentCount; - public static MyQuestionDto toDto(Talk talk){ + public static MyQuestionDto toDto(Opinion talk){ return MyQuestionDto.builder() - .questionId(talk.getTalkId()) + .questionId(talk.getOpinionId()) .question(talk.getQuestion()) .view(talk.getViewCount()) .commentCount(talk.getCommentCount()) diff --git a/src/main/java/com/ticle/server/mypage/repository/QuestionRepository.java b/src/main/java/com/ticle/server/mypage/repository/QuestionRepository.java index 5a26592..be7e9f3 100644 --- a/src/main/java/com/ticle/server/mypage/repository/QuestionRepository.java +++ b/src/main/java/com/ticle/server/mypage/repository/QuestionRepository.java @@ -1,12 +1,12 @@ package com.ticle.server.mypage.repository; -import com.ticle.server.talk.domain.Talk; +import com.ticle.server.opinion.domain.Opinion; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository -public interface QuestionRepository extends JpaRepository { - List findByUserId(Long userId); +public interface QuestionRepository extends JpaRepository { + List findByUserId(Long userId); } diff --git a/src/main/java/com/ticle/server/mypage/service/MyPageService.java b/src/main/java/com/ticle/server/mypage/service/MyPageService.java index 0f04b57..1172f69 100644 --- a/src/main/java/com/ticle/server/mypage/service/MyPageService.java +++ b/src/main/java/com/ticle/server/mypage/service/MyPageService.java @@ -10,7 +10,7 @@ import com.ticle.server.post.domain.Post; import com.ticle.server.post.repository.PostRepository; import com.ticle.server.scrapped.domain.Scrapped; -import com.ticle.server.talk.domain.Talk; +import com.ticle.server.opinion.domain.Opinion; import com.ticle.server.user.domain.type.Category; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -50,7 +50,7 @@ public List getSavedArticlesByCategory(Long userId, Category cate } public List getMyQuestions(Long userId) { - List questions = questionRepository.findByUserId(userId); + List questions = questionRepository.findByUserId(userId); return questions.stream() .map(MyQuestionDto::toDto) .collect(toList()); diff --git a/src/main/java/com/ticle/server/talk/controller/TalkController.java b/src/main/java/com/ticle/server/opinion/controller/OpinionController.java similarity index 67% rename from src/main/java/com/ticle/server/talk/controller/TalkController.java rename to src/main/java/com/ticle/server/opinion/controller/OpinionController.java index 5171ee0..60d6da0 100644 --- a/src/main/java/com/ticle/server/talk/controller/TalkController.java +++ b/src/main/java/com/ticle/server/opinion/controller/OpinionController.java @@ -1,19 +1,16 @@ -package com.ticle.server.talk.controller; +package com.ticle.server.opinion.controller; import com.ticle.server.global.dto.ResponseTemplate; -import com.ticle.server.talk.domain.type.Order; -import com.ticle.server.talk.dto.request.CommentUploadRequest; -import com.ticle.server.talk.dto.response.CommentResponse; -import com.ticle.server.talk.dto.response.TalkResponseList; -import com.ticle.server.talk.service.TalkService; +import com.ticle.server.opinion.domain.type.Order; +import com.ticle.server.opinion.dto.request.CommentUploadRequest; +import com.ticle.server.opinion.dto.response.CommentResponse; +import com.ticle.server.opinion.dto.response.OpinionResponseList; +import com.ticle.server.opinion.service.OpinionService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; @@ -25,22 +22,22 @@ import static com.ticle.server.global.dto.ResponseTemplate.EMPTY_RESPONSE; @Slf4j -@Tag(name = "Talk", description = "물어봥 관련 API") +@Tag(name = "Opinion", description = "티클문답 관련 API") @RestController @RequiredArgsConstructor -@RequestMapping("/talk") -public class TalkController { +@RequestMapping("/opinion") +public class OpinionController { - private final TalkService talkService; + private final OpinionService opinionService; @Operation(summary = "댓글 등록", description = "질문에 대한 댓글 등록하기") - @PostMapping("/{talkId}/comment") + @PostMapping("/{opinionId}/comment") public ResponseEntity> uploadComment( - @PathVariable Long talkId, + @PathVariable Long opinionId, @AuthenticationPrincipal UserDetails userDetails, @Valid @RequestBody CommentUploadRequest request) { - talkService.uploadComment(request, talkId, userDetails); + opinionService.uploadComment(request, opinionId, userDetails); return ResponseEntity .status(HttpStatus.OK) @@ -49,12 +46,12 @@ public ResponseEntity> uploadComment( @Operation(summary = "좋아요/취소 기능", description = "댓글에 대하여 좋아요 및 취소 기능
" + "유저가 좋아요 했던 댓글이라면 취소를, 좋아요 하지 않았던 댓글이라면 좋아요를 할 수 있습니다.") - @PostMapping("/comment/{commentId}/heart/{userId}") + @PostMapping("/comment/{commentId}/heart") public ResponseEntity> heartComment( @PathVariable Long commentId, - @PathVariable Long userId) { + @AuthenticationPrincipal UserDetails userDetails) { - talkService.heartComment(commentId, userId); + opinionService.heartComment(commentId, userDetails); return ResponseEntity .status(HttpStatus.OK) @@ -64,13 +61,13 @@ public ResponseEntity> heartComment( @Operation(summary = "질문에 대한 모든 댓글 가져오기", description = "질문에 대한 모든 댓글 가져오는 API입니다.
" + "orderBy에는 시간순이라면 time, 좋아요순이라면 heart를 넣어주시면 됩니다.(default는 시간순)
" + "isHeart는 해당 유저가 좋아요를 눌렀는지 유무를 나타냅니다.") - @GetMapping("/{talkId}/{userId}") + @GetMapping("/{opinionId}") public ResponseEntity> getComments( - @PathVariable Long talkId, - @PathVariable Long userId, + @PathVariable Long opinionId, + @AuthenticationPrincipal UserDetails userDetails, @RequestParam(required = false, defaultValue = "TIME") Order orderBy) { - List responses = talkService.getCommentsByTalk(talkId, userId, orderBy); + List responses = opinionService.getCommentsByOpinion(opinionId, userDetails, orderBy); return ResponseEntity .status(HttpStatus.OK) @@ -79,10 +76,10 @@ public ResponseEntity> getComments( @Operation(summary = "물어봥 질문 리스트 조회", description = "물어봥 모든 질문과 함께 인기댓글 2개 조회 가능") @GetMapping() - public ResponseEntity> getTalks( + public ResponseEntity> getOpinions( @RequestParam(defaultValue = "0") int page) { - TalkResponseList response = talkService.getTalksByPage(page); + OpinionResponseList response = opinionService.getOpinionsByPage(page); return ResponseEntity .status(HttpStatus.OK) diff --git a/src/main/java/com/ticle/server/talk/domain/Comment.java b/src/main/java/com/ticle/server/opinion/domain/Comment.java similarity index 77% rename from src/main/java/com/ticle/server/talk/domain/Comment.java rename to src/main/java/com/ticle/server/opinion/domain/Comment.java index 23d73ce..805e299 100644 --- a/src/main/java/com/ticle/server/talk/domain/Comment.java +++ b/src/main/java/com/ticle/server/opinion/domain/Comment.java @@ -1,4 +1,4 @@ -package com.ticle.server.talk.domain; +package com.ticle.server.opinion.domain; import com.ticle.server.global.domain.BaseTimeEntity; import com.ticle.server.user.domain.User; @@ -29,21 +29,21 @@ public class Comment extends BaseTimeEntity { @ManyToOne(fetch = FetchType.LAZY) private User user; - @JoinColumn(name = "talk_id") + @JoinColumn(name = "opinion_id") @ManyToOne(fetch = FetchType.LAZY) - private Talk talk; + private Opinion opinion; - @OneToMany(mappedBy = "comment", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(mappedBy = "comment", cascade = CascadeType.REMOVE, orphanRemoval = true) private List hearts = new ArrayList<>(); @Column(name = "heart_count") private Long heartCount; @Builder - public Comment(String content, User user, Talk talk, List hearts, Long heartCount) { + public Comment(String content, User user, Opinion opinion, List hearts, Long heartCount) { this.content = content; this.user = user; - this.talk = talk; + this.opinion = opinion; this.hearts = hearts; this.heartCount = heartCount; } diff --git a/src/main/java/com/ticle/server/talk/domain/Heart.java b/src/main/java/com/ticle/server/opinion/domain/Heart.java similarity index 94% rename from src/main/java/com/ticle/server/talk/domain/Heart.java rename to src/main/java/com/ticle/server/opinion/domain/Heart.java index 44bfafd..b3bf077 100644 --- a/src/main/java/com/ticle/server/talk/domain/Heart.java +++ b/src/main/java/com/ticle/server/opinion/domain/Heart.java @@ -1,4 +1,4 @@ -package com.ticle.server.talk.domain; +package com.ticle.server.opinion.domain; import com.ticle.server.user.domain.User; import jakarta.persistence.*; diff --git a/src/main/java/com/ticle/server/talk/domain/Talk.java b/src/main/java/com/ticle/server/opinion/domain/Opinion.java similarity index 74% rename from src/main/java/com/ticle/server/talk/domain/Talk.java rename to src/main/java/com/ticle/server/opinion/domain/Opinion.java index 5659ecb..79bd9ba 100644 --- a/src/main/java/com/ticle/server/talk/domain/Talk.java +++ b/src/main/java/com/ticle/server/opinion/domain/Opinion.java @@ -1,4 +1,4 @@ -package com.ticle.server.talk.domain; +package com.ticle.server.opinion.domain; import com.ticle.server.global.domain.BaseTimeEntity; import com.ticle.server.user.domain.User; @@ -11,16 +11,16 @@ import java.util.ArrayList; import java.util.List; -@Table(name = "talk") +@Table(name = "opinion") @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity -public class Talk extends BaseTimeEntity { +public class Opinion extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "talk_id") - private Long talkId; + @Column(name = "opinion_id") + private Long opinionId; @Column(name = "question", length = 200) private String question; @@ -32,14 +32,14 @@ public class Talk extends BaseTimeEntity { @ManyToOne(fetch = FetchType.LAZY) private User user; - @OneToMany(mappedBy = "talk", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(mappedBy = "opinion", cascade = CascadeType.REMOVE, orphanRemoval = true) private List comments = new ArrayList<>(); @Column(name = "comment_count") private Long commentCount; @Builder - public Talk(String question, Long viewCount, User user, List comments, Long commentCount) { + public Opinion(String question, Long viewCount, User user, List comments, Long commentCount) { this.question = question; this.viewCount = viewCount; this.user = user; diff --git a/src/main/java/com/ticle/server/talk/domain/type/Order.java b/src/main/java/com/ticle/server/opinion/domain/type/Order.java similarity index 87% rename from src/main/java/com/ticle/server/talk/domain/type/Order.java rename to src/main/java/com/ticle/server/opinion/domain/type/Order.java index afc672b..e7d7fae 100644 --- a/src/main/java/com/ticle/server/talk/domain/type/Order.java +++ b/src/main/java/com/ticle/server/opinion/domain/type/Order.java @@ -1,4 +1,4 @@ -package com.ticle.server.talk.domain.type; +package com.ticle.server.opinion.domain.type; import org.springframework.data.domain.Sort; diff --git a/src/main/java/com/ticle/server/talk/dto/request/CommentUploadRequest.java b/src/main/java/com/ticle/server/opinion/dto/request/CommentUploadRequest.java similarity index 62% rename from src/main/java/com/ticle/server/talk/dto/request/CommentUploadRequest.java rename to src/main/java/com/ticle/server/opinion/dto/request/CommentUploadRequest.java index f121901..0b28a5a 100644 --- a/src/main/java/com/ticle/server/talk/dto/request/CommentUploadRequest.java +++ b/src/main/java/com/ticle/server/opinion/dto/request/CommentUploadRequest.java @@ -1,7 +1,7 @@ -package com.ticle.server.talk.dto.request; +package com.ticle.server.opinion.dto.request; -import com.ticle.server.talk.domain.Comment; -import com.ticle.server.talk.domain.Talk; +import com.ticle.server.opinion.domain.Comment; +import com.ticle.server.opinion.domain.Opinion; import com.ticle.server.user.domain.User; import jakarta.validation.constraints.NotNull; @@ -9,10 +9,10 @@ public record CommentUploadRequest( @NotNull(message = "내용을 입력해주세요.") String content ) { - public Comment toComment(Talk talk, User user) { + public Comment toComment(Opinion opinion, User user) { return Comment .builder() - .talk(talk) + .opinion(opinion) .content(content) .heartCount(0L) .user(user) diff --git a/src/main/java/com/ticle/server/talk/dto/response/CommentResponse.java b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponse.java similarity index 83% rename from src/main/java/com/ticle/server/talk/dto/response/CommentResponse.java rename to src/main/java/com/ticle/server/opinion/dto/response/CommentResponse.java index 361c2f7..7022500 100644 --- a/src/main/java/com/ticle/server/talk/dto/response/CommentResponse.java +++ b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponse.java @@ -1,6 +1,6 @@ -package com.ticle.server.talk.dto.response; +package com.ticle.server.opinion.dto.response; -import com.ticle.server.talk.domain.Comment; +import com.ticle.server.opinion.domain.Comment; import lombok.Builder; @Builder diff --git a/src/main/java/com/ticle/server/talk/dto/response/CommentShortResponse.java b/src/main/java/com/ticle/server/opinion/dto/response/CommentShortResponse.java similarity index 79% rename from src/main/java/com/ticle/server/talk/dto/response/CommentShortResponse.java rename to src/main/java/com/ticle/server/opinion/dto/response/CommentShortResponse.java index a9216d2..a581182 100644 --- a/src/main/java/com/ticle/server/talk/dto/response/CommentShortResponse.java +++ b/src/main/java/com/ticle/server/opinion/dto/response/CommentShortResponse.java @@ -1,6 +1,6 @@ -package com.ticle.server.talk.dto.response; +package com.ticle.server.opinion.dto.response; -import com.ticle.server.talk.domain.Comment; +import com.ticle.server.opinion.domain.Comment; import lombok.Builder; @Builder diff --git a/src/main/java/com/ticle/server/talk/dto/response/HeartResponse.java b/src/main/java/com/ticle/server/opinion/dto/response/HeartResponse.java similarity index 68% rename from src/main/java/com/ticle/server/talk/dto/response/HeartResponse.java rename to src/main/java/com/ticle/server/opinion/dto/response/HeartResponse.java index 838136a..256473c 100644 --- a/src/main/java/com/ticle/server/talk/dto/response/HeartResponse.java +++ b/src/main/java/com/ticle/server/opinion/dto/response/HeartResponse.java @@ -1,7 +1,7 @@ -package com.ticle.server.talk.dto.response; +package com.ticle.server.opinion.dto.response; -import com.ticle.server.talk.domain.Comment; -import com.ticle.server.talk.domain.Heart; +import com.ticle.server.opinion.domain.Comment; +import com.ticle.server.opinion.domain.Heart; import com.ticle.server.user.domain.User; public record HeartResponse( diff --git a/src/main/java/com/ticle/server/talk/dto/response/TalkResponse.java b/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponse.java similarity index 77% rename from src/main/java/com/ticle/server/talk/dto/response/TalkResponse.java rename to src/main/java/com/ticle/server/opinion/dto/response/OpinionResponse.java index 85e0bf7..86b98db 100644 --- a/src/main/java/com/ticle/server/talk/dto/response/TalkResponse.java +++ b/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponse.java @@ -1,21 +1,21 @@ -package com.ticle.server.talk.dto.response; +package com.ticle.server.opinion.dto.response; -import com.ticle.server.talk.domain.Talk; +import com.ticle.server.opinion.domain.Opinion; import jakarta.validation.constraints.Size; import lombok.Builder; import java.util.List; @Builder -public record TalkResponse( +public record OpinionResponse( String question, Long viewCount, Long commentCount, @Size(max = 2, message = "최대 2개의 인기 댓글만 표시할 수 있습니다.") List comments ) { - public static TalkResponse from(Talk talk) { - return TalkResponse.builder() + public static OpinionResponse from(Opinion talk) { + return OpinionResponse.builder() .question(talk.getQuestion()) .viewCount(talk.getViewCount()) .commentCount(talk.getCommentCount()) diff --git a/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponseList.java b/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponseList.java new file mode 100644 index 0000000..c654e4b --- /dev/null +++ b/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponseList.java @@ -0,0 +1,15 @@ +package com.ticle.server.opinion.dto.response; + +import lombok.Builder; + +import java.util.List; + +@Builder +public record OpinionResponseList( + List opinionResponseList +) { + + public static OpinionResponseList from(List opinionList) { + return new OpinionResponseList(opinionList); + } +} \ No newline at end of file diff --git a/src/main/java/com/ticle/server/talk/exception/CommentNotFoundException.java b/src/main/java/com/ticle/server/opinion/exception/CommentNotFoundException.java similarity index 85% rename from src/main/java/com/ticle/server/talk/exception/CommentNotFoundException.java rename to src/main/java/com/ticle/server/opinion/exception/CommentNotFoundException.java index 07042cb..78abef9 100644 --- a/src/main/java/com/ticle/server/talk/exception/CommentNotFoundException.java +++ b/src/main/java/com/ticle/server/opinion/exception/CommentNotFoundException.java @@ -1,4 +1,4 @@ -package com.ticle.server.talk.exception; +package com.ticle.server.opinion.exception; import com.ticle.server.global.exception.errorcode.ErrorCode; import lombok.Getter; diff --git a/src/main/java/com/ticle/server/talk/exception/TalkNotFoundException.java b/src/main/java/com/ticle/server/opinion/exception/OpinionNotFoundException.java similarity index 64% rename from src/main/java/com/ticle/server/talk/exception/TalkNotFoundException.java rename to src/main/java/com/ticle/server/opinion/exception/OpinionNotFoundException.java index bc74bae..b1532b9 100644 --- a/src/main/java/com/ticle/server/talk/exception/TalkNotFoundException.java +++ b/src/main/java/com/ticle/server/opinion/exception/OpinionNotFoundException.java @@ -1,4 +1,4 @@ -package com.ticle.server.talk.exception; +package com.ticle.server.opinion.exception; import com.ticle.server.global.exception.errorcode.ErrorCode; import lombok.Getter; @@ -6,7 +6,7 @@ @Getter @RequiredArgsConstructor -public class TalkNotFoundException extends RuntimeException { +public class OpinionNotFoundException extends RuntimeException { private final ErrorCode errorCode; } diff --git a/src/main/java/com/ticle/server/talk/exception/errorcode/TalkErrorCode.java b/src/main/java/com/ticle/server/opinion/exception/errorcode/OpinionErrorCode.java similarity index 67% rename from src/main/java/com/ticle/server/talk/exception/errorcode/TalkErrorCode.java rename to src/main/java/com/ticle/server/opinion/exception/errorcode/OpinionErrorCode.java index 1b46b13..e2b5a21 100644 --- a/src/main/java/com/ticle/server/talk/exception/errorcode/TalkErrorCode.java +++ b/src/main/java/com/ticle/server/opinion/exception/errorcode/OpinionErrorCode.java @@ -1,4 +1,4 @@ -package com.ticle.server.talk.exception.errorcode; +package com.ticle.server.opinion.exception.errorcode; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -7,9 +7,9 @@ @Getter @RequiredArgsConstructor -public enum TalkErrorCode implements ErrorCode { +public enum OpinionErrorCode implements ErrorCode { COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "Comment not found"), - TALK_NOT_FOUND(HttpStatus.NOT_FOUND, "Talk not found"), + OPINION_NOT_FOUND(HttpStatus.NOT_FOUND, "Opinion not found"), ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/ticle/server/talk/repository/CommentRepository.java b/src/main/java/com/ticle/server/opinion/repository/CommentRepository.java similarity index 63% rename from src/main/java/com/ticle/server/talk/repository/CommentRepository.java rename to src/main/java/com/ticle/server/opinion/repository/CommentRepository.java index 7401d4a..69bd25f 100644 --- a/src/main/java/com/ticle/server/talk/repository/CommentRepository.java +++ b/src/main/java/com/ticle/server/opinion/repository/CommentRepository.java @@ -1,7 +1,7 @@ -package com.ticle.server.talk.repository; +package com.ticle.server.opinion.repository; -import com.ticle.server.talk.domain.Comment; -import com.ticle.server.talk.domain.Talk; +import com.ticle.server.opinion.domain.Comment; +import com.ticle.server.opinion.domain.Opinion; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -14,6 +14,6 @@ public interface CommentRepository extends JpaRepository { @Query("SELECT c " + "FROM Comment c JOIN FETCH Heart h ON c.commentId = h.comment.commentId " + - "WHERE c.talk = :talk") - List findAllByTalkWithFetch(Talk talk, Sort sort); + "WHERE c.opinion = :opinion") + List findAllByOpinionWithFetch(Opinion opinion, Sort sort); } diff --git a/src/main/java/com/ticle/server/talk/repository/HeartRepository.java b/src/main/java/com/ticle/server/opinion/repository/HeartRepository.java similarity index 74% rename from src/main/java/com/ticle/server/talk/repository/HeartRepository.java rename to src/main/java/com/ticle/server/opinion/repository/HeartRepository.java index c2fbb7c..39d2f08 100644 --- a/src/main/java/com/ticle/server/talk/repository/HeartRepository.java +++ b/src/main/java/com/ticle/server/opinion/repository/HeartRepository.java @@ -1,7 +1,7 @@ -package com.ticle.server.talk.repository; +package com.ticle.server.opinion.repository; -import com.ticle.server.talk.domain.Comment; -import com.ticle.server.talk.domain.Heart; +import com.ticle.server.opinion.domain.Comment; +import com.ticle.server.opinion.domain.Heart; import com.ticle.server.user.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; diff --git a/src/main/java/com/ticle/server/opinion/repository/OpinionRepository.java b/src/main/java/com/ticle/server/opinion/repository/OpinionRepository.java new file mode 100644 index 0000000..09f4516 --- /dev/null +++ b/src/main/java/com/ticle/server/opinion/repository/OpinionRepository.java @@ -0,0 +1,23 @@ +package com.ticle.server.opinion.repository; + +import com.ticle.server.opinion.domain.Opinion; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface OpinionRepository extends JpaRepository { + + @Query("SELECT o " + + "FROM Opinion o JOIN FETCH Comment c on o.opinionId = c.opinion.opinionId " + + "WHERE o.opinionId = :opinionId") + Optional findByOpinionIdWithFetch(Long opinionId); + + @EntityGraph(attributePaths = {"comments", "comments.user"}) + Page findAll(Pageable pageable); +} diff --git a/src/main/java/com/ticle/server/talk/repository/init/CommentInitializer.java b/src/main/java/com/ticle/server/opinion/repository/init/CommentInitializer.java similarity index 82% rename from src/main/java/com/ticle/server/talk/repository/init/CommentInitializer.java rename to src/main/java/com/ticle/server/opinion/repository/init/CommentInitializer.java index 051b734..445d0b6 100644 --- a/src/main/java/com/ticle/server/talk/repository/init/CommentInitializer.java +++ b/src/main/java/com/ticle/server/opinion/repository/init/CommentInitializer.java @@ -1,10 +1,10 @@ -package com.ticle.server.talk.repository.init; +package com.ticle.server.opinion.repository.init; import com.ticle.server.global.util.LocalDummyDataInit; -import com.ticle.server.talk.domain.Comment; -import com.ticle.server.talk.domain.Talk; -import com.ticle.server.talk.repository.CommentRepository; -import com.ticle.server.talk.repository.TalkRepository; +import com.ticle.server.opinion.domain.Comment; +import com.ticle.server.opinion.domain.Opinion; +import com.ticle.server.opinion.repository.CommentRepository; +import com.ticle.server.opinion.repository.OpinionRepository; import com.ticle.server.user.domain.User; import com.ticle.server.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -24,7 +24,7 @@ public class CommentInitializer implements ApplicationRunner { private final UserRepository userRepository; private final CommentRepository commentRepository; - private final TalkRepository talkRepository; + private final OpinionRepository opinionRepository; @Override public void run(ApplicationArguments args) throws Exception { @@ -37,78 +37,77 @@ public void run(ApplicationArguments args) throws Exception { User dummyUser4 = userRepository.findById(5L).orElseThrow(); User dummyUser5 = userRepository.findById(6L).orElseThrow(); - Talk talk1 = talkRepository.findById(1L).orElseThrow(); - Talk talk2 = talkRepository.findById(2L).orElseThrow(); + Opinion opinion1 = opinionRepository.findById(1L).orElseThrow(); + Opinion opinion2 = opinionRepository.findById(2L).orElseThrow(); List commentList = new ArrayList<>(); Comment comment1 = Comment.builder() - .talk(talk1) + .opinion(opinion1) .content("좋은 리더에게는 리더십이 필요해") .heartCount(5L) .user(dummyUser) .build(); Comment comment2 = Comment.builder() - .talk(talk1) + .opinion(opinion1) .content("리더는 소통 능력이 중요하다고 생각해") .heartCount(8L) .user(dummyUser2) .build(); Comment comment3 = Comment.builder() - .talk(talk1) + .opinion(opinion1) .content("책임감과 결단력이 있어야 해") .heartCount(10L) .user(dummyUser3) .build(); Comment comment4 = Comment.builder() - .talk(talk1) + .opinion(opinion1) .content("팀원들의 의견을 존중하는 리더가 필요해") .heartCount(7L) .user(dummyUser4) .build(); Comment comment5 = Comment.builder() - .talk(talk1) + .opinion(opinion1) .content("문제를 해결하는 능력도 중요하지") .heartCount(6L) .user(dummyUser5) .build(); - // "팀에서 가장 중요한 구성원은 누구인가요?"에 대한 댓글 5개 Comment comment6 = Comment.builder() - .talk(talk2) + .opinion(opinion2) .content("모두가 중요한 역할을 하지만, 프로젝트 매니저가 핵심이라고 생각해") .heartCount(5L) .user(dummyUser4) .build(); Comment comment7 = Comment.builder() - .talk(talk2) + .opinion(opinion2) .content("팀의 기술 리더가 가장 중요한 구성원이라고 생각해") .heartCount(8L) .user(dummyUser) .build(); Comment comment8 = Comment.builder() - .talk(talk2) + .opinion(opinion2) .content("커뮤니케이션을 담당하는 사람이 중요하다고 봐") .heartCount(10L) .user(dummyUser5) .build(); Comment comment9 = Comment.builder() - .talk(talk2) + .opinion(opinion2) .content("모든 팀원이 각자의 역할을 충실히 하는 것이 가장 중요해") .heartCount(7L) .user(dummyUser2) .build(); Comment comment10 = Comment.builder() - .talk(talk2) + .opinion(opinion2) .content("팀워크를 이끌어가는 리더가 가장 중요한 구성원이지") .heartCount(6L) .user(dummyUser3) diff --git a/src/main/java/com/ticle/server/talk/repository/init/TalkInitializer.java b/src/main/java/com/ticle/server/opinion/repository/init/OpinionInitializer.java similarity index 70% rename from src/main/java/com/ticle/server/talk/repository/init/TalkInitializer.java rename to src/main/java/com/ticle/server/opinion/repository/init/OpinionInitializer.java index 18297e6..e04164e 100644 --- a/src/main/java/com/ticle/server/talk/repository/init/TalkInitializer.java +++ b/src/main/java/com/ticle/server/opinion/repository/init/OpinionInitializer.java @@ -1,8 +1,8 @@ -package com.ticle.server.talk.repository.init; +package com.ticle.server.opinion.repository.init; import com.ticle.server.global.util.LocalDummyDataInit; -import com.ticle.server.talk.domain.Talk; -import com.ticle.server.talk.repository.TalkRepository; +import com.ticle.server.opinion.domain.Opinion; +import com.ticle.server.opinion.repository.OpinionRepository; import com.ticle.server.user.domain.User; import com.ticle.server.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -18,134 +18,134 @@ @RequiredArgsConstructor @Order(1) @LocalDummyDataInit -public class TalkInitializer implements ApplicationRunner { +public class OpinionInitializer implements ApplicationRunner { private final UserRepository userRepository; - private final TalkRepository talkRepository; + private final OpinionRepository opinionRepository; @Override public void run(ApplicationArguments args) throws Exception { - if (talkRepository.count() > 0) { - log.info("[Talk] 더미 데이터 존재"); + if (opinionRepository.count() > 0) { + log.info("[Opinion] 더미 데이터 존재"); } else { User dummyAdmin = userRepository.findById(1L).orElseThrow(); - List talkList = new ArrayList<>(); + List opinionList = new ArrayList<>(); - Talk talk1 = Talk.builder() + Opinion opinion1 = Opinion.builder() .question("좋은 리더란 어떤 자질이 필요할까요?") .viewCount(5L) .commentCount(4L) .user(dummyAdmin) .build(); - Talk talk2 = Talk.builder() + Opinion opinion2 = Opinion.builder() .question("팀에서 가장 중요한 구성원은 누구인가요?") .viewCount(10L) .commentCount(3L) .user(dummyAdmin) .build(); - Talk talk3 = Talk.builder() + Opinion opinion3 = Opinion.builder() .question("롤모델이 있다면 누구이고 이유는 무엇인가요?") .viewCount(14L) .commentCount(5L) .user(dummyAdmin) .build(); - Talk talk4 = Talk.builder() + Opinion opinion4 = Opinion.builder() .question("인생에서 가장 필요한 사항은 무엇이라 생각하나요?") .viewCount(20L) .commentCount(7L) .user(dummyAdmin) .build(); - Talk talk5 = Talk.builder() + Opinion opinion5 = Opinion.builder() .question("대인관계에서 가장 중요하게 생각하는 것은 무엇인가요?") .viewCount(4L) .commentCount(2L) .user(dummyAdmin) .build(); - Talk talk6 = Talk.builder() + Opinion opinion6 = Opinion.builder() .question("가장 기억에 남는 프로젝트는 무엇인가요?") .viewCount(12L) .commentCount(6L) .user(dummyAdmin) .build(); - Talk talk7 = Talk.builder() + Opinion opinion7 = Opinion.builder() .question("성공적인 팀워크를 위해 가장 필요한 요소는 무엇인가요?") .viewCount(8L) .commentCount(3L) .user(dummyAdmin) .build(); - Talk talk8 = Talk.builder() + Opinion opinion8 = Opinion.builder() .question("기술 발전이 사회에 미치는 영향은 무엇인가요?") .viewCount(15L) .commentCount(4L) .user(dummyAdmin) .build(); - Talk talk9 = Talk.builder() + Opinion opinion9 = Opinion.builder() .question("효과적인 문제 해결 방법은 무엇이라 생각하나요?") .viewCount(9L) .commentCount(2L) .user(dummyAdmin) .build(); - Talk talk10 = Talk.builder() + Opinion opinion10 = Opinion.builder() .question("업무에서 동기부여를 유지하는 방법은 무엇인가요?") .viewCount(18L) .commentCount(5L) .user(dummyAdmin) .build(); - Talk talk11 = Talk.builder() + Opinion opinion11 = Opinion.builder() .question("IT 업계에서 가장 중요한 기술 트렌드는 무엇인가요?") .viewCount(11L) .commentCount(3L) .user(dummyAdmin) .build(); - Talk talk12 = Talk.builder() + Opinion opinion12 = Opinion.builder() .question("효과적인 의사소통 방법은 무엇이라고 생각하나요?") .viewCount(7L) .commentCount(4L) .user(dummyAdmin) .build(); - Talk talk13 = Talk.builder() + Opinion opinion13 = Opinion.builder() .question("최근에 배운 가장 중요한 교훈은 무엇인가요?") .viewCount(10L) .commentCount(5L) .user(dummyAdmin) .build(); - Talk talk14 = Talk.builder() + Opinion opinion14 = Opinion.builder() .question("미래의 커리어 목표는 무엇인가요?") .viewCount(13L) .commentCount(6L) .user(dummyAdmin) .build(); - talkList.add(talk1); - talkList.add(talk2); - talkList.add(talk3); - talkList.add(talk4); - talkList.add(talk5); - talkList.add(talk6); - talkList.add(talk7); - talkList.add(talk8); - talkList.add(talk9); - talkList.add(talk10); - talkList.add(talk11); - talkList.add(talk12); - talkList.add(talk13); - talkList.add(talk14); - - talkRepository.saveAll(talkList); + opinionList.add(opinion1); + opinionList.add(opinion2); + opinionList.add(opinion3); + opinionList.add(opinion4); + opinionList.add(opinion5); + opinionList.add(opinion6); + opinionList.add(opinion7); + opinionList.add(opinion8); + opinionList.add(opinion9); + opinionList.add(opinion10); + opinionList.add(opinion11); + opinionList.add(opinion12); + opinionList.add(opinion13); + opinionList.add(opinion14); + + opinionRepository.saveAll(opinionList); } } } diff --git a/src/main/java/com/ticle/server/talk/service/TalkService.java b/src/main/java/com/ticle/server/opinion/service/OpinionService.java similarity index 50% rename from src/main/java/com/ticle/server/talk/service/TalkService.java rename to src/main/java/com/ticle/server/opinion/service/OpinionService.java index b5fd012..55a1096 100644 --- a/src/main/java/com/ticle/server/talk/service/TalkService.java +++ b/src/main/java/com/ticle/server/opinion/service/OpinionService.java @@ -1,18 +1,18 @@ -package com.ticle.server.talk.service; - -import com.ticle.server.talk.domain.Comment; -import com.ticle.server.talk.domain.Talk; -import com.ticle.server.talk.domain.type.Order; -import com.ticle.server.talk.dto.request.CommentUploadRequest; -import com.ticle.server.talk.dto.response.CommentResponse; -import com.ticle.server.talk.dto.response.HeartResponse; -import com.ticle.server.talk.dto.response.TalkResponse; -import com.ticle.server.talk.dto.response.TalkResponseList; -import com.ticle.server.talk.exception.CommentNotFoundException; -import com.ticle.server.talk.exception.TalkNotFoundException; -import com.ticle.server.talk.repository.CommentRepository; -import com.ticle.server.talk.repository.HeartRepository; -import com.ticle.server.talk.repository.TalkRepository; +package com.ticle.server.opinion.service; + +import com.ticle.server.opinion.domain.Comment; +import com.ticle.server.opinion.domain.Opinion; +import com.ticle.server.opinion.domain.type.Order; +import com.ticle.server.opinion.dto.request.CommentUploadRequest; +import com.ticle.server.opinion.dto.response.CommentResponse; +import com.ticle.server.opinion.dto.response.HeartResponse; +import com.ticle.server.opinion.dto.response.OpinionResponse; +import com.ticle.server.opinion.dto.response.OpinionResponseList; +import com.ticle.server.opinion.exception.CommentNotFoundException; +import com.ticle.server.opinion.exception.OpinionNotFoundException; +import com.ticle.server.opinion.repository.CommentRepository; +import com.ticle.server.opinion.repository.HeartRepository; +import com.ticle.server.opinion.repository.OpinionRepository; import com.ticle.server.user.domain.User; import com.ticle.server.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -27,20 +27,20 @@ import java.util.List; -import static com.ticle.server.talk.domain.type.Order.TIME; -import static com.ticle.server.talk.domain.type.Order.getOrder; -import static com.ticle.server.talk.exception.errorcode.TalkErrorCode.COMMENT_NOT_FOUND; -import static com.ticle.server.talk.exception.errorcode.TalkErrorCode.TALK_NOT_FOUND; +import static com.ticle.server.opinion.domain.type.Order.TIME; +import static com.ticle.server.opinion.domain.type.Order.getOrder; +import static com.ticle.server.opinion.exception.errorcode.OpinionErrorCode.COMMENT_NOT_FOUND; +import static com.ticle.server.opinion.exception.errorcode.OpinionErrorCode.OPINION_NOT_FOUND; @Slf4j @Service @Transactional(readOnly = true) @RequiredArgsConstructor -public class TalkService { +public class OpinionService { private final UserRepository userRepository; private final CommentRepository commentRepository; - private final TalkRepository talkRepository; + private final OpinionRepository opinionRepository; private final HeartRepository heartRepository; private static final int PAGE_SIZE = 5; @@ -49,18 +49,18 @@ public class TalkService { public void uploadComment(CommentUploadRequest request, Long talkId, UserDetails userId) { User user = userRepository.findByEmail(userId.getUsername()) .orElseThrow(RuntimeException::new); - Talk talk = talkRepository.findById(talkId) - .orElseThrow(() -> new TalkNotFoundException(TALK_NOT_FOUND)); + Opinion opinion = opinionRepository.findById(talkId) + .orElseThrow(() -> new OpinionNotFoundException(OPINION_NOT_FOUND)); - Comment comment = request.toComment(talk, user); - talk.addCommentCount(); + Comment comment = request.toComment(opinion, user); + opinion.addCommentCount(); commentRepository.save(comment); } @Transactional - public void heartComment(Long commentId, Long userId) { - User user = userRepository.findById(userId) + public void heartComment(Long commentId, UserDetails userDetails) { + User user = userRepository.findByEmail(userDetails.getUsername()) .orElseThrow(RuntimeException::new); Comment comment = commentRepository.findById(commentId) .orElseThrow(() -> new CommentNotFoundException(COMMENT_NOT_FOUND)); @@ -79,18 +79,18 @@ public void heartComment(Long commentId, Long userId) { } @Transactional - public List getCommentsByTalk(Long talkId, Long userId, Order orderBy) { - User user = userRepository.findById(userId) + public List getCommentsByOpinion(Long opinionId, UserDetails userDetails, Order orderBy) { + User user = userRepository.findByEmail(userDetails.getUsername()) .orElseThrow(RuntimeException::new); - Talk talk = talkRepository.findByTalkIdWithFetch(talkId) - .orElseThrow(() -> new TalkNotFoundException(TALK_NOT_FOUND)); + Opinion opinion = opinionRepository.findByOpinionIdWithFetch(opinionId) + .orElseThrow(() -> new OpinionNotFoundException(OPINION_NOT_FOUND)); // 질문에 대한 댓글들 보기 위해 클릭했을 시 질문 조회수 +1 - talk.addViewCount(); + opinion.addViewCount(); Sort sort = getOrder(orderBy); - List comments = commentRepository.findAllByTalkWithFetch(talk, sort); + List comments = commentRepository.findAllByOpinionWithFetch(opinion, sort); return comments.stream() .map(comment -> { @@ -100,15 +100,15 @@ public List getCommentsByTalk(Long talkId, Long userId, Order o .toList(); } - public TalkResponseList getTalksByPage(int page) { + public OpinionResponseList getOpinionsByPage(int page) { Pageable pageable = PageRequest.of(page, PAGE_SIZE, getOrder(TIME)); - Page talkPage = talkRepository.findAll(pageable); + Page opinionPage = opinionRepository.findAll(pageable); - List talkResponseList = talkPage.stream() - .map(TalkResponse::from) + List opinionResponseList = opinionPage.stream() + .map(OpinionResponse::from) .toList(); - return TalkResponseList.from(talkResponseList); + return OpinionResponseList.from(opinionResponseList); } } \ No newline at end of file diff --git a/src/main/java/com/ticle/server/post/repository/PostRepository.java b/src/main/java/com/ticle/server/post/repository/PostRepository.java index 07c27e4..adcfa17 100644 --- a/src/main/java/com/ticle/server/post/repository/PostRepository.java +++ b/src/main/java/com/ticle/server/post/repository/PostRepository.java @@ -2,7 +2,6 @@ import com.ticle.server.user.domain.type.Category; import com.ticle.server.post.domain.Post; -import com.ticle.server.talk.domain.Talk; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/src/main/java/com/ticle/server/talk/dto/response/TalkResponseList.java b/src/main/java/com/ticle/server/talk/dto/response/TalkResponseList.java deleted file mode 100644 index f1ddd1e..0000000 --- a/src/main/java/com/ticle/server/talk/dto/response/TalkResponseList.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.ticle.server.talk.dto.response; - -import lombok.Builder; - -import java.util.List; - -@Builder -public record TalkResponseList( - List talkResponseList -) { - - public static TalkResponseList from(List talkList) { - return new TalkResponseList(talkList); - } -} \ No newline at end of file diff --git a/src/main/java/com/ticle/server/talk/repository/TalkRepository.java b/src/main/java/com/ticle/server/talk/repository/TalkRepository.java deleted file mode 100644 index 4b43e24..0000000 --- a/src/main/java/com/ticle/server/talk/repository/TalkRepository.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ticle.server.talk.repository; - -import com.ticle.server.talk.domain.Talk; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.repository.EntityGraph; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.stereotype.Repository; - -import java.util.Optional; - -@Repository -public interface TalkRepository extends JpaRepository { - - @Query("SELECT t " + - "FROM Talk t JOIN FETCH Comment c on t.talkId = c.talk.talkId " + - "WHERE t.talkId = :talkId") - Optional findByTalkIdWithFetch(Long talkId); - - @EntityGraph(attributePaths = {"comments", "comments.user"}) - Page findAll(Pageable pageable); -} diff --git a/src/main/java/com/ticle/server/user/domain/User.java b/src/main/java/com/ticle/server/user/domain/User.java index b5e0cee..8da0586 100644 --- a/src/main/java/com/ticle/server/user/domain/User.java +++ b/src/main/java/com/ticle/server/user/domain/User.java @@ -2,7 +2,7 @@ import com.ticle.server.memo.domain.Memo; import com.ticle.server.scrapped.domain.Scrapped; -import com.ticle.server.talk.domain.Talk; +import com.ticle.server.opinion.domain.Opinion; import com.ticle.server.user.domain.type.Category; import jakarta.persistence.*; @@ -14,7 +14,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; @Table(name = "users") @@ -49,7 +48,7 @@ public class User { private boolean agreeTerms; @OneToMany(mappedBy = "user") - private List talks; + private List talks; @OneToMany(mappedBy = "user") private List memos; From af751995e57a57f5d6974234e54ac31841c8ff6e Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sat, 6 Jul 2024 22:36:53 +0900 Subject: [PATCH 2/7] =?UTF-8?q?:heavy=5Fplus=5Fsign:=20Add:=20Response?= =?UTF-8?q?=EC=97=90=20Id=20=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/opinion/dto/response/CommentResponse.java | 2 ++ .../server/opinion/dto/response/OpinionResponse.java | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/ticle/server/opinion/dto/response/CommentResponse.java b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponse.java index 7022500..4839a91 100644 --- a/src/main/java/com/ticle/server/opinion/dto/response/CommentResponse.java +++ b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponse.java @@ -5,6 +5,7 @@ @Builder public record CommentResponse( + Long commentId, String nickname, String content, Long heartCount, @@ -12,6 +13,7 @@ public record CommentResponse( ) { public static CommentResponse of(Comment comment, Boolean isHeart) { return CommentResponse.builder() + .commentId(comment.getCommentId()) .nickname(comment.getUser().getNickName()) .content(comment.getContent()) .heartCount(comment.getHeartCount()) diff --git a/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponse.java b/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponse.java index 86b98db..624a70b 100644 --- a/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponse.java +++ b/src/main/java/com/ticle/server/opinion/dto/response/OpinionResponse.java @@ -8,18 +8,20 @@ @Builder public record OpinionResponse( + Long opinionId, String question, Long viewCount, Long commentCount, @Size(max = 2, message = "최대 2개의 인기 댓글만 표시할 수 있습니다.") List comments ) { - public static OpinionResponse from(Opinion talk) { + public static OpinionResponse from(Opinion opinion) { return OpinionResponse.builder() - .question(talk.getQuestion()) - .viewCount(talk.getViewCount()) - .commentCount(talk.getCommentCount()) - .comments(talk.getComments().stream() + .opinionId(opinion.getOpinionId()) + .question(opinion.getQuestion()) + .viewCount(opinion.getViewCount()) + .commentCount(opinion.getCommentCount()) + .comments(opinion.getComments().stream() .sorted((c1, c2) -> Long.compare(c2.getHeartCount(), c1.getHeartCount())) .limit(2) .map(CommentShortResponse::from) From 841281ca87383c85519d38546223e582f71a9650 Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sat, 6 Jul 2024 22:38:51 +0900 Subject: [PATCH 3/7] =?UTF-8?q?:zap:=20Improve:=20N+1=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=20=ED=95=B4=EA=B2=B0=20=EB=B0=8F=20=EC=BF=BC=EB=A6=AC=20?= =?UTF-8?q?=EC=B5=9C=EC=A0=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/opinion/repository/CommentRepository.java | 8 +++----- .../ticle/server/opinion/repository/HeartRepository.java | 2 -- .../com/ticle/server/opinion/service/OpinionService.java | 5 +++-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/ticle/server/opinion/repository/CommentRepository.java b/src/main/java/com/ticle/server/opinion/repository/CommentRepository.java index 69bd25f..b878a97 100644 --- a/src/main/java/com/ticle/server/opinion/repository/CommentRepository.java +++ b/src/main/java/com/ticle/server/opinion/repository/CommentRepository.java @@ -3,8 +3,8 @@ import com.ticle.server.opinion.domain.Comment; import com.ticle.server.opinion.domain.Opinion; import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; @@ -12,8 +12,6 @@ @Repository public interface CommentRepository extends JpaRepository { - @Query("SELECT c " + - "FROM Comment c JOIN FETCH Heart h ON c.commentId = h.comment.commentId " + - "WHERE c.opinion = :opinion") - List findAllByOpinionWithFetch(Opinion opinion, Sort sort); + @EntityGraph(attributePaths = {"hearts", "user"}) + List findAllByOpinion(Opinion opinion, Sort sort); } diff --git a/src/main/java/com/ticle/server/opinion/repository/HeartRepository.java b/src/main/java/com/ticle/server/opinion/repository/HeartRepository.java index 39d2f08..acebea7 100644 --- a/src/main/java/com/ticle/server/opinion/repository/HeartRepository.java +++ b/src/main/java/com/ticle/server/opinion/repository/HeartRepository.java @@ -11,7 +11,5 @@ @Repository public interface HeartRepository extends JpaRepository { - Boolean existsByUserAndComment(User user, Comment comment); - Optional findHeartByUserAndComment(User user, Comment comment); } diff --git a/src/main/java/com/ticle/server/opinion/service/OpinionService.java b/src/main/java/com/ticle/server/opinion/service/OpinionService.java index 55a1096..206b62b 100644 --- a/src/main/java/com/ticle/server/opinion/service/OpinionService.java +++ b/src/main/java/com/ticle/server/opinion/service/OpinionService.java @@ -90,11 +90,12 @@ public List getCommentsByOpinion(Long opinionId, UserDetails us Sort sort = getOrder(orderBy); - List comments = commentRepository.findAllByOpinionWithFetch(opinion, sort); + List comments = commentRepository.findAllByOpinion(opinion, sort); return comments.stream() .map(comment -> { - boolean isHeart = heartRepository.existsByUserAndComment(user, comment); + boolean isHeart = comment.getHearts().stream() + .anyMatch(heart -> heart.getUser().equals(user)); return CommentResponse.of(comment, isHeart); }) .toList(); From 7ebd9ce3199dffafb66a4886f854a76f1cbfce3e Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sat, 6 Jul 2024 22:56:18 +0900 Subject: [PATCH 4/7] =?UTF-8?q?:truck:=20Rename:=20talk=20->=20opinion?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD=EB=90=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EC=9D=80=20=EB=B6=80=EB=B6=84=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ticle/server/mypage/dto/MyQuestionDto.java | 10 +++++----- .../java/com/ticle/server/user/domain/User.java | 16 +++++++--------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java b/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java index 1231511..214a97f 100644 --- a/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java +++ b/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java @@ -14,12 +14,12 @@ public class MyQuestionDto { private Long view; private Long commentCount; - public static MyQuestionDto toDto(Opinion talk){ + public static MyQuestionDto toDto(Opinion opinion){ return MyQuestionDto.builder() - .questionId(talk.getOpinionId()) - .question(talk.getQuestion()) - .view(talk.getViewCount()) - .commentCount(talk.getCommentCount()) + .questionId(opinion.getOpinionId()) + .question(opinion.getQuestion()) + .view(opinion.getViewCount()) + .commentCount(opinion.getCommentCount()) .build(); } } diff --git a/src/main/java/com/ticle/server/user/domain/User.java b/src/main/java/com/ticle/server/user/domain/User.java index 8da0586..1bf718a 100644 --- a/src/main/java/com/ticle/server/user/domain/User.java +++ b/src/main/java/com/ticle/server/user/domain/User.java @@ -27,16 +27,15 @@ public class User { // 주로 데이터베이스와 직접적으로 연관되어 있음 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name="user_id") + @Column(name = "user_id") private Long id; - @Column(name="email",nullable = false) + @Column(name = "email", nullable = false) private String email; - @Column(name = "password",nullable = false) + @Column(name = "password", nullable = false) private String password; - @Column(name = "nick_name") private String nickName; @@ -48,22 +47,21 @@ public class User { private boolean agreeTerms; @OneToMany(mappedBy = "user") - private List talks; + private List opinions; @OneToMany(mappedBy = "user") private List memos; - @OneToMany(mappedBy = "user",cascade = CascadeType.ALL,orphanRemoval = true) + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private List scrappeds; - @ElementCollection(fetch=FetchType.EAGER) + @ElementCollection(fetch = FetchType.EAGER) @Builder.Default private List roles = new ArrayList<>(); - public Collection getAuthorities(){ + public Collection getAuthorities() { return this.roles.stream() .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()); } - } From b47871637a2e39ee6f92d0817ca5b61ca84d9055 Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sun, 7 Jul 2024 00:24:17 +0900 Subject: [PATCH 5/7] =?UTF-8?q?:pencil2:=20Chore:=20view=20->=20viewCount?= =?UTF-8?q?=20=EB=B0=98=EC=98=81=20=EC=95=88=EB=90=9C=EA=B1=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java b/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java index 214a97f..6a662ed 100644 --- a/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java +++ b/src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java @@ -11,14 +11,14 @@ public class MyQuestionDto { private Long questionId; private String question; - private Long view; + private Long viewCount; private Long commentCount; public static MyQuestionDto toDto(Opinion opinion){ return MyQuestionDto.builder() .questionId(opinion.getOpinionId()) .question(opinion.getQuestion()) - .view(opinion.getViewCount()) + .viewCount(opinion.getViewCount()) .commentCount(opinion.getCommentCount()) .build(); } From 197bb4e2f6dd2ab5b276c3cf43024472eea72bd7 Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sun, 7 Jul 2024 11:55:44 +0900 Subject: [PATCH 6/7] =?UTF-8?q?:truck:=20Rename:=20Global=EC=9D=98=20Swagg?= =?UTF-8?q?er=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ticle/server/global/controller/GlobalController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ticle/server/global/controller/GlobalController.java b/src/main/java/com/ticle/server/global/controller/GlobalController.java index db58bda..7c9f1f8 100644 --- a/src/main/java/com/ticle/server/global/controller/GlobalController.java +++ b/src/main/java/com/ticle/server/global/controller/GlobalController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -@Tag(name = "GlobalController", description = "전체 설정을 위해서 필요한 api") +@Tag(name = "Global", description = "전체 설정을 위해서 필요한 api") @RestController @RequiredArgsConstructor @RequestMapping("/global") @@ -19,7 +19,7 @@ public class GlobalController { @Value("${server.env}") private String env; - @Operation(summary = "health check", description = "cicd를 위한 health check api") + @Operation(summary = "health check", description = "CI/CD를 위한 health check api") @GetMapping("/health-check") public ResponseEntity healthCheck() { return ResponseEntity From 40e82e8b0e10de252642d6847403bdee54829ab4 Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sun, 7 Jul 2024 17:33:15 +0900 Subject: [PATCH 7/7] =?UTF-8?q?:pencil2:=20Chore:=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=95=20=EC=B2=98=EB=A6=AC=EC=97=90=EC=84=9C=201=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=EB=B6=80=ED=84=B0=20=EC=8B=9C=EC=9E=91?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ticle/server/opinion/controller/OpinionController.java | 2 +- .../java/com/ticle/server/opinion/service/OpinionService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ticle/server/opinion/controller/OpinionController.java b/src/main/java/com/ticle/server/opinion/controller/OpinionController.java index 60d6da0..48b001f 100644 --- a/src/main/java/com/ticle/server/opinion/controller/OpinionController.java +++ b/src/main/java/com/ticle/server/opinion/controller/OpinionController.java @@ -77,7 +77,7 @@ public ResponseEntity> getComments( @Operation(summary = "물어봥 질문 리스트 조회", description = "물어봥 모든 질문과 함께 인기댓글 2개 조회 가능") @GetMapping() public ResponseEntity> getOpinions( - @RequestParam(defaultValue = "0") int page) { + @RequestParam(defaultValue = "1") int page) { OpinionResponseList response = opinionService.getOpinionsByPage(page); diff --git a/src/main/java/com/ticle/server/opinion/service/OpinionService.java b/src/main/java/com/ticle/server/opinion/service/OpinionService.java index 206b62b..54141f9 100644 --- a/src/main/java/com/ticle/server/opinion/service/OpinionService.java +++ b/src/main/java/com/ticle/server/opinion/service/OpinionService.java @@ -102,7 +102,7 @@ public List getCommentsByOpinion(Long opinionId, UserDetails us } public OpinionResponseList getOpinionsByPage(int page) { - Pageable pageable = PageRequest.of(page, PAGE_SIZE, getOrder(TIME)); + Pageable pageable = PageRequest.of(page - 1, PAGE_SIZE, getOrder(TIME)); Page opinionPage = opinionRepository.findAll(pageable);