Skip to content

Commit

Permalink
#91 DAILYLIFE2-43
Browse files Browse the repository at this point in the history
게시글 좋아요 구현
게시글 번호 tbl_board의 PK값(boardNum)을 사용하여
한 번 클릭 시 좋아요UP (tbl_heart의 heartNum생성 , hearCount +1)
다시 클릭 시 좋아요DOWN (tbl_heart의 heartNum삭제)
  • Loading branch information
wjdrms5815 committed Aug 17, 2022
1 parent 6118891 commit 6197869
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/dailylife/domain/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +39,10 @@ public class Board {
@JsonIgnore
private List<Image> images = new ArrayList<>();

@OneToMany(mappedBy = "board" ,cascade = CascadeType.ALL) // board이(가) 삭제되면 자동으로 heart또한 삭제
@JsonIgnore
private List<Heart> hearts = new ArrayList<>();

/* @OneToMany(mappedBy = "board")
@JsonIgnore
private List<ReplyReply> replies = new ArrayList<>();*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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<Boolean> boardHeartPlus(@Valid @RequestBody HeartStateRequest heartStateRequest){
System.out.println("게시글 좋아요");
return ResponseEntity.ok(heartService.heartPlusBoard(heartStateRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
22 changes: 18 additions & 4 deletions src/main/java/com/dailylife/domain/heart/entity/Heart.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import java.util.Optional;

public interface HeartRepository extends JpaRepository<Heart, Long> {
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);*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

@Service
public interface HeartService {

boolean heartPlus(HeartStateRequest heartStateRequest);
/*댓글 좋아요 UP / 좋아요 취소*/
boolean heartPlusReply(HeartStateRequest heartStateRequest);
/*게시글 좋아요 UP / 좋아요 취소*/
boolean heartPlusBoard(HeartStateRequest heartStateRequest);



Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public ResponseEntity<Reply> createBoard(@Valid @RequestBody ReplyInsertRequest
return ResponseEntity.ok(replyService.insert(replyInsertRequest));
}

@ApiOperation(value = "댓글 삭제", notes = "댓글 삭제 완료")
@ApiOperation(value = "댓글 삭제", notes = "작성한 댓글을 삭제하기 위해서 댓글 PK (replyNum)넘겨 주시면 됩니다.")
@DeleteMapping("/delete/{replyNum}")
public ResponseEntity<Boolean> 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<List<Reply>> getReply(@PathVariable("boardNum")Long boardNum){
return ResponseEntity.ok(replyService.getReplyList(boardNum));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dailylife/domain/reply/entity/Reply.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Heart> hearts = new ArrayList<>();

Expand Down

0 comments on commit 6197869

Please sign in to comment.