Skip to content

Commit

Permalink
fix: lambda에서 실행될 함수 Bean 등록
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghooni committed Aug 1, 2024
1 parent 0eb9a0d commit 4f6d090
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/main/java/com/goormy/hackathon/lambda/LikeFunction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.goormy.hackathon.lambda;

import com.goormy.hackathon.entity.Follow;
import com.goormy.hackathon.entity.Hashtag;
import com.goormy.hackathon.entity.Like;
import com.goormy.hackathon.entity.Post;
import com.goormy.hackathon.entity.User;
Expand All @@ -9,7 +11,9 @@
import com.goormy.hackathon.repository.UserRepository;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -23,6 +27,35 @@ public class LikeFunction {
private final UserRepository userRepository;
private final PostRepository postRepository;

@Bean
public Consumer<Map<String, Object>> processLike() {

return messageBody -> {
try {
// userId와 hashtagId를 Number로 파싱하고 long으로 변환
Long userId = ((Long) messageBody.get("userId"));
Long postId = ((Long) messageBody.get("postId"));
String action = (String) messageBody.get("action");

User user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("존재하지 않는 사용자입니다. userId: " + userId));
Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("존재하지 않는 포스트입니다. postId: " + postId));

if ("like".equals(action)) {
addLike(postId,userId);
System.out.println("좋아요 성공: " + messageBody);
} else if ("unlike".equals(action)) {
cancelLike(postId,userId);
System.out.println("좋아요 취소 성공: " + messageBody);
} else {
System.out.println("존재하지 않는 action입니다 : " + action);
}
} catch (Exception e) {
System.err.println("메시지 전송 실패: " + messageBody);
e.printStackTrace();
}
};
}

/**
* @description 좋아요 정보를 Redis 캐시에 업데이트
* */
Expand All @@ -33,7 +66,7 @@ public void addLike(Long postId, Long userId) {

if (findPostLike == null) {
// 캐시에 좋아요에 대한 정보가 없다면,
// Key = postlike:{postId}, Field = {userId}, Value = 1 로 '좋아요' 정보 생성
// Key = postlike:{postId}, Field = {userId}, Value = 1 로 '좋아요' 정보 생성w
likeRedisRepository.update(postId, userId, 1);
}else if (findPostLike == -1) {
// '좋아요 취소' 정보가 있는 상태라면
Expand Down

0 comments on commit 4f6d090

Please sign in to comment.