Skip to content

Commit

Permalink
#94 DAILYLIFE2-47 by yuk
Browse files Browse the repository at this point in the history
게시글/댓글의 좋아요 갯수를 출력

게시글 총 좋아요 갯수 tbl_heart에서 boardNum = 조회한 게시물 PK번호 테이블에서 모두 출력

댓글 총 좋아요 갯수 tbl_heart에서 replyNum = 조회한 댓글 PK번호 테이블에서 모두 출력

대댓글은 레포지티에서 댓글과 식이 겹쳐서 대댓글 entity 이름 수정 후 구현 예정
  • Loading branch information
wjdrms5815 committed Aug 21, 2022
1 parent 598c025 commit 1c62342
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,11 +26,40 @@ public ResponseEntity<Boolean> replyHeartPlus(@Valid @RequestBody HeartStateRequ
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));
}

/* @ApiOperation(value="대댓글 좋아요", notes = "replyReplyNum(tbl_replyReply)(대댓글)PK값 넘겨주시면 됩니다. 한 번 누르면 좋아요 / 2번 클릭 시 좋아요 취소")
@PostMapping("/replyReplyHeartPlus")
public ResponseEntity<Boolean> 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<Long> 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<Long> 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<Long> countHeartReplyReply(@PathVariable("replyReplyNum")Long replyReplyNum){
System.out.println("대댓글 좋아요 총 개수 출력 : "+ heartService.heartCountReplyReply(replyReplyNum));
return ResponseEntity.ok(heartService.heartCountReplyReply(replyReplyNum));
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public class HeartStateRequest {
private Long userNum;
private Long boardNum;
private Long heartState;
/* private Long replyReplyNum; //대댓글 번호*/
}
17 changes: 17 additions & 0 deletions src/main/java/com/dailylife/domain/heart/entity/Heart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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;
}*/



}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,8 +16,15 @@ public interface HeartRepository extends JpaRepository<Heart, Long> {

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);*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);*/


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +25,7 @@ public class HeartServiceImpl implements HeartService{
private final UserRepository userRepository;
private final JwtService jwtService;


/*댓글 좋아요 */
@Override
@Transactional
public boolean heartPlusReply(HeartStateRequest heartStateRequest) {
Expand All @@ -42,7 +43,7 @@ public boolean heartPlusReply(HeartStateRequest heartStateRequest) {
return true;

}

/*게시글 좋아요*/
@Override
@Transactional
public boolean heartPlusBoard(HeartStateRequest heartStateRequest) {
Expand All @@ -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;
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public ResponseEntity<Boolean> deleteReply(@PathVariable("replyNum")Long replyNu
@ApiOperation(value = "댓글 확인", notes = "게시물 댓글 확인을 위해서 게시물번호 PK (boardNum)넘겨 주시면 됩니다.")
@GetMapping("/getReply/{boardNum}")
public ResponseEntity<List<Reply>> getReply(@PathVariable("boardNum")Long boardNum){
System.out.println(replyService.getReplyList(boardNum));
return ResponseEntity.ok(replyService.getReplyList(boardNum));
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/dailylife/domain/reply/entity/Reply.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public class Reply {
@JsonIgnore
private List<Heart> hearts = new ArrayList<>();

@OneToMany(mappedBy = "reply" ) // reply이 삭제되면 자동으로 replyReply또한 삭제(대댓글 삭제)
@OneToMany(mappedBy = "reply" ) // reply이 삭제되면 자동으로 replyReply또한 삭제(대댓글 삭제)
@JsonIgnore
private List<ReplyReply> replyReplies = new ArrayList<>();



@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userNum")
private User user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class ReplyReply {
@JoinColumn(name="parentReplyNum") // 상위 댓글 replyNum
private Reply reply;

/* @OneToMany(mappedBy = "replyReply")
@JsonIgnore
private List<Heart> hearts = new ArrayList<>();*/

public static ReplyReply toEntityReplyReply(ReplyReplyInsertRequest replyInsertRequest){
return ReplyReply.builder()
.replyReplyContext(replyInsertRequest.getReplyReplyContext())
Expand Down

0 comments on commit 1c62342

Please sign in to comment.