Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import Mua.Mua_backend.domain.member.entity.Member;
import Mua.Mua_backend.domain.notification.dto.response.NotificationResponse;
import Mua.Mua_backend.domain.notification.repository.NotificationRepository;
import Mua.Mua_backend.domain.notification.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -18,25 +15,32 @@
@RequestMapping("/api/notifications")
public class NotificationController {

private final NotificationRepository notificationRepository;
private final NotificationService notificationService;

// 알림 목록 조회
@GetMapping
public List<NotificationResponse> getMyNotifications(
@AuthenticationPrincipal Member member
) {
return notificationRepository
.findByMemberOrderByCreatedAtDesc(member)
.stream()
.map(NotificationResponse::from)
.toList();
return notificationService.getMyNotifications(member);
}

// 알림 읽음 처리
@PatchMapping("/{notificationId}/read")
public ResponseEntity<Void> readNotification(
@PathVariable Long notificationId,
@AuthenticationPrincipal Member member
) {
notificationService.readNotification(notificationId, member);
return ResponseEntity.ok().build();
}

// 알림 전체 삭제
@DeleteMapping
public ResponseEntity<Void> deleteMyNotifications(
@AuthenticationPrincipal Member member
) {
notificationRepository.deleteByMember(member);
notificationService.deleteMyNotifications(member);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public Notification(Member member, String message,
this.targetId = targetId;
this.read = false;
}

public void markAsRead() {
this.read = true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Mua.Mua_backend.domain.notification.service;

import Mua.Mua_backend.domain.member.entity.Member;
import Mua.Mua_backend.domain.notification.dto.response.NotificationResponse;
import Mua.Mua_backend.domain.notification.entity.Notification;
import Mua.Mua_backend.domain.notification.entity.NotificationType;
import Mua.Mua_backend.domain.notification.repository.NotificationRepository;
Expand All @@ -9,6 +10,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -17,21 +20,49 @@ public class NotificationService {
private final NotificationRepository notificationRepository;
private final FcmService fcmService;

// 알림 생성 + FCM 전송
public void sendNotification(
Member targetMember,
NotificationType type,
Long targetId,
String message
) {
Notification notification = new Notification(
targetMember,
message,
type,
targetId
);
Notification notification = Notification.builder()
.member(targetMember)
.message(message)
.targetType(type)
.targetId(targetId)
.build();

notificationRepository.save(notification);

fcmService.send(targetMember.getFcmToken(), message);
}

// 알림 목록 조회
public List<NotificationResponse> getMyNotifications(Member member) {
return notificationRepository
.findByMemberOrderByCreatedAtDesc(member)
.stream()
.map(NotificationResponse::from)
.toList();
}

// 읽음 처리
public void readNotification(Long notificationId, Member member) {
Notification notification = notificationRepository
.findById(notificationId)
.orElseThrow();

if (!notification.getMember().getId().equals(member.getId())) {
throw new RuntimeException("권한 없음");
}

notification.markAsRead();
}

// 알림 전체 삭제
public void deleteMyNotifications(Member member) {
notificationRepository.deleteByMember(member);
}
}
Loading