Skip to content

Commit

Permalink
fix: Like 관련 람다에서 실행하는 로직 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghooni committed Aug 1, 2024
1 parent f7baf6f commit da5459d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
implementation 'redis.clients:jedis:4.0.1'
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
implementation 'com.amazonaws:aws-lambda-java-events:3.11.0'
implementation 'org.springframework.cloud:spring-cloud-function-adapter-aws:3.2.7'
implementation 'org.springframework.cloud:spring-cloud-function-adapter-aws:4.0.0'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand All @@ -67,4 +67,6 @@ task buildZip(type: Zip) {
}
archiveFileName = 'lambda-function.zip'
destinationDirectory = file("$buildDir/distributions")
}
}

build.dependsOn buildZip
18 changes: 12 additions & 6 deletions src/main/java/com/goormy/hackathon/lambda/LikeFunction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.goormy.hackathon.lambda;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.goormy.hackathon.entity.Follow;
import com.goormy.hackathon.entity.Hashtag;
import com.goormy.hackathon.entity.Like;
Expand All @@ -9,6 +10,7 @@
import com.goormy.hackathon.repository.LikeRepository;
import com.goormy.hackathon.repository.PostRepository;
import com.goormy.hackathon.repository.UserRepository;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
Expand All @@ -26,23 +28,27 @@ public class LikeFunction {
private final LikeRepository likeRepository;
private final UserRepository userRepository;
private final PostRepository postRepository;
private final ObjectMapper objectMapper;

@Bean
public Consumer<Map<String, Object>> processLike() {
return messageBody -> {
try {
// userId와 postId를 Long으로 변환
Long userId = ((Long) messageBody.get("userId"));
Long postId = ((Long) messageBody.get("postId"));
String action = (String) messageBody.get("action");
List<Map<String, Object>> records = (List<Map<String, Object>>) messageBody.get("Records");
String bodyString = (String) records.get(0).get("body");
Map<String, Object> body = objectMapper.readValue(bodyString, Map.class);

long userId = ((Number) body.get("userId")).longValue();
long postId = ((Number) body.get("postId")).longValue();
String action = (String) body.get("action");

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

if ("follow".equals(action)) {
if ("like".equals(action)) {
addLike(postId,userId);
System.out.println("좋아요 성공: " + messageBody);
} else if ("unfollow".equals(action)) {
} else if ("unlike".equals(action)) {
cancelLike(postId,userId);
System.out.println("좋아요 취소 성공: " + messageBody);
} else {
Expand Down
16 changes: 7 additions & 9 deletions src/test/java/com/goormy/hackathon/service/LikeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.goormy.hackathon.entity.Like;
import com.goormy.hackathon.entity.Post;
import com.goormy.hackathon.entity.User;
import com.goormy.hackathon.lambda.LikeFunction;
import com.goormy.hackathon.repository.LikeRedisRepository;
import com.goormy.hackathon.repository.LikeRepository;
import com.goormy.hackathon.repository.PostRepository;
import com.goormy.hackathon.repository.UserRepository;
import jakarta.persistence.PrePersist;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -16,8 +16,6 @@

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class LikeServiceTest {

Expand All @@ -28,7 +26,7 @@ class LikeServiceTest {
PostRepository postRepository;

@Autowired
LikeService likeService;
LikeFunction likeFunction;

@Autowired
LikeRepository likeRepository;
Expand Down Expand Up @@ -101,7 +99,7 @@ class LikeServiceTest {
// given

// when
likeService.addLike(2L,1L);
likeFunction.addLike(2L,1L);

}

Expand All @@ -110,7 +108,7 @@ class LikeServiceTest {
// given

// when
likeService.cancelLike(2L,1L);
likeFunction.cancelLike(2L,1L);

}

Expand All @@ -120,8 +118,8 @@ class LikeServiceTest {
// given

// when
boolean exist = likeService.findLike(4L, 3L);
boolean noExist = likeService.findLike(2L, 3L);
boolean exist = likeFunction.findLike(4L, 3L);
boolean noExist = likeFunction.findLike(2L, 3L);

// then
Assertions.assertEquals(exist, true);
Expand All @@ -138,7 +136,7 @@ class LikeServiceTest {
likeRedisRepository.update(4L, 3L, -1);

// when
likeService.dumpToDB();
likeFunction.dumpToDB();

// then
}
Expand Down

0 comments on commit da5459d

Please sign in to comment.