From 619786929ec44796067ec92e2f10ab8b8175e1d9 Mon Sep 17 00:00:00 2001 From: yuk Date: Wed, 17 Aug 2022 18:58:18 +0900 Subject: [PATCH] #91 DAILYLIFE2-43 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 게시글 좋아요 구현 게시글 번호 tbl_board의 PK값(boardNum)을 사용하여 한 번 클릭 시 좋아요UP (tbl_heart의 heartNum생성 , hearCount +1) 다시 클릭 시 좋아요DOWN (tbl_heart의 heartNum삭제) --- .../dailylife/domain/board/entity/Board.java | 5 ++++ .../heart/controller/HeartController.java | 13 ++++++-- .../domain/heart/dto/HeartStateRequest.java | 7 ++--- .../dailylife/domain/heart/entity/Heart.java | 22 +++++++++++--- .../heart/repository/HeartRepository.java | 11 +++++-- .../domain/heart/service/HeartService.java | 6 ++-- .../heart/service/HeartServiceImpl.java | 30 ++++++++++++++----- .../reply/controller/ReplyController.java | 4 +-- .../dailylife/domain/reply/entity/Reply.java | 2 +- 9 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/dailylife/domain/board/entity/Board.java b/src/main/java/com/dailylife/domain/board/entity/Board.java index 5f1b844..415c543 100644 --- a/src/main/java/com/dailylife/domain/board/entity/Board.java +++ b/src/main/java/com/dailylife/domain/board/entity/Board.java @@ -2,6 +2,7 @@ import com.dailylife.domain.board.dto.BoardCreateRequest; import com.dailylife.domain.board.dto.BoardUpdateRequest; +import com.dailylife.domain.heart.entity.Heart; import com.dailylife.domain.image.entity.Image; import com.dailylife.domain.reply.entity.Reply; import com.dailylife.domain.user.entity.User; @@ -38,6 +39,10 @@ public class Board { @JsonIgnore private List images = new ArrayList<>(); + @OneToMany(mappedBy = "board" ,cascade = CascadeType.ALL) // board이(가) 삭제되면 자동으로 heart또한 삭제 + @JsonIgnore + private List hearts = new ArrayList<>(); + /* @OneToMany(mappedBy = "board") @JsonIgnore private List replies = new ArrayList<>();*/ 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 ae33d5d..26373a0 100644 --- a/src/main/java/com/dailylife/domain/heart/controller/HeartController.java +++ b/src/main/java/com/dailylife/domain/heart/controller/HeartController.java @@ -18,11 +18,18 @@ public class HeartController { private final HeartService heartService; - @ApiOperation(value = "댓글 좋아요", notes = "rno값(tbl_reply)PK값만 넘겨주시면 됩니다.") + @ApiOperation(value = "댓글 좋아요", notes = "replyNum값(tbl_reply)PK값만 넘겨주시면 됩니다. 한 번 누르면 좋아요 / 2번 클릭 시 좋아요 취소") @PostMapping("/replyHeartPlus") public ResponseEntity replyHeartPlus(@Valid @RequestBody HeartStateRequest heartStateRequest){ - System.out.println("좋아요+"); - return ResponseEntity.ok(heartService.heartPlus(heartStateRequest)); + System.out.println("댓글 좋아요"); + 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)); + } } 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 e3467fe..1a8dee9 100644 --- a/src/main/java/com/dailylife/domain/heart/dto/HeartStateRequest.java +++ b/src/main/java/com/dailylife/domain/heart/dto/HeartStateRequest.java @@ -12,9 +12,8 @@ @NoArgsConstructor @ApiModel(description = "좋아요 상태 변경을 위한 객체") public class HeartStateRequest { - private Long rno; - - private Long uno; - + private Long replyNum; + private Long userNum; + private Long boardNum; private Long heartState; } 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 2076799..fdaad51 100644 --- a/src/main/java/com/dailylife/domain/heart/entity/Heart.java +++ b/src/main/java/com/dailylife/domain/heart/entity/Heart.java @@ -1,5 +1,6 @@ package com.dailylife.domain.heart.entity; +import com.dailylife.domain.board.entity.Board; import com.dailylife.domain.heart.dto.HeartStateRequest; import com.dailylife.domain.reply.entity.Reply; @@ -22,22 +23,35 @@ public class Heart { private Long heartNum; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name="reply_replyNum") + @JoinColumn(name="replyNum") private Reply reply; - private Long uno; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="boardNum") + private Board board; + + private Long userNum; private Long heartState; - public static Heart toEntity(HeartStateRequest heartStateRequest, Reply reply){ + public static Heart toEntityReply(HeartStateRequest heartStateRequest, Reply reply){ Heart build = Heart.builder() .heartState(heartStateRequest.getHeartState()) - .uno(heartStateRequest.getUno()) + .userNum(heartStateRequest.getUserNum()) .build(); build.setReply(reply); return build; } + public static Heart toEntityBoard(HeartStateRequest heartStateRequest, Board board){ + Heart build = Heart.builder() + .heartState(heartStateRequest.getHeartState()) + .userNum(heartStateRequest.getUserNum()) + .build(); + build.setBoard(board); + 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 73d62b9..0111efb 100644 --- a/src/main/java/com/dailylife/domain/heart/repository/HeartRepository.java +++ b/src/main/java/com/dailylife/domain/heart/repository/HeartRepository.java @@ -9,7 +9,14 @@ import java.util.Optional; public interface HeartRepository extends JpaRepository { - int countByReplyReplyNumAndUno(Long rno, Long uno); - void deleteByReplyReplyNumAndUno(@Param(value="rno") Long rno, Long uno); + int countByReplyReplyNumAndUserNum(Long replyNum, Long userNum); // 댓글 좋아요 + void deleteByReplyReplyNumAndUserNum(@Param(value="replyNum") Long replyNum, Long userNum); // 댓글 좋아요 취소 + + int countByBoardBoardNumAndUserNum(Long boardNum, Long userNum); // 게시글 좋아요 + void deleteByBoardBoardNumAndUserNum(@Param(value="boardNum") Long boardNum, Long userNum); // 게시글 좋아요 취소 + +/* int countByBoardNumAndUserNum(Long boardNum, Long userNum); + + 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 af49ee9..8c6b54f 100644 --- a/src/main/java/com/dailylife/domain/heart/service/HeartService.java +++ b/src/main/java/com/dailylife/domain/heart/service/HeartService.java @@ -9,8 +9,10 @@ @Service public interface HeartService { - - boolean heartPlus(HeartStateRequest heartStateRequest); + /*댓글 좋아요 UP / 좋아요 취소*/ + boolean heartPlusReply(HeartStateRequest heartStateRequest); + /*게시글 좋아요 UP / 좋아요 취소*/ + boolean heartPlusBoard(HeartStateRequest heartStateRequest); 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 caabff5..9c41131 100644 --- a/src/main/java/com/dailylife/domain/heart/service/HeartServiceImpl.java +++ b/src/main/java/com/dailylife/domain/heart/service/HeartServiceImpl.java @@ -1,5 +1,6 @@ package com.dailylife.domain.heart.service; +import com.dailylife.domain.board.entity.Board; import com.dailylife.domain.heart.dto.HeartStateRequest; import com.dailylife.domain.heart.entity.Heart; import com.dailylife.domain.heart.repository.HeartRepository; @@ -26,21 +27,36 @@ public class HeartServiceImpl implements HeartService{ @Override @Transactional - public boolean heartPlus(HeartStateRequest heartStateRequest) { - + public boolean heartPlusReply(HeartStateRequest heartStateRequest) { Reply reply = new Reply(); - reply.setReplyNum(heartStateRequest.getRno()); + reply.setReplyNum(heartStateRequest.getReplyNum()); User user = userRepository.findByUserId(jwtService.getLoginId()); - heartStateRequest.setUno(user.getUserNum()); - if(heartRepository.countByReplyReplyNumAndUno(reply.getReplyNum(), user.getUserNum()) == 0){ + heartStateRequest.setUserNum(user.getUserNum()); + if(heartRepository.countByReplyReplyNumAndUserNum(reply.getReplyNum(), user.getUserNum()) == 0){ heartStateRequest.setHeartState(1L); - Heart heart = heartRepository.save(Heart.toEntity(heartStateRequest, reply)); + Heart heart = heartRepository.save(Heart.toEntityReply(heartStateRequest, reply)); } else { - heartRepository.deleteByReplyReplyNumAndUno(reply.getReplyNum(), user.getUserNum()); + heartRepository.deleteByReplyReplyNumAndUserNum(reply.getReplyNum(), user.getUserNum()); } return true; } + @Override + @Transactional + public boolean heartPlusBoard(HeartStateRequest heartStateRequest) { + Board board = new Board(); + board.setBoardNum(heartStateRequest.getBoardNum()); + User user = userRepository.findByUserId(jwtService.getLoginId()); + heartStateRequest.setUserNum(user.getUserNum()); + if(heartRepository.countByBoardBoardNumAndUserNum(board.getBoardNum(),user.getUserNum())==0){ + heartStateRequest.setHeartState(1L); + Heart heart = heartRepository.save(Heart.toEntityBoard(heartStateRequest, board)); + }else { + heartRepository.deleteByBoardBoardNumAndUserNum(board.getBoardNum(), user.getUserNum()); + } + return true; + } + } 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 287b326..31bb223 100644 --- a/src/main/java/com/dailylife/domain/reply/controller/ReplyController.java +++ b/src/main/java/com/dailylife/domain/reply/controller/ReplyController.java @@ -34,13 +34,13 @@ public ResponseEntity createBoard(@Valid @RequestBody ReplyInsertRequest return ResponseEntity.ok(replyService.insert(replyInsertRequest)); } - @ApiOperation(value = "댓글 삭제", notes = "댓글 삭제 완료") + @ApiOperation(value = "댓글 삭제", notes = "작성한 댓글을 삭제하기 위해서 댓글 PK (replyNum)넘겨 주시면 됩니다.") @DeleteMapping("/delete/{replyNum}") public ResponseEntity deleteReply(@PathVariable("replyNum")Long replyNum, ReplyDeleteRequest replyDeleteRequest){ return ResponseEntity.ok(replyService.delete(replyNum)); } - @ApiOperation(value = "댓글 확인", notes = "게시물 댓글 확인") + @ApiOperation(value = "댓글 확인", notes = "게시물 댓글 확인을 위해서 게시물번호 PK (boardNum)넘겨 주시면 됩니다.") @GetMapping("/getReply/{boardNum}") public ResponseEntity> getReply(@PathVariable("boardNum")Long 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 944dc16..62c4753 100644 --- a/src/main/java/com/dailylife/domain/reply/entity/Reply.java +++ b/src/main/java/com/dailylife/domain/reply/entity/Reply.java @@ -40,7 +40,7 @@ public class Reply { private Board board;*/ - @OneToMany(mappedBy = "reply" /*,cascade = CascadeType.ALL*/) // reply이 삭제되면 자동으로 heart또한 삭제 + @OneToMany(mappedBy = "reply" ,cascade = CascadeType.ALL) // reply이 삭제되면 자동으로 heart또한 삭제 @JsonIgnore private List hearts = new ArrayList<>();