Skip to content

Commit b25db5d

Browse files
authored
Merge pull request #81 from kjiyun/alarm
#80 Feat: 실시간 알림 isOn 기능 추가
2 parents cadc142 + 21c5a1b commit b25db5d

File tree

13 files changed

+89
-41
lines changed

13 files changed

+89
-41
lines changed

src/main/java/com/memesphere/domain/chartdata/repository/ChartDataRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import com.memesphere.domain.chartdata.entity.ChartData;
44
import com.memesphere.domain.memecoin.entity.MemeCoin;
5+
import org.springframework.data.domain.Pageable;
56
import org.springframework.data.jpa.repository.EntityGraph;
67
import org.springframework.data.jpa.repository.JpaRepository;
78
import org.springframework.data.jpa.repository.Query;
89
import org.springframework.data.repository.query.Param;
910

10-
import java.awt.print.Pageable;
1111
import java.math.BigDecimal;
1212
import java.time.LocalDateTime;
1313
import java.util.List;
@@ -28,4 +28,6 @@ public interface ChartDataRepository extends JpaRepository<ChartData, Long> {
2828
LocalDateTime findRecordedTimeByCoinId1();
2929

3030
List<ChartData> findByMemeCoinOrderByRecordedTimeDesc(MemeCoin memeCoin);
31+
//TODO: 위아래 코드 합치는 방법 찾기
32+
List<ChartData> findByMemeCoinOrderByRecordedTimeDesc(MemeCoin memeCoin, Pageable pageable);
3133
}

src/main/java/com/memesphere/domain/chat/service/ChatService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public String postChatLike(Long chat_id, Long user_id) {
8282

8383
// 사용자가 좋아요 눌렀는지 확인
8484
Optional<ChatLike> existingLike = chatLikeRepository.findByChatAndUser(chat, user);
85-
System.out.println("좋아요" + existingLike.isPresent());
8685

8786
if (existingLike.isPresent()) {
8887
chatLikeRepository.delete(existingLike.get());

src/main/java/com/memesphere/domain/notification/controller/CoinNotificationController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import com.memesphere.domain.notification.dto.response.NotificationListResponse;
77
import com.memesphere.domain.notification.dto.response.NotificationResponse;
88
import com.memesphere.domain.notification.service.CoinNotificationService;
9+
import com.memesphere.global.jwt.CustomUserDetails;
910
import io.swagger.v3.oas.annotations.Operation;
1011
import io.swagger.v3.oas.annotations.tags.Tag;
1112
import lombok.RequiredArgsConstructor;
13+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
1214
import org.springframework.web.bind.annotation.*;
1315

1416
@Tag(name="알림", description = "알림 관련 API")
@@ -65,8 +67,9 @@ public ApiResponse<NotificationListResponse> getNotificationList() {
6567
- "is_rising": 상승(true) 또는 하락(false)
6668
- "is_on": 알림 on(true) 또는 off(false)
6769
```""")
68-
public ApiResponse<NotificationResponse> postNotification(@RequestBody NotificationRequest request) {
69-
return ApiResponse.onSuccess(coinNotificationService.addNotification(request));
70+
public ApiResponse<NotificationResponse> postNotification(@RequestBody NotificationRequest request,
71+
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
72+
return ApiResponse.onSuccess(coinNotificationService.addNotification(request, customUserDetails.getUser()));
7073
}
7174

7275
@PatchMapping("/{notification-id}")
@@ -83,7 +86,7 @@ public ApiResponse<NotificationResponse> postNotification(@RequestBody Notificat
8386
```
8487
알림 등록 API 응답 형식과 동일
8588
```""")
86-
public ApiResponse<NotificationResponse> updateNotificationStatus(@PathVariable("notification-id") Long id) {
89+
public ApiResponse<String> updateNotificationStatus(@PathVariable("notification-id") Long id) {
8790
return ApiResponse.onSuccess(coinNotificationService.modifyNotification(id));
8891
}
8992

src/main/java/com/memesphere/domain/notification/converter/NotificationConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import com.memesphere.domain.notification.dto.response.NotificationListResponse;
66
import com.memesphere.domain.notification.dto.response.NotificationResponse;
77
import com.memesphere.domain.notification.entity.Notification;
8+
import com.memesphere.domain.user.entity.User;
89

910
import java.util.List;
1011

1112
public class NotificationConverter {
1213

13-
public static Notification toNotification(NotificationRequest notificationRequest, MemeCoin memeCoin) {
14+
public static Notification toNotification(NotificationRequest notificationRequest, MemeCoin memeCoin, User user) {
1415
return Notification.builder()
1516
.memeCoin(memeCoin)
1617
.volatility(notificationRequest.getVolatility())
1718
.stTime(notificationRequest.getStTime())
1819
.isRising(notificationRequest.getIsRising())
20+
.user(user)
1921
.build();
2022
}
2123

src/main/java/com/memesphere/domain/notification/dto/request/NotificationRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ public class NotificationRequest {
2222

2323
@Schema(description = "상승 또는 하락", example = "True")
2424
private Boolean isRising;
25+
26+
@Schema(description = "알림 켜기/끄기", example = "True")
27+
private Boolean isOn;
2528
}

src/main/java/com/memesphere/domain/notification/entity/Notification.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ public class Notification extends BaseEntity {
4141
@ManyToOne(fetch=FetchType.LAZY)
4242
@JoinColumn(name="coin_id")
4343
private MemeCoin memeCoin;
44+
45+
public void updateIsOn(Boolean isOn) {
46+
this.isOn = isOn;
47+
}
4448
}

src/main/java/com/memesphere/domain/notification/repository/EmitterRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.memesphere.domain.notification.repository;
22

3+
import org.springframework.data.jpa.repository.JpaRepository;
34
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
45
import org.yaml.snakeyaml.emitter.Emitter;
56

@@ -11,4 +12,5 @@ public interface EmitterRepository {
1112
void deleteById(String emitterId);
1213
Map<String, SseEmitter> findAllEmitterStartWithByUserId(String UserId);
1314
Map<String, Object> findAllEventCacheStartWithByUserId(String UserId);
15+
void saveEventCache(String emitterId, Object event);
1416
}

src/main/java/com/memesphere/domain/notification/repository/EmitterRepositoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public Map<String, Object> findAllEventCacheStartWithByUserId(String UserId) {
3939
.filter(entry -> entry.getKey().startsWith(UserId))
4040
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
4141
}
42+
43+
@Override
44+
public void saveEventCache(String emitterId, Object event) {
45+
eventCache.put(emitterId, event);
46+
}
4247
}

src/main/java/com/memesphere/domain/notification/service/CoinNotificationService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import com.memesphere.domain.notification.dto.request.NotificationRequest;
44
import com.memesphere.domain.notification.dto.response.NotificationListResponse;
55
import com.memesphere.domain.notification.dto.response.NotificationResponse;
6+
import com.memesphere.domain.user.entity.User;
67

78
public interface CoinNotificationService {
89
NotificationListResponse findNotificationList();
9-
NotificationResponse addNotification(NotificationRequest notificationRequest);
10-
NotificationResponse modifyNotification(Long notificationId);
10+
NotificationResponse addNotification(NotificationRequest notificationRequest, User user);
11+
String modifyNotification(Long notificationId);
1112
NotificationListResponse removeNotification(Long notificationId);
1213
}

src/main/java/com/memesphere/domain/notification/service/CoinNotificationServiceImpl.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.memesphere.domain.memecoin.entity.MemeCoin;
44
import com.memesphere.domain.memecoin.repository.MemeCoinRepository;
55
import com.memesphere.domain.notification.entity.Notification;
6+
import com.memesphere.domain.user.entity.User;
67
import com.memesphere.global.apipayload.code.status.ErrorStatus;
78
import com.memesphere.global.apipayload.exception.GeneralException;
89
import com.memesphere.domain.notification.converter.NotificationConverter;
@@ -44,11 +45,11 @@ public NotificationListResponse findNotificationList() {
4445

4546
// 알림 등록 API
4647
@Override
47-
public NotificationResponse addNotification(NotificationRequest notificationRequest) {
48+
public NotificationResponse addNotification(NotificationRequest notificationRequest, User user) {
4849
MemeCoin memeCoin = memeCoinRepository.findByName(notificationRequest.getName())
4950
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMECOIN_NOT_FOUND));
5051

51-
Notification notification = NotificationConverter.toNotification(notificationRequest, memeCoin);
52+
Notification notification = NotificationConverter.toNotification(notificationRequest, memeCoin, user);
5253
notificationRepository.save(notification);
5354

5455
NotificationResponse notificationResponse = NotificationConverter.toNotificationCreateResponse(notification, memeCoin);
@@ -57,8 +58,20 @@ public NotificationResponse addNotification(NotificationRequest notificationRequ
5758
}
5859

5960
@Override
60-
public NotificationResponse modifyNotification(Long notificationId) {
61-
return null;
61+
public String modifyNotification(Long notificationId) {
62+
63+
Notification existingNotification = notificationRepository.findById(notificationId)
64+
.orElseThrow(() -> new GeneralException(ErrorStatus.NOTIFICATION_NOT_FOUND));
65+
66+
if (existingNotification.getIsOn()) { // 알림이 켜져있는 경우
67+
existingNotification.updateIsOn(false);
68+
notificationRepository.save(existingNotification);
69+
return "알림을 껐습니다.";
70+
} else { // 알림이 꺼져있는 경우
71+
existingNotification.updateIsOn(true);
72+
notificationRepository.save(existingNotification);
73+
return "알림을 켰습니다.";
74+
}
6275
}
6376

6477
@Override

0 commit comments

Comments
 (0)