-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from dlrkdus/d#6
Feat/follow service#6
- Loading branch information
Showing
17 changed files
with
348 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.goormy.hackathon.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.sqs.SqsClient; | ||
|
||
@Configuration | ||
public class AwsConfig { | ||
|
||
|
||
@Value("${spring.cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${spring.cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${spring.cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public SqsClient sqsClient() { | ||
return SqsClient.builder() | ||
.region(Region.of(region)) | ||
.credentialsProvider(StaticCredentialsProvider.create( | ||
AwsBasicCredentials.create(accessKey, secretKey))) | ||
.build(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/goormy/hackathon/controller/FollowController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.goormy.hackathon.controller; | ||
|
||
import com.goormy.hackathon.service.FollowService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/goormy") | ||
public class FollowController { | ||
|
||
@Autowired | ||
private FollowService followService; | ||
|
||
@PostMapping("/follow") | ||
public ResponseEntity<String> follow(@RequestHeader long userId, @RequestParam long hashtagId) { | ||
followService.sendFollowRequest(userId,hashtagId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@PostMapping("/unfollow") | ||
public ResponseEntity<String> unfollow(@RequestHeader long userId, @RequestParam long hashtagId) { | ||
followService.sendUnfollowRequest(userId,hashtagId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/goormy/hackathon/handler/FollowHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.goormy.hackathon.handler; | ||
|
||
import org.springframework.cloud.function.adapter.aws.FunctionInvoker; | ||
|
||
public class FollowHandler extends FunctionInvoker { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/goormy/hackathon/handler/ScheduledHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.goormy.hackathon.handler; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent; | ||
import com.goormy.hackathon.lambda.FollowFunction; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
public class ScheduledHandler implements RequestHandler<ScheduledEvent, String> { | ||
|
||
private static final ApplicationContext context = new AnnotationConfigApplicationContext(FollowFunction.class); | ||
private final FollowFunction followFunction; | ||
|
||
public ScheduledHandler() { | ||
followFunction = context.getBean(FollowFunction.class); | ||
} | ||
|
||
@Override | ||
public String handleRequest(ScheduledEvent event, Context context) { | ||
followFunction.migrateData(); | ||
return "Data migration completed"; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/goormy/hackathon/lambda/FollowFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.goormy.hackathon.lambda; | ||
|
||
import com.goormy.hackathon.entity.Follow; | ||
import com.goormy.hackathon.entity.Hashtag; | ||
import com.goormy.hackathon.entity.User; | ||
import com.goormy.hackathon.repository.FollowRedisRepository; | ||
import com.goormy.hackathon.repository.FollowRepository; | ||
import com.goormy.hackathon.repository.HashtagRepository; | ||
import com.goormy.hackathon.repository.UserRepository; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
@Configuration | ||
public class FollowFunction{ | ||
|
||
private final FollowRepository followRepository; | ||
private final UserRepository userRepository; | ||
private final HashtagRepository hashtagRepository; | ||
private final FollowRedisRepository followRedisRepository; | ||
|
||
public FollowFunction(FollowRepository followRepository, UserRepository userRepository, HashtagRepository hashtagRepository, FollowRedisRepository followRedisRepository) { | ||
this.followRepository = followRepository; | ||
this.userRepository = userRepository; | ||
this.hashtagRepository = hashtagRepository; | ||
this.followRedisRepository = followRedisRepository; | ||
} | ||
|
||
@Bean | ||
public Consumer<Map<String, Object>> processFollow() { | ||
return messageBody -> { | ||
try { | ||
// userId와 hashtagId를 Number로 파싱하고 long으로 변환 | ||
long userId = ((Number) messageBody.get("userId")).longValue(); | ||
long hashtagId = ((Number) messageBody.get("hashtagId")).longValue(); | ||
String action = (String) messageBody.get("action"); | ||
|
||
User user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("존재하지 않는 사용자입니다. userId: " + userId)); | ||
Hashtag hashtag = hashtagRepository.findById(hashtagId).orElseThrow(() -> new RuntimeException("존재하지 않는 해시태그입니다. hashtagId: " + hashtagId)); | ||
|
||
if ("follow".equals(action)) { | ||
Follow follow = new Follow(user,hashtag); | ||
followRedisRepository.insertFollow(hashtagId, userId); | ||
System.out.println("팔로우 성공: " + messageBody); | ||
} else if ("unfollow".equals(action)) { | ||
Follow follow = followRepository.findByUserIdAndHashTagId(userId, hashtagId) | ||
.orElseThrow(() -> new RuntimeException("존재하지 않는 팔로우입니다. userId: " + userId + " hashtagId: " + hashtagId)); | ||
followRedisRepository.removeFollow(hashtagId, userId); | ||
System.out.println("팔로우 취소 성공: " + messageBody); | ||
} else { | ||
System.out.println("존재하지 않는 action입니다 : " + action); | ||
} | ||
} catch (Exception e) { | ||
System.err.println("메시지 전송 실패: " + messageBody); | ||
e.printStackTrace(); | ||
} | ||
}; | ||
} | ||
|
||
// Redis 데이터를 RDBMS에 저장하고 Redis 비우기 | ||
public void migrateData() { | ||
// Redis에서 모든 팔로우 데이터 가져오기 | ||
List<Follow> follows = followRedisRepository.getAllFollows(); | ||
|
||
// RDBMS에 배치 저장 | ||
followRepository.deleteAll(); | ||
followRepository.saveAll(follows); | ||
|
||
// Redis 비우기 | ||
System.out.println("Redis 데이터를 RDBMS로 옮기고 Redis를 초기화했습니다."); | ||
} | ||
|
||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/goormy/hackathon/repository/FollowRedisRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.goormy.hackathon.repository; | ||
|
||
import com.goormy.hackathon.entity.Follow; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Repository | ||
public class FollowRedisRepository { | ||
|
||
@Autowired | ||
RedisTemplate<String, Object> redisTemplate; | ||
|
||
@Autowired | ||
FollowRepository followRepository; | ||
|
||
public void insertFollow(Long hashtagId, Long userId) { | ||
String key = "hashtagId: " + hashtagId.toString(); | ||
redisTemplate.opsForList().rightPush(key, userId); | ||
} | ||
|
||
public void removeFollow(Long hashtagId, Long userId) { | ||
String key = "hashtagId:" + hashtagId.toString(); | ||
redisTemplate.opsForList().remove(key, 0, userId); | ||
} | ||
|
||
public List<Follow> getAllFollows() { | ||
// Redis에서 데이터를 가져와 RDBMS로 마이그레이션 | ||
List<Follow> follows = new ArrayList<>(); | ||
|
||
// 모든 키 가져오기 | ||
Set<String> keys = redisTemplate.keys("hashtagId:*"); | ||
|
||
if (keys != null) { | ||
for (String key : keys) { | ||
List<Long> userIds = (List<Long>) (List<?>) redisTemplate.opsForList().range(key, 0, -1); | ||
if (userIds != null) { | ||
for (Long userId : userIds) { | ||
Long hashtagId = Long.parseLong(key.split(":")[1]); | ||
Follow follow = followRepository.findByUserIdAndHashTagId(userId,hashtagId) | ||
.orElseThrow(() -> new RuntimeException("존재하지 않는 팔로우입니다. userId: " + userId + " hashtagId: " + hashtagId)); | ||
follows.add(follow); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return follows; | ||
} | ||
|
||
|
||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/goormy/hackathon/repository/FollowRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.goormy.hackathon.repository; | ||
|
||
import com.goormy.hackathon.entity.Follow; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface FollowRepository extends JpaRepository<Follow, Long> { | ||
|
||
@Query("SELECT f FROM Follow f WHERE f.user.id = :userId AND f.hashtag.id = :hashtagId") | ||
Optional<Follow> findByUserIdAndHashTagId(@Param("userId") Long userId, @Param("hashtagId") Long hashtagId); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/goormy/hackathon/repository/HashtagRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.goormy.hackathon.repository; | ||
|
||
import com.goormy.hackathon.entity.Hashtag; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface HashtagRepository extends JpaRepository<Hashtag, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/goormy/hackathon/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.goormy.hackathon.repository; | ||
|
||
import com.goormy.hackathon.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
} |
Oops, something went wrong.