forked from TeamMajorLink/majorLink_server
-
Notifications
You must be signed in to change notification settings - Fork 0
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 TeamMajorLink#52 from kchaeeun/feat#38
- Loading branch information
Showing
14 changed files
with
359 additions
and
8 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
majorLink/src/main/java/com/example/majorLink/controller/NotificationController.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,54 @@ | ||
package com.example.majorLink.controller; | ||
|
||
import com.example.majorLink.domain.User; | ||
import com.example.majorLink.dto.response.NotificationResponse; | ||
import com.example.majorLink.global.auth.AuthUser; | ||
import com.example.majorLink.service.NotificationService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/notification") | ||
@RequiredArgsConstructor | ||
public class NotificationController { | ||
private final NotificationService notificationService; | ||
|
||
// Last-Event-ID는 SSE 연결이 끊어졌을 경우, 클라이언트가 수신한 마지막 데이터 ID 값을 의미, 항상 존재 X -> false | ||
|
||
/** | ||
* 알림을 위한 구독 API | ||
* [GET] /notification/subscribe | ||
* @param authUser | ||
* @param lastEventId | ||
* @return | ||
*/ | ||
@GetMapping(value = "/subscribe", produces = "text/event-stream") | ||
public SseEmitter subscribe( | ||
@AuthenticationPrincipal AuthUser authUser, | ||
@RequestHeader(value = "Last-Event-ID", required = false, defaultValue = "") String lastEventId) { | ||
User user = authUser.getUser(); | ||
return notificationService.subscribe(user, lastEventId); | ||
} | ||
|
||
/** | ||
* 알림 전체 조회 | ||
* [GET] /notification | ||
* @param authUser | ||
* @return | ||
*/ | ||
@GetMapping | ||
public ResponseEntity<List<NotificationResponse>> getNotificationList( | ||
@AuthenticationPrincipal AuthUser authUser) { | ||
User user = authUser.getUser(); | ||
|
||
List<NotificationResponse> notificationResponse = notificationService.getNotificationList(user); | ||
|
||
return ResponseEntity.status(HttpStatus.OK).body(notificationResponse); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
majorLink/src/main/java/com/example/majorLink/domain/NotificationContent.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,15 @@ | ||
package com.example.majorLink.domain; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import jakarta.persistence.Embedded; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Embeddable | ||
@NoArgsConstructor | ||
public class NotificationContent { | ||
@Column(nullable = false) | ||
private String content; | ||
} |
16 changes: 16 additions & 0 deletions
16
majorLink/src/main/java/com/example/majorLink/domain/RelatedUrl.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.example.majorLink.domain; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Embeddable | ||
@NoArgsConstructor | ||
public class RelatedUrl { | ||
@Column(nullable = false) | ||
private String url; | ||
} |
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
5 changes: 5 additions & 0 deletions
5
majorLink/src/main/java/com/example/majorLink/domain/enums/NotificationType.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,5 @@ | ||
package com.example.majorLink.domain.enums; | ||
|
||
public enum NotificationType { | ||
APPLICATION | ||
} |
19 changes: 19 additions & 0 deletions
19
majorLink/src/main/java/com/example/majorLink/dto/response/NotificationResponse.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,19 @@ | ||
package com.example.majorLink.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class NotificationResponse { | ||
private Long id; | ||
private String receiver; | ||
private String sender; | ||
private String content; | ||
private String url; | ||
private String createdAt; | ||
} |
18 changes: 18 additions & 0 deletions
18
majorLink/src/main/java/com/example/majorLink/repository/EmitterRepository.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,18 @@ | ||
package com.example.majorLink.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public interface EmitterRepository { | ||
SseEmitter save(String emitterId, SseEmitter sseEmitter); | ||
void saveEventCache(String emitterId, Object event); | ||
// 해당 유저와 관련된 모든 emitter를 찾는다 | ||
Map<String, SseEmitter> findAllEmitterStartWithByUserId(UUID userId); | ||
// 해당 유저와 관련된 모든 이벤트를 찾는다. | ||
Map<String, Object> findAllEventCacheStartWithByUserId(UUID userId); | ||
|
||
void deleteById(String emitterId); | ||
} |
47 changes: 47 additions & 0 deletions
47
majorLink/src/main/java/com/example/majorLink/repository/EmitterRepositoryImpl.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,47 @@ | ||
package com.example.majorLink.repository; | ||
|
||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
public class EmitterRepositoryImpl implements EmitterRepository{ | ||
// 동시성 고려를 위해 ConcurrentHashMap 사용 | ||
private final Map<String, SseEmitter> emitters = new ConcurrentHashMap<>(); | ||
private final Map<String, Object> eventCache = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public SseEmitter save(String emitterId, SseEmitter sseEmitter) { | ||
emitters.put(emitterId, sseEmitter); | ||
return sseEmitter; | ||
} | ||
|
||
@Override | ||
public void saveEventCache(String emitterId, Object event) { | ||
eventCache.put(emitterId, event); | ||
} | ||
|
||
@Override | ||
public Map<String, SseEmitter> findAllEmitterStartWithByUserId(UUID userId) { | ||
return emitters.entrySet().stream() | ||
.filter(entry -> entry.getKey().startsWith(String.valueOf(userId))) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> findAllEventCacheStartWithByUserId(UUID userId) { | ||
return eventCache.entrySet().stream() | ||
.filter(entry -> entry.getKey().startsWith(String.valueOf(userId))) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
@Override | ||
public void deleteById(String emitterId) { | ||
emitters.remove(emitterId); | ||
} | ||
|
||
|
||
} |
13 changes: 13 additions & 0 deletions
13
majorLink/src/main/java/com/example/majorLink/repository/NotificationRepository.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,13 @@ | ||
package com.example.majorLink.repository; | ||
|
||
import com.example.majorLink.domain.Notification; | ||
import com.example.majorLink.domain.User; | ||
import com.example.majorLink.dto.response.NotificationResponse; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface NotificationRepository extends JpaRepository<Notification, Long> { | ||
|
||
List<Notification> findAllByReceiver(User user); | ||
} |
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
15 changes: 15 additions & 0 deletions
15
majorLink/src/main/java/com/example/majorLink/service/NotificationService.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,15 @@ | ||
package com.example.majorLink.service; | ||
|
||
import com.example.majorLink.domain.*; | ||
import com.example.majorLink.dto.response.NotificationResponse; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.util.List; | ||
|
||
public interface NotificationService { | ||
SseEmitter subscribe(User user, String lastEventId); | ||
void send(User sender, Lecture lecture, String content); | ||
|
||
// 전체 알림 조회 | ||
List<NotificationResponse> getNotificationList(User user); | ||
} |
Oops, something went wrong.