From 1c62342ad657b0e5d741e37fde68531c060359de Mon Sep 17 00:00:00 2001 From: yuk Date: Sun, 21 Aug 2022 18:02:05 +0900 Subject: [PATCH] #94 DAILYLIFE2-47 by yuk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 게시글/댓글의 좋아요 갯수를 출력 게시글 총 좋아요 갯수 tbl_heart에서 boardNum = 조회한 게시물 PK번호 테이블에서 모두 출력 댓글 총 좋아요 갯수 tbl_heart에서 replyNum = 조회한 댓글 PK번호 테이블에서 모두 출력 대댓글은 레포지티에서 댓글과 식이 겹쳐서 대댓글 entity 이름 수정 후 구현 예정 --- .../heart/controller/HeartController.java | 32 ++++++++++++- .../domain/heart/dto/HeartStateRequest.java | 1 + .../dailylife/domain/heart/entity/Heart.java | 17 +++++++ .../heart/repository/HeartRepository.java | 12 ++++- .../domain/heart/service/HeartService.java | 7 +++ .../heart/service/HeartServiceImpl.java | 47 +++++++++++++++++-- .../reply/controller/ReplyController.java | 1 + .../dailylife/domain/reply/entity/Reply.java | 4 +- .../domain/replyReply/entity/ReplyReply.java | 4 ++ 9 files changed, 118 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/dailylife/domain/heart/controller/HeartController.java b/src/main/java/com/dailylife/domain/heart/controller/HeartController.java index 26373a0..ca52f8e 100644 --- a/src/main/java/com/dailylife/domain/heart/controller/HeartController.java +++ b/src/main/java/com/dailylife/domain/heart/controller/HeartController.java @@ -6,6 +6,7 @@ import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.parameters.P; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @@ -25,11 +26,40 @@ public ResponseEntity replyHeartPlus(@Valid @RequestBody HeartStateRequ return ResponseEntity.ok(heartService.heartPlusReply(heartStateRequest)); } - @ApiOperation(value="게시글 좋아요", notes = "boardNum값(tbl_board)PK값 넘겨주시면 됩니다. 한 번 누르면 좋아요 / 2번 클릭 시 좋아요 취소") @PostMapping("/boardHeartPlus") public ResponseEntity boardHeartPlus(@Valid @RequestBody HeartStateRequest heartStateRequest){ System.out.println("게시글 좋아요"); return ResponseEntity.ok(heartService.heartPlusBoard(heartStateRequest)); } + +/* @ApiOperation(value="대댓글 좋아요", notes = "replyReplyNum(tbl_replyReply)(대댓글)PK값 넘겨주시면 됩니다. 한 번 누르면 좋아요 / 2번 클릭 시 좋아요 취소") + @PostMapping("/replyReplyHeartPlus") + public ResponseEntity replyReplyHeartPlus(@Valid @RequestBody HeartStateRequest heartStateRequest){ + System.out.println("대댓글 좋아요"); + return ResponseEntity.ok(heartService.heartPlusReplyReply(heartStateRequest)); + }*/ + + @ApiOperation(value = "댓글 좋아요 개수 출력", notes = "replyNum값(tbl_reply)PK값만 넘겨주시면 됩니다.") + @GetMapping("/countHeartReply/{replyNum}") + public ResponseEntity countHeartReply(@PathVariable("replyNum")Long replyNum){ + System.out.println("댓글 좋아요 총 개수 출력 : "+ heartService.heartCountReply(replyNum)); + return ResponseEntity.ok(heartService.heartCountReply(replyNum)); + + } + + @ApiOperation(value = "게시글 좋아요 개수 출력", notes = "boardNum(tbl_board)PK값만 넘겨주시면 됩니다") + @GetMapping("/countHeartBoard/{boardNum}") + public ResponseEntity countHeartBoard(@PathVariable("boardNum")Long boardNum){ + System.out.println("게시물 좋아요 총 개수 출력 : "+heartService.heartCountBoard(boardNum)); + return ResponseEntity.ok(heartService.heartCountBoard(boardNum)); + } + +/* @ApiOperation(value = "대댓글 좋아요 개수 출력", notes = "replyNum값(tbl_reply)PK값만 넘겨주시면 됩니다.") + @GetMapping("/countHeartReplyReply/{replyReplyNum}") + public ResponseEntity countHeartReplyReply(@PathVariable("replyReplyNum")Long replyReplyNum){ + System.out.println("대댓글 좋아요 총 개수 출력 : "+ heartService.heartCountReplyReply(replyReplyNum)); + return ResponseEntity.ok(heartService.heartCountReplyReply(replyReplyNum)); + + }*/ } diff --git a/src/main/java/com/dailylife/domain/heart/dto/HeartStateRequest.java b/src/main/java/com/dailylife/domain/heart/dto/HeartStateRequest.java index 1a8dee9..25943d5 100644 --- a/src/main/java/com/dailylife/domain/heart/dto/HeartStateRequest.java +++ b/src/main/java/com/dailylife/domain/heart/dto/HeartStateRequest.java @@ -16,4 +16,5 @@ public class HeartStateRequest { private Long userNum; private Long boardNum; private Long heartState; +/* private Long replyReplyNum; //대댓글 번호*/ } diff --git a/src/main/java/com/dailylife/domain/heart/entity/Heart.java b/src/main/java/com/dailylife/domain/heart/entity/Heart.java index fdaad51..b447973 100644 --- a/src/main/java/com/dailylife/domain/heart/entity/Heart.java +++ b/src/main/java/com/dailylife/domain/heart/entity/Heart.java @@ -4,6 +4,7 @@ import com.dailylife.domain.heart.dto.HeartStateRequest; import com.dailylife.domain.reply.entity.Reply; +import com.dailylife.domain.replyReply.entity.ReplyReply; import com.dailylife.domain.user.entity.User; import lombok.*; @@ -30,10 +31,15 @@ public class Heart { @JoinColumn(name="boardNum") private Board board; +/* @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="replyReplyNum") + private ReplyReply replyReply;*/ + private Long userNum; private Long heartState; + //댓글 좋아요 public static Heart toEntityReply(HeartStateRequest heartStateRequest, Reply reply){ Heart build = Heart.builder() .heartState(heartStateRequest.getHeartState()) @@ -43,6 +49,7 @@ public static Heart toEntityReply(HeartStateRequest heartStateRequest, Reply rep return build; } + //게시글 좋아요 public static Heart toEntityBoard(HeartStateRequest heartStateRequest, Board board){ Heart build = Heart.builder() .heartState(heartStateRequest.getHeartState()) @@ -52,6 +59,16 @@ public static Heart toEntityBoard(HeartStateRequest heartStateRequest, Board boa return build; } + //대댓글 좋아요 +/* public static Heart toEntityReplyReply(HeartStateRequest heartStateRequest, ReplyReply replyReply){ + Heart build = Heart.builder() + .heartState(heartStateRequest.getHeartState()) + .userNum(heartStateRequest.getUserNum()) + .build(); + build.setReplyReply(replyReply); + return build; + }*/ + } diff --git a/src/main/java/com/dailylife/domain/heart/repository/HeartRepository.java b/src/main/java/com/dailylife/domain/heart/repository/HeartRepository.java index 0111efb..c89eaef 100644 --- a/src/main/java/com/dailylife/domain/heart/repository/HeartRepository.java +++ b/src/main/java/com/dailylife/domain/heart/repository/HeartRepository.java @@ -4,6 +4,7 @@ import com.dailylife.domain.reply.entity.Reply; import lombok.Value; 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; @@ -15,8 +16,15 @@ public interface HeartRepository extends JpaRepository { int countByBoardBoardNumAndUserNum(Long boardNum, Long userNum); // 게시글 좋아요 void deleteByBoardBoardNumAndUserNum(@Param(value="boardNum") Long boardNum, Long userNum); // 게시글 좋아요 취소 + + /* int countByReplyReplyReplyReplyNumAndUserNum(Long replyReplyNum, Long userNum); // 대댓글 좋아요 + + void deleteByReplyReplyReplyReplyNumAndUserNum(@Param(value="replyReplyNum") Long replyReplyNum, Long userNum); // 대댓글 좋아요 취소*/ -/* int countByBoardNumAndUserNum(Long boardNum, Long userNum); + Long countByReplyReplyNum(Long replyNum); // 댓글 좋아요 총 갯수 출력 + + Long countByBoardBoardNum(Long boardNum); // 게시글 좋아요 총 갯수 출력 + + /*Long countByReplyReplyReplyReplyNum(Long replyReplyNum); // 대댓글 좋아요 총 개수 출력*/ - void deleteByBoardNumAndUserNum(@Param(value = "boardNum") Long boardNum, Long userNum);*/ } diff --git a/src/main/java/com/dailylife/domain/heart/service/HeartService.java b/src/main/java/com/dailylife/domain/heart/service/HeartService.java index 8c6b54f..010e2de 100644 --- a/src/main/java/com/dailylife/domain/heart/service/HeartService.java +++ b/src/main/java/com/dailylife/domain/heart/service/HeartService.java @@ -14,6 +14,13 @@ public interface HeartService { /*게시글 좋아요 UP / 좋아요 취소*/ boolean heartPlusBoard(HeartStateRequest heartStateRequest); +/* boolean heartPlusReplyReply(HeartStateRequest heartStateRequest);*/ + + Long heartCountReply(Long replyNum); + + Long heartCountBoard(Long boardNum); + +/* Long heartCountReplyReply(Long replyReplyNum);*/ } diff --git a/src/main/java/com/dailylife/domain/heart/service/HeartServiceImpl.java b/src/main/java/com/dailylife/domain/heart/service/HeartServiceImpl.java index 9c41131..a67d3a0 100644 --- a/src/main/java/com/dailylife/domain/heart/service/HeartServiceImpl.java +++ b/src/main/java/com/dailylife/domain/heart/service/HeartServiceImpl.java @@ -5,6 +5,7 @@ import com.dailylife.domain.heart.entity.Heart; import com.dailylife.domain.heart.repository.HeartRepository; import com.dailylife.domain.reply.entity.Reply; +import com.dailylife.domain.replyReply.entity.ReplyReply; import com.dailylife.domain.user.entity.User; import com.dailylife.domain.user.repository.UserRepository; import com.dailylife.global.jwt.service.JwtService; @@ -24,7 +25,7 @@ public class HeartServiceImpl implements HeartService{ private final UserRepository userRepository; private final JwtService jwtService; - + /*댓글 좋아요 */ @Override @Transactional public boolean heartPlusReply(HeartStateRequest heartStateRequest) { @@ -42,7 +43,7 @@ public boolean heartPlusReply(HeartStateRequest heartStateRequest) { return true; } - + /*게시글 좋아요*/ @Override @Transactional public boolean heartPlusBoard(HeartStateRequest heartStateRequest) { @@ -52,11 +53,51 @@ public boolean heartPlusBoard(HeartStateRequest heartStateRequest) { heartStateRequest.setUserNum(user.getUserNum()); if(heartRepository.countByBoardBoardNumAndUserNum(board.getBoardNum(),user.getUserNum())==0){ heartStateRequest.setHeartState(1L); - Heart heart = heartRepository.save(Heart.toEntityBoard(heartStateRequest, board)); + Heart heart = heartRepository.save(Heart.toEntityBoard(heartStateRequest,board)); }else { heartRepository.deleteByBoardBoardNumAndUserNum(board.getBoardNum(), user.getUserNum()); } return true; } + /*대댓글 좋아요*//* + @Override + @Transactional + public boolean heartPlusReplyReply(HeartStateRequest heartStateRequest) { + ReplyReply replyReply = new ReplyReply(); + replyReply.setReplyReplyNum(heartStateRequest.getReplyReplyNum()); + User user = userRepository.findByUserId(jwtService.getLoginId()); + heartStateRequest.setUserNum(user.getUserNum()); + if(heartRepository.countByReplyReplyReplyReplyNumAndUserNum(replyReply.getReplyReplyNum(),user.getUserNum())==0){ + heartStateRequest.setHeartState(1L); + Heart heart = heartRepository.save(Heart.toEntityReplyReply(heartStateRequest,replyReply)); + }else{ + heartRepository.deleteByReplyReplyReplyReplyNumAndUserNum(replyReply.getReplyReplyNum(),user.getUserNum()); + } + return true; + }*/ + + /*댓글 좋아요 총 개수 출력*/ + @Override + @Transactional + public Long heartCountReply(Long replyNum) { + Long heartCount = heartRepository.countByReplyReplyNum(replyNum); + return heartCount; + } + /*게시글 좋아요 총 개수 출력*/ + @Override + @Transactional + public Long heartCountBoard(Long boardNum) { + Long heartCount = heartRepository.countByBoardBoardNum(boardNum); + return heartCount; + } +/* + *//*대댓글 좋아요 총 개수 출력*//* + @Override + @Transactional + public Long heartCountReplyReply(Long replyReplyNum) { + Long heartCount = heartRepository.countByReplyReplyReplyReplyNum(replyReplyNum); + return heartCount; + }*/ + } diff --git a/src/main/java/com/dailylife/domain/reply/controller/ReplyController.java b/src/main/java/com/dailylife/domain/reply/controller/ReplyController.java index 31bb223..344a558 100644 --- a/src/main/java/com/dailylife/domain/reply/controller/ReplyController.java +++ b/src/main/java/com/dailylife/domain/reply/controller/ReplyController.java @@ -43,6 +43,7 @@ public ResponseEntity deleteReply(@PathVariable("replyNum")Long replyNu @ApiOperation(value = "댓글 확인", notes = "게시물 댓글 확인을 위해서 게시물번호 PK (boardNum)넘겨 주시면 됩니다.") @GetMapping("/getReply/{boardNum}") public ResponseEntity> getReply(@PathVariable("boardNum")Long boardNum){ + System.out.println(replyService.getReplyList(boardNum)); return ResponseEntity.ok(replyService.getReplyList(boardNum)); } diff --git a/src/main/java/com/dailylife/domain/reply/entity/Reply.java b/src/main/java/com/dailylife/domain/reply/entity/Reply.java index 62c4753..a5e0661 100644 --- a/src/main/java/com/dailylife/domain/reply/entity/Reply.java +++ b/src/main/java/com/dailylife/domain/reply/entity/Reply.java @@ -44,10 +44,12 @@ public class Reply { @JsonIgnore private List hearts = new ArrayList<>(); - @OneToMany(mappedBy = "reply" ) // reply이 삭제되면 자동으로 replyReply또한 삭제(대댓글 삭제) + @OneToMany(mappedBy = "reply" ) // reply이 삭제되면 자동으로 replyReply또한 삭제(대댓글 삭제) @JsonIgnore private List replyReplies = new ArrayList<>(); + + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="userNum") private User user; diff --git a/src/main/java/com/dailylife/domain/replyReply/entity/ReplyReply.java b/src/main/java/com/dailylife/domain/replyReply/entity/ReplyReply.java index a72f269..ebf6193 100644 --- a/src/main/java/com/dailylife/domain/replyReply/entity/ReplyReply.java +++ b/src/main/java/com/dailylife/domain/replyReply/entity/ReplyReply.java @@ -47,6 +47,10 @@ public class ReplyReply { @JoinColumn(name="parentReplyNum") // 상위 댓글 replyNum private Reply reply; +/* @OneToMany(mappedBy = "replyReply") + @JsonIgnore + private List hearts = new ArrayList<>();*/ + public static ReplyReply toEntityReplyReply(ReplyReplyInsertRequest replyInsertRequest){ return ReplyReply.builder() .replyReplyContext(replyInsertRequest.getReplyReplyContext())