Skip to content

Commit

Permalink
feat: view like
Browse files Browse the repository at this point in the history
  • Loading branch information
doxxx93 committed Feb 22, 2024
1 parent 4bdc926 commit 8722d5c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.palette.easeltimelineservice.service;

import lombok.RequiredArgsConstructor;
import org.palette.easeltimelineservice.util.RedisKeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static org.palette.easeltimelineservice.service.RedisKeyConstants.LIKED_PAINT_PREFIX;


@Service
@RequiredArgsConstructor
public class PaintLikeService {

private final RedisTemplate<String, String> redisTemplate;

public boolean isLiked(Long userId, Long paintId) {
String key = RedisKeyUtil.constructKey(LIKED_PAINT_PREFIX.getKey(), paintId);
return Optional.ofNullable(redisTemplate.opsForSet().isMember(key, userId.toString())).orElse(false);
}

public void like(Long userId, Long paintId) {
String key = RedisKeyUtil.constructKey(LIKED_PAINT_PREFIX.getKey(), paintId);
redisTemplate.opsForSet().add(key, userId.toString());
}

public void unlike(Long userId, Long paintId) {
String key = RedisKeyUtil.constructKey(LIKED_PAINT_PREFIX.getKey(), paintId);
redisTemplate.opsForSet().remove(key, userId.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ public void incrementViewCount(final Long aLong) {
}

private void changeMetricCount(final Long pid, final String metric, final int delta) {
redistemplate.opsForHash()
.increment(
RedisKeyUtil.constructKey(RedisKeyConstants.METRICS_PREFIX.getKey(), pid),
metric,
delta
);
String key = RedisKeyUtil.constructKey(RedisKeyConstants.METRICS_PREFIX.getKey(), metric, pid);
redistemplate.opsForValue().increment(key, delta);
}

public PaintMetrics getPaintMetrics(final Long pid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static PaintResponse of(Paint paint, PaintMetrics metrics) {
metrics.replyCount(),
metrics.repaintCount(),
metrics.likeCount(),
false,
isLiked,
false,
false,
metrics.viewCount(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
public enum RedisKeyConstants {
FOLLOWER_PAINT_TIMELINE_PREFIX("follow_timeline"),
PAINT_PREFIX("paint"),
METRICS_PREFIX("metrics");
METRICS_PREFIX("metrics"),
LIKED_PAINT_PREFIX("liked_paint");

private final String key;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class TimelineUsecase {
private final FollowerPaintMapService followerPaintMapService;
private final PaintCacheService paintCacheService;
private final PaintMetricsService paintMetricsService;
private final PaintLikeService paintLikeService;

public void handlePaintCreatedEvent(PaintCreatedEvent paintCreatedEvent) {
final GFollowerIdsResponse followerIds = gRPCSocialClient.getFollowerIds(paintCreatedEvent.authorId());
Expand All @@ -37,16 +38,20 @@ public List<PaintResponse> getFollowingTimeline(final Long userId) {
final List<Paint> paints = paintCacheService.getPaints(paintIds);
return paints.stream().map(paint -> {
PaintMetrics metrics = paintMetricsService.getPaintMetrics(paint.getId());
return PaintResponse.of(paint, metrics);
final boolean liked = paintLikeService.isLiked(userId, paint.getId());
return PaintResponse.of(paint, metrics, liked);
}
).toList();
}


public void handleLikedPaintEvent(final LikedPaintEvent likedPaintEvent) {
paintLikeService.like(likedPaintEvent.likingUserId(), likedPaintEvent.paintId());
paintMetricsService.incrementLikeCount(likedPaintEvent.paintId());
}

public void handleUnlikedPaintEvent(final UnlikedPaintEvent unlikedPaintEvent) {
paintLikeService.unlike(unlikedPaintEvent.unlikingUserId(), unlikedPaintEvent.paintId());
paintMetricsService.decrementLikeCount(unlikedPaintEvent.paintId());
}

Expand All @@ -61,7 +66,8 @@ public List<PaintResponse> getForYouTimeline(final Long userId) {

return filteredPaints.stream().map(paint -> {
PaintMetrics metrics = paintMetricsService.getPaintMetrics(paint.getId());
return PaintResponse.of(paint, metrics);
final boolean liked = paintLikeService.isLiked(userId, paint.getId());
return PaintResponse.of(paint, metrics, liked);
}
).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public static String constructKey(String prefix, Long id) {
return prefix + ":" + id;
}

public static String constructKey(String prefix, String metric, Long id) {
return prefix + ":" + metric + ":" + id;
}

public static List<String> constructKeys(final String prefix, final List<Long> ids) {
return ids.stream()
.map(paintId -> constructKey(prefix, paintId))
Expand Down

0 comments on commit 8722d5c

Please sign in to comment.