Skip to content

Commit

Permalink
✨ feat: Redis 환경 기능 개선 + 상시 위치 정보 알림 API 구현 (#30)
Browse files Browse the repository at this point in the history
✨ feat: Redis 환경 기능 개선 + 상시 위치 정보 알림 API 구현 (#30)
  • Loading branch information
DDonghyeo authored Apr 27, 2024
2 parents efbe645 + b27dc17 commit c80d2ad
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,4 +44,9 @@ public List<NotificationResponse> getNotifications(Long userId) {
public void sendGoOutAlarm(Long userId) {
notificationService.sendGoOutAlarm(userId);
}

@PostMapping("/")
public void checkCurrentAlarm(@RequestBody LocationDto locationDto) {
notificationService.checkCurrentAlarm(locationDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
public class UserData {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;

private String nickName;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +30,7 @@ public class KafkaConsumer {
private final UserDataRepository userDataRepository;
private final UserMedianRepository userMedianRepository;

private final FireTokenBaseRepository fireBaseTokenRepository;
private final RedisUtils redisUtils;

/**
* 중앙값 동기화 Listener
Expand Down Expand Up @@ -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());
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,4 +83,14 @@ public String sendGoOutAlarm(Long userId) {

return finalMessage;
}

//현재 위치 공유 -> 상시 알림 검사
public void checkCurrentAlarm(LocationDto locationDto) {

//TODO : 현재 지역에 강수량 정보가 있는지?

//TODO : 현재 지역에 바람 세기 정보는 있는지?

//TODO : 만약 알림 내용이 있다면 전송하기
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,7 +65,7 @@ static void beforeAll() {
private UserMedianRepository userMedianRepository;

@Autowired
private FireTokenBaseRepository fireTokenBaseRepository;
private RedisUtils redisUtils;

@Autowired
private UserDataRepository userDataRepository;
Expand Down Expand Up @@ -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
Expand All @@ -148,7 +148,7 @@ void userSettingsWindDegreeTest() throws InterruptedException {

//when
userDataRepository.save(UserData.builder()
.windDegree(10)
.windDegree(11)
.userId(0L)
.build());
UserSettingsDto userSettingsDto = UserSettingsDto.builder()
Expand Down

0 comments on commit c80d2ad

Please sign in to comment.