diff --git a/noti-service/src/main/java/com/waither/notiservice/api/NotificationController.java b/noti-service/src/main/java/com/waither/notiservice/api/NotificationController.java index 9fe13831..e9ca08a5 100644 --- a/noti-service/src/main/java/com/waither/notiservice/api/NotificationController.java +++ b/noti-service/src/main/java/com/waither/notiservice/api/NotificationController.java @@ -1,6 +1,7 @@ package com.waither.notiservice.api; import com.waither.notiservice.api.response.NotificationResponse; +import com.waither.notiservice.dto.LocationDto; import com.waither.notiservice.service.NotificationService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -43,4 +44,9 @@ public List getNotifications(Long userId) { public void sendGoOutAlarm(Long userId) { notificationService.sendGoOutAlarm(userId); } + + @PostMapping("/") + public void checkCurrentAlarm(@RequestBody LocationDto locationDto) { + notificationService.checkCurrentAlarm(locationDto); + } } diff --git a/noti-service/src/main/java/com/waither/notiservice/domain/UserData.java b/noti-service/src/main/java/com/waither/notiservice/domain/UserData.java index 80c40cae..246c2df8 100644 --- a/noti-service/src/main/java/com/waither/notiservice/domain/UserData.java +++ b/noti-service/src/main/java/com/waither/notiservice/domain/UserData.java @@ -16,7 +16,6 @@ public class UserData { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) private Long userId; private String nickName; diff --git a/noti-service/src/main/java/com/waither/notiservice/dto/LocationDto.java b/noti-service/src/main/java/com/waither/notiservice/dto/LocationDto.java new file mode 100644 index 00000000..553489f9 --- /dev/null +++ b/noti-service/src/main/java/com/waither/notiservice/dto/LocationDto.java @@ -0,0 +1,16 @@ +package com.waither.notiservice.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class LocationDto { + + public double x; + + public double y; +} diff --git a/noti-service/src/main/java/com/waither/notiservice/redis/FireTokenBaseRepository.java b/noti-service/src/main/java/com/waither/notiservice/redis/FireTokenBaseRepository.java deleted file mode 100644 index a26ce421..00000000 --- a/noti-service/src/main/java/com/waither/notiservice/redis/FireTokenBaseRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.waither.notiservice.redis; - -import com.waither.notiservice.domain.FireBaseToken; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface FireTokenBaseRepository extends CrudRepository { -} diff --git a/noti-service/src/main/java/com/waither/notiservice/service/KafkaConsumer.java b/noti-service/src/main/java/com/waither/notiservice/service/KafkaConsumer.java index 0788685b..ba612334 100644 --- a/noti-service/src/main/java/com/waither/notiservice/service/KafkaConsumer.java +++ b/noti-service/src/main/java/com/waither/notiservice/service/KafkaConsumer.java @@ -6,9 +6,9 @@ import com.waither.notiservice.dto.kafka.TokenDto; import com.waither.notiservice.dto.kafka.UserMedianDto; import com.waither.notiservice.dto.kafka.UserSettingsDto; -import com.waither.notiservice.redis.FireTokenBaseRepository; import com.waither.notiservice.repository.UserDataRepository; import com.waither.notiservice.repository.UserMedianRepository; +import com.waither.notiservice.utils.RedisUtils; import com.waither.notiservice.utils.TemperatureUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,7 +30,7 @@ public class KafkaConsumer { private final UserDataRepository userDataRepository; private final UserMedianRepository userMedianRepository; - private final FireTokenBaseRepository fireBaseTokenRepository; + private final RedisUtils redisUtils; /** * 중앙값 동기화 Listener @@ -72,8 +72,8 @@ public void consumeFirebaseToken(TokenDto tokenDto) { log.info("[ Kafka Listener ] User Id : --> {}", tokenDto.getUserId()); log.info("[ Kafka Listener ] Token : --> {}", tokenDto.getToken()); - fireBaseTokenRepository.save(tokenDto.toEntity()); - + //토큰 Redis 저장 + redisUtils.save(String.valueOf(tokenDto.getUserId()), tokenDto.getToken()); } @@ -126,6 +126,7 @@ public void consumeWindAlarm(@Payload String message) { //TODO : 푸시알림 전송 String finalResultMessage = resultMessage; userIds.forEach(id ->{ + String token = String.valueOf(redisUtils.get(String.valueOf(id))); System.out.println("[ 푸시알림 ] 바람 세기 알림"); System.out.printf("[ 푸시알림 ] userId ---> {%d}", id); System.out.printf("[ 푸시알림 ] message ---> {%s}", finalResultMessage); @@ -155,7 +156,7 @@ public void consumeSnow(@Payload String message) { String finalResultMessage = resultMessage; userIds.forEach(id ->{ //TODO : 예외처리하기 - String token = fireBaseTokenRepository.findById(id).orElseThrow().getToken(); + String token = String.valueOf(redisUtils.get(String.valueOf(id))); System.out.println("[ 푸시알림 ] 강수량 알림"); System.out.printf("[ 푸시알림 ] userId ---> {%d}", id); System.out.printf("[ 푸시알림 ] message ---> {%s}", finalResultMessage); @@ -180,7 +181,7 @@ public void consumeClimateAlarm(@Payload String message) { //TODO : 푸시알림 전송 String finalResultMessage = resultMessage; userIds.forEach(id ->{ - String token = fireBaseTokenRepository.findById(id).orElseThrow().getToken(); + String token = String.valueOf(redisUtils.get(String.valueOf(id))); System.out.println("[ 푸시알림 ] 기상 특보 알림"); System.out.printf("[ 푸시알림 ] userId ---> {%d}", id); System.out.printf("[ 푸시알림 ] message ---> {%s}", finalResultMessage); diff --git a/noti-service/src/main/java/com/waither/notiservice/service/NotificationService.java b/noti-service/src/main/java/com/waither/notiservice/service/NotificationService.java index 053cbabb..121c1f17 100644 --- a/noti-service/src/main/java/com/waither/notiservice/service/NotificationService.java +++ b/noti-service/src/main/java/com/waither/notiservice/service/NotificationService.java @@ -3,6 +3,7 @@ import com.waither.notiservice.api.response.NotificationResponse; import com.waither.notiservice.domain.UserData; import com.waither.notiservice.domain.UserMedian; +import com.waither.notiservice.dto.LocationDto; import com.waither.notiservice.repository.NotificationRepository; import com.waither.notiservice.repository.UserDataRepository; import com.waither.notiservice.repository.UserMedianRepository; @@ -82,4 +83,14 @@ public String sendGoOutAlarm(Long userId) { return finalMessage; } + + //현재 위치 공유 -> 상시 알림 검사 + public void checkCurrentAlarm(LocationDto locationDto) { + + //TODO : 현재 지역에 강수량 정보가 있는지? + + //TODO : 현재 지역에 바람 세기 정보는 있는지? + + //TODO : 만약 알림 내용이 있다면 전송하기 + } } diff --git a/noti-service/src/main/java/com/waither/notiservice/utils/RedisUtils.java b/noti-service/src/main/java/com/waither/notiservice/utils/RedisUtils.java new file mode 100644 index 00000000..a4ec1920 --- /dev/null +++ b/noti-service/src/main/java/com/waither/notiservice/utils/RedisUtils.java @@ -0,0 +1,34 @@ +package com.waither.notiservice.utils; + +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +@Component +@RequiredArgsConstructor +public class RedisUtils { + + private final RedisTemplate redisTemplate; + + public void save(String key, Object val) { + redisTemplate.opsForValue().set(key, val); + } + + public void save(String key, Object val, long timeout, TimeUnit unit ) { + redisTemplate.opsForValue().set(key, val, timeout, unit); + } + + public boolean hasKey(String key) { + return Boolean.TRUE.equals(redisTemplate.hasKey(key)); + } + + public Object get(String key) { + return redisTemplate.opsForValue().get(key); + } + + public boolean delete(String key) { + return Boolean.TRUE.equals(redisTemplate.delete(key)); + } +} diff --git a/noti-service/src/test/java/com/waither/notiservice/service/KafkaConsumerTest.java b/noti-service/src/test/java/com/waither/notiservice/service/KafkaConsumerTest.java index d7355fe1..43eaa0f1 100644 --- a/noti-service/src/test/java/com/waither/notiservice/service/KafkaConsumerTest.java +++ b/noti-service/src/test/java/com/waither/notiservice/service/KafkaConsumerTest.java @@ -4,9 +4,9 @@ import com.waither.notiservice.dto.kafka.TokenDto; import com.waither.notiservice.dto.kafka.UserMedianDto; import com.waither.notiservice.dto.kafka.UserSettingsDto; -import com.waither.notiservice.redis.FireTokenBaseRepository; import com.waither.notiservice.repository.UserDataRepository; import com.waither.notiservice.repository.UserMedianRepository; +import com.waither.notiservice.utils.RedisUtils; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.serialization.StringSerializer; import org.junit.jupiter.api.BeforeAll; @@ -65,7 +65,7 @@ static void beforeAll() { private UserMedianRepository userMedianRepository; @Autowired - private FireTokenBaseRepository fireTokenBaseRepository; + private RedisUtils redisUtils; @Autowired private UserDataRepository userDataRepository; @@ -132,10 +132,10 @@ void firebaseTokenTest() throws InterruptedException { Thread.sleep(2000); //2초 대기 - assertThat(fireTokenBaseRepository.findById(0L).get().getToken()).isEqualTo("test token"); + assertThat(String.valueOf(redisUtils.get("0"))).isEqualTo("test token"); //끝나고 삭제 -> Rollback 일어나지 않아서 - fireTokenBaseRepository.deleteById(0L); + redisUtils.delete("0"); } @Test @@ -148,7 +148,7 @@ void userSettingsWindDegreeTest() throws InterruptedException { //when userDataRepository.save(UserData.builder() - .windDegree(10) + .windDegree(11) .userId(0L) .build()); UserSettingsDto userSettingsDto = UserSettingsDto.builder()