Skip to content

Commit 1890c42

Browse files
authored
Merge pull request #173 from TRIP-Side-Project/dev
pr for merge
2 parents d14fd50 + 74f11e3 commit 1890c42

File tree

9 files changed

+63
-1149
lines changed

9 files changed

+63
-1149
lines changed

src/main/java/com/api/trip/common/sse/emitter/SseEmitterMap.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,40 @@
1515
@Slf4j
1616
public class SseEmitterMap {
1717

18-
private final Map<Long, SseEmitter> sseEmitterMap = new ConcurrentHashMap<>();
18+
private final Map<String, SseEmitter> sseEmitterMap = new ConcurrentHashMap<>();
1919

20-
public void put(Long memberId, SseEmitter sseEmitter) {
21-
sseEmitter.onCompletion(() -> remove(memberId));
20+
public void put(String email, SseEmitter sseEmitter) {
21+
sseEmitter.onCompletion(() -> remove(email));
2222
sseEmitter.onTimeout(sseEmitter::complete);
23-
sseEmitterMap.put(memberId, sseEmitter);
24-
log.info("connected with {}, the number of connections is {}", memberId, sseEmitterMap.size());
23+
sseEmitterMap.put(email, sseEmitter);
24+
log.info("connected with {}, the number of connections is {}", email, sseEmitterMap.size());
2525
}
2626

27-
public void remove(Long memberId) {
28-
sseEmitterMap.remove(memberId);
29-
log.info("disconnected with {}, the number of connections is {}", memberId, sseEmitterMap.size());
27+
public void remove(String email) {
28+
sseEmitterMap.remove(email);
29+
log.info("disconnected with {}, the number of connections is {}", email, sseEmitterMap.size());
3030
}
3131

32-
public void send(Long memberId, String eventName, Object eventData) {
33-
SseEmitter sseEmitter = sseEmitterMap.get(memberId);
32+
public void send(String email, String eventName, Object eventData) {
33+
SseEmitter sseEmitter = sseEmitterMap.get(email);
3434
try {
3535
sseEmitter.send(
3636
event()
3737
.name(eventName)
3838
.data(eventData)
3939
);
4040
} catch (IOException | IllegalStateException e) {
41-
remove(memberId);
41+
remove(email);
4242
}
4343
}
4444

4545
public void sendToAll(String eventName, Object eventData) {
4646
SseEventBuilder sseEventBuilder = event().name(eventName).data(eventData);
47-
sseEmitterMap.forEach((memberId, sseEmitter) -> {
47+
sseEmitterMap.forEach((email, sseEmitter) -> {
4848
try {
4949
sseEmitter.send(sseEventBuilder);
5050
} catch (IOException | IllegalStateException e) {
51-
remove(memberId);
51+
remove(email);
5252
}
5353
});
5454
}

src/main/java/com/api/trip/domain/notification/controller/NotificationController.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.api.trip.domain.notification.controller;
22

3-
import com.api.trip.common.exception.CustomException;
4-
import com.api.trip.common.exception.ErrorCode;
5-
import com.api.trip.domain.member.repository.MemberRepository;
6-
import com.api.trip.domain.notification.controller.dto.GetMyNotificationsResponse;
73
import com.api.trip.common.sse.emitter.SseEmitterMap;
4+
import com.api.trip.domain.notification.controller.dto.DeleteNotificationRequest;
5+
import com.api.trip.domain.notification.controller.dto.GetMyNotificationsResponse;
6+
import com.api.trip.domain.notification.controller.dto.ReadNotificationRequest;
87
import com.api.trip.domain.notification.service.NotificationService;
98
import lombok.RequiredArgsConstructor;
109
import org.springframework.http.MediaType;
@@ -20,46 +19,35 @@
2019
@RequiredArgsConstructor
2120
public class NotificationController {
2221

23-
private final MemberRepository memberRepository;
2422
private final NotificationService notificationService;
2523
private final SseEmitterMap sseEmitterMap;
2624

2725
@GetMapping(value = "/connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
2826
public ResponseEntity<SseEmitter> connect() {
2927
String email = SecurityContextHolder.getContext().getAuthentication().getName();
30-
Long memberId = memberRepository.findByEmail(email)
31-
.orElseThrow(() -> new CustomException(ErrorCode.UNAUTHORIZED))
32-
.getId();
33-
3428
SseEmitter sseEmitter = new SseEmitter(3600000L);
35-
sseEmitterMap.put(memberId, sseEmitter);
36-
sseEmitterMap.send(memberId, "connect", LocalDateTime.now());
29+
sseEmitterMap.put(email, sseEmitter);
30+
sseEmitterMap.send(email, "connect", LocalDateTime.now());
3731
return ResponseEntity.ok(sseEmitter);
3832
}
3933

40-
@GetMapping("/send-to-all")
41-
public void sendToAll(@RequestParam String message) {
42-
String email = SecurityContextHolder.getContext().getAuthentication().getName();
43-
sseEmitterMap.sendToAll("send-to-all", email + ": " + message);
44-
}
45-
4634
@GetMapping("/me")
4735
public ResponseEntity<GetMyNotificationsResponse> getMyNotifications() {
4836
String email = SecurityContextHolder.getContext().getAuthentication().getName();
4937
return ResponseEntity.ok(notificationService.getMyNotifications(email));
5038
}
5139

52-
@PatchMapping("/{notificationId}")
53-
public ResponseEntity<Void> readNotification(@PathVariable Long notificationId) {
40+
@PatchMapping
41+
public ResponseEntity<Void> readNotification(@RequestBody ReadNotificationRequest request) {
5442
String email = SecurityContextHolder.getContext().getAuthentication().getName();
55-
notificationService.readNotification(notificationId, email);
43+
notificationService.readNotification(request, email);
5644
return ResponseEntity.ok().build();
5745
}
5846

59-
@DeleteMapping("/{notificationId}")
60-
public ResponseEntity<Void> deleteNotification(@PathVariable Long notificationId) {
47+
@DeleteMapping
48+
public ResponseEntity<Void> deleteNotification(@RequestBody DeleteNotificationRequest request) {
6149
String email = SecurityContextHolder.getContext().getAuthentication().getName();
62-
notificationService.deleteNotification(notificationId, email);
50+
notificationService.deleteNotification(request, email);
6351
return ResponseEntity.ok().build();
6452
}
6553

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.api.trip.domain.notification.controller.dto;
2+
3+
import jakarta.validation.constraints.NotNull;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class DeleteNotificationRequest {
8+
9+
@NotNull(message = "itemId를 입력해 주세요.")
10+
private Long itemId;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.api.trip.domain.notification.controller.dto;
2+
3+
import jakarta.validation.constraints.NotNull;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class ReadNotificationRequest {
8+
9+
@NotNull(message = "itemId를 입력해 주세요.")
10+
private Long itemId;
11+
}

src/main/java/com/api/trip/domain/notification/repository/NotificationRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import org.springframework.data.jpa.repository.JpaRepository;
66

77
import java.util.List;
8+
import java.util.Optional;
89

910
public interface NotificationRepository extends JpaRepository<Notification, Long>, NotificationRepositoryCustom {
1011

1112
List<Notification> findAllByMember(Member member);
13+
14+
Optional<Notification> findByMemberIdAndItemId(Long memberId, Long itemId);
1215
}

src/main/java/com/api/trip/domain/notification/service/NotificationService.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.api.trip.domain.itemtag.model.ItemTag;
88
import com.api.trip.domain.member.model.Member;
99
import com.api.trip.domain.member.repository.MemberRepository;
10+
import com.api.trip.domain.notification.controller.dto.DeleteNotificationRequest;
1011
import com.api.trip.domain.notification.controller.dto.GetMyNotificationsResponse;
12+
import com.api.trip.domain.notification.controller.dto.ReadNotificationRequest;
1113
import com.api.trip.domain.notification.domain.Notification;
1214
import com.api.trip.domain.notification.repository.NotificationRepository;
1315
import lombok.RequiredArgsConstructor;
@@ -25,17 +27,17 @@ public class NotificationService {
2527
private final NotificationRepository notificationRepository;
2628
private final InterestTagService interestTagService;
2729

28-
public void createNotification(Item item, List<String> tagNames){
29-
30+
public void createNotification(Item item, List<String> tagNames) {
3031
List<Member> receivers = interestTagService.getMemberByTags(tagNames);
3132

32-
receivers.stream().forEach(member -> {
33-
notificationRepository.save(Notification.builder()
34-
.item(item)
35-
.member(member).build());
33+
receivers.forEach(member -> {
34+
notificationRepository.save(
35+
Notification.builder()
36+
.item(item)
37+
.member(member)
38+
.build()
39+
);
3640
});
37-
38-
3941
}
4042

4143
@Transactional(readOnly = true)
@@ -54,31 +56,23 @@ public GetMyNotificationsResponse getMyNotifications(String email) {
5456
return GetMyNotificationsResponse.of(notifications, itemTags);
5557
}
5658

57-
public void readNotification(Long notificationId, String email) {
59+
public void readNotification(ReadNotificationRequest request, String email) {
5860
Member member = memberRepository.findByEmail(email)
5961
.orElseThrow(() -> new CustomException(ErrorCode.UNAUTHORIZED));
6062

61-
Notification notification = notificationRepository.findById(notificationId)
63+
Notification notification = notificationRepository.findByMemberIdAndItemId(member.getId(), request.getItemId())
6264
.orElseThrow(() -> new CustomException(ErrorCode.NOTIFICATION_NOT_FOUND));
6365

64-
if (notification.getMember() == member) {
65-
throw new CustomException(ErrorCode.FORBIDDEN);
66-
}
67-
6866
notification.read();
6967
}
7068

71-
public void deleteNotification(Long notificationId, String email) {
69+
public void deleteNotification(DeleteNotificationRequest request, String email) {
7270
Member member = memberRepository.findByEmail(email)
7371
.orElseThrow(() -> new CustomException(ErrorCode.UNAUTHORIZED));
7472

75-
Notification notification = notificationRepository.findById(notificationId)
73+
Notification notification = notificationRepository.findByMemberIdAndItemId(member.getId(), request.getItemId())
7674
.orElseThrow(() -> new CustomException(ErrorCode.NOTIFICATION_NOT_FOUND));
7775

78-
if (notification.getMember() == member) {
79-
throw new CustomException(ErrorCode.FORBIDDEN);
80-
}
81-
8276
notificationRepository.delete(notification);
8377
}
8478

src/main/resources/application.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
spring:
22
profiles:
33
active: dev
4-
datasource:
5-
hikari:
6-
maximum-pool-size: 4
74
h2:
85
console:
96
enabled: true

src/main/resources/static/index.html

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)