Skip to content

Commit

Permalink
feat: #128 게시글 좋아요 생성시간 저장 및 조회 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kyxxgsoo committed Aug 20, 2024
1 parent 1835529 commit d7d61de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -21,21 +23,33 @@ public class BoardHeartRedisRepository implements Serializable {
private final RedisTemplate<String, String> redisTemplate;

// 게시판 좋아요 토글
public Boolean toggleBoardHeart(Long memberId, Long boardId) {
public Boolean toggleBoardHeart(Long boardId, Long memberId) {
String key = BOARD_HEART_KEY_PREFIX + boardId.toString();
String member = MEMBER_KEY_PREFIX + memberId.toString();
String createAtKey = key + ":" + member + ":createAt"; // member ID를 포함한 createAtKeyㄱ
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

//해당 key에 member가 존재하지 않으면 추가, 존재하면 삭제
if (FALSE.equals(setOperations.isMember(key, member))) {
setOperations.add(key, member);
// 현재 시간을 createAtKey로 저장
valueOperations.set(createAtKey, LocalDateTime.now().toString());
return TRUE;
} else {
setOperations.remove(key, member);
// 좋아요 취소 시 생성 시간도 삭제할 수 있음
redisTemplate.delete(createAtKey);
return FALSE;
}
}

//
public String getBoardHeartCreateAt(Long boardId, Long memberId) {
String createAtKey = BOARD_HEART_KEY_PREFIX + boardId.toString() + ":" + MEMBER_KEY_PREFIX + memberId.toString() + ":createAt";
return redisTemplate.opsForValue().get(createAtKey);
}

// 게시판 좋아요 수 조회
public Integer getBoardHeartsCount(Long boardId) {
String key = BOARD_HEART_KEY_PREFIX + boardId.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public BoardResponse.BoardToggleHeart toggleBoardHeart(Member member, Long board
Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new RestApiException(BoardErrorCode.BOARD_NOT_FOUND));

System.out.println(boardHeartRedisRepository.getBoardHeartCreateAt(board.getId(), member.getId()));

Boolean isLiked = boardHeartRedisRepository.toggleBoardHeart(member.getId(), board.getId());
return new BoardResponse.BoardToggleHeart(boardId, isLiked);
}
Expand Down

0 comments on commit d7d61de

Please sign in to comment.