Skip to content

Commit

Permalink
refactor: 메서드명 변경 및 LikeInfo dto 패키지로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
mcodnjs committed Jan 31, 2024
1 parent 4a62d4c commit babb205
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import hanglog.community.dto.response.CommunityTripResponse;
import hanglog.community.dto.response.RecommendTripListResponse;
import hanglog.global.exception.BadRequestException;
import hanglog.like.domain.LikeInfo;
import hanglog.like.dto.LikeInfo;
import hanglog.like.domain.repository.CustomLikeRepository;
import hanglog.like.dto.LikeElement;
import hanglog.like.dto.LikeElements;
Expand Down Expand Up @@ -118,7 +118,7 @@ private Map<Long, LikeInfo> getLikeInfoByTripIds(final Long memberId, final List
if (!nonCachedTripIds.isEmpty()) {
final List<LikeElement> likeElements = customLikeRepository.findLikeElementByTripIds(nonCachedTripIds);
likeElements.addAll(getEmptyLikeElements(likeElements, nonCachedTripIds));
likeElements.forEach(this::cachingLike);
likeElements.forEach(this::storeLikeInCache);
likeInfoByTrip.putAll(new LikeElements(likeElements).toLikeInfo(memberId));
}
return likeInfoByTrip;
Expand Down Expand Up @@ -169,8 +169,8 @@ private LikeInfo getLikeInfoByTripId(final Long memberId, final Long tripId) {

final LikeElement likeElement = customLikeRepository.findLikesElementByTripId(tripId)
.orElse(LikeElement.empty(tripId));
cachingLike(likeElement);
return likeElement.toLikeInfo(memberId);
storeLikeInCache(likeElement);
return new LikeInfo(likeElement.getLikeCount(), likeElement.isLike(memberId));
}

private LikeInfo readLikeInfoFromCache(final String key, final Long memberId) {
Expand All @@ -180,7 +180,7 @@ private LikeInfo readLikeInfoFromCache(final String key, final Long memberId) {
return new LikeInfo(count, isLike);
}

private void cachingLike(final LikeElement likeElement) {
private void storeLikeInCache(final LikeElement likeElement) {
final SetOperations<String, Object> opsForSet = redisTemplate.opsForSet();
final String key = generateLikeKey(likeElement.getTripId());
opsForSet.add(key, EMPTY_MARKER);
Expand Down
5 changes: 0 additions & 5 deletions backend/src/main/java/hanglog/like/dto/LikeElement.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hanglog.like.dto;

import hanglog.like.domain.LikeInfo;
import java.util.Collections;
import java.util.Set;
import lombok.AllArgsConstructor;
Expand All @@ -18,10 +17,6 @@ public boolean isLike(final Long memberId) {
return memberIds.contains(memberId);
}

public LikeInfo toLikeInfo(final Long memberId) {
return new LikeInfo(this.getLikeCount(), this.isLike(memberId));
}

public static LikeElement empty(final Long tripId) {
return new LikeElement(tripId, 0, Collections.emptySet());
}
Expand Down
1 change: 0 additions & 1 deletion backend/src/main/java/hanglog/like/dto/LikeElements.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hanglog.like.dto;

import hanglog.like.domain.LikeInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hanglog.like.domain;
package hanglog.like.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
16 changes: 8 additions & 8 deletions backend/src/main/java/hanglog/like/service/LikeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public void update(final Long memberId, final Long tripId, final LikeRequest lik
if (FALSE.equals(redisTemplate.hasKey(key))) {
final LikeElement likeElement = customLikeRepository.findLikesElementByTripId(tripId)
.orElse(LikeElement.empty(tripId));
cachingLike(likeElement);
storeLikeInCache(likeElement);
}
update(key, memberId, likeRequest.getIsLike());
updateToCache(key, memberId, likeRequest.getIsLike());
}

private void cachingLike(final LikeElement likeElement) {
private void storeLikeInCache(final LikeElement likeElement) {
final SetOperations<String, Object> opsForSet = redisTemplate.opsForSet();
final String key = generateLikeKey(likeElement.getTripId());
opsForSet.add(key, EMPTY_MARKER);
Expand All @@ -44,21 +44,21 @@ private void cachingLike(final LikeElement likeElement) {
redisTemplate.expire(key, LIKE_TTL);
}

private void update(final String key, final Long memberId, final Boolean isLike) {
private void updateToCache(final String key, final Long memberId, final Boolean isLike) {
if (isLike) {
addMemberInLike(key, memberId);
addMember(key, memberId);
return;
}
removeMemberInLike(key, memberId);
removeMember(key, memberId);
}

private void addMemberInLike(final String key, final Long memberId) {
private void addMember(final String key, final Long memberId) {
final SetOperations<String, Object> opsForSet = redisTemplate.opsForSet();
opsForSet.add(key, memberId);
redisTemplate.expire(key, LIKE_TTL);
}

private void removeMemberInLike(final String key, final Long memberId) {
private void removeMember(final String key, final Long memberId) {
final SetOperations<String, Object> opsForSet = redisTemplate.opsForSet();
opsForSet.remove(key, memberId);
redisTemplate.expire(key, LIKE_TTL);
Expand Down

0 comments on commit babb205

Please sign in to comment.