From 6f4d354b042d78818bc246babd3940abf23fc93f Mon Sep 17 00:00:00 2001 From: "tk.yoon" Date: Tue, 29 Aug 2023 17:42:53 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=95=8C=EB=9E=8C=20=EB=AA=A8=EB=91=90?= =?UTF-8?q?=20=EC=9D=BD=EA=B8=B0=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modoos/alarm/controller/AlarmController.java | 15 +++++++++++++++ .../modoos/alarm/repository/AlarmRepository.java | 2 ++ .../study/modoos/alarm/service/AlarmService.java | 16 +++++++++++++++- .../study/modoos/common/exception/ErrorCode.java | 3 ++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/study/modoos/alarm/controller/AlarmController.java b/src/main/java/com/study/modoos/alarm/controller/AlarmController.java index 469763c..655b2c8 100644 --- a/src/main/java/com/study/modoos/alarm/controller/AlarmController.java +++ b/src/main/java/com/study/modoos/alarm/controller/AlarmController.java @@ -4,6 +4,9 @@ import com.study.modoos.alarm.response.ReadAlarmResponse; import com.study.modoos.alarm.service.AlarmService; import com.study.modoos.common.CurrentUser; +import com.study.modoos.common.exception.ErrorCode; +import com.study.modoos.common.exception.ModoosException; +import com.study.modoos.common.response.NormalResponse; import com.study.modoos.member.entity.Member; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; @@ -34,4 +37,16 @@ public ResponseEntity> getComment(@CurrentUser Member curre public ResponseEntity readAlarm(@CurrentUser Member currentUser, @PathVariable Long alarmId) { return ResponseEntity.ok(alarmService.readAlarm(currentUser, alarmId)); } + + @GetMapping("/read/all") + public ResponseEntity readAllAlarm(@CurrentUser Member currentUser){ + try { + alarmService.readAllAlarm(currentUser); + return ResponseEntity.ok(NormalResponse.success()); + } + catch (ModoosException ex){ + throw new ModoosException(ErrorCode.MEMBER_HAS_NOT_ALARM); + } + } } + diff --git a/src/main/java/com/study/modoos/alarm/repository/AlarmRepository.java b/src/main/java/com/study/modoos/alarm/repository/AlarmRepository.java index 4392861..eb71df9 100644 --- a/src/main/java/com/study/modoos/alarm/repository/AlarmRepository.java +++ b/src/main/java/com/study/modoos/alarm/repository/AlarmRepository.java @@ -9,6 +9,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @@ -16,4 +17,5 @@ public interface AlarmRepository extends JpaRepository { Slice findByMemberOrderByCreatedAtDesc(Member member, Pageable sortedPageable); Optional findByStudyAndMemberAndAlarmType(Study study, Member member, AlarmType alarmType); + ListfindByMemberAndIsRead(Member member, boolean read); } diff --git a/src/main/java/com/study/modoos/alarm/service/AlarmService.java b/src/main/java/com/study/modoos/alarm/service/AlarmService.java index 9718bb2..6251e3b 100644 --- a/src/main/java/com/study/modoos/alarm/service/AlarmService.java +++ b/src/main/java/com/study/modoos/alarm/service/AlarmService.java @@ -11,7 +11,6 @@ import com.study.modoos.member.repository.MemberRepository; import com.study.modoos.participant.repository.ParticipantRepository; import com.study.modoos.study.entity.Study; -import com.study.modoos.study.repository.StudyRepository; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.*; import org.springframework.stereotype.Service; @@ -130,4 +129,19 @@ public ReadAlarmResponse readAlarm(Member currentUser, Long alarmId) { alarmRepository.save(requestAlarm); return ReadAlarmResponse.readAlarm(requestAlarm, true); } + + public void readAllAlarm(Member currentUser) { + Member member = memberRepository.findByEmail(currentUser.getEmail()) + .orElseThrow(() -> new ModoosException(ErrorCode.MEMBER_NOT_FOUND)); + + List unreadAlarms = alarmRepository.findByMemberAndIsRead(member, false); + if (unreadAlarms.isEmpty()){ + throw new ModoosException(ErrorCode.MEMBER_HAS_NOT_ALARM); + }else { + for (Alarm alarm : unreadAlarms) { + alarm.readAlarm(true); + alarmRepository.save(alarm); + } + } + } } diff --git a/src/main/java/com/study/modoos/common/exception/ErrorCode.java b/src/main/java/com/study/modoos/common/exception/ErrorCode.java index d971118..02fe9ad 100644 --- a/src/main/java/com/study/modoos/common/exception/ErrorCode.java +++ b/src/main/java/com/study/modoos/common/exception/ErrorCode.java @@ -37,7 +37,8 @@ public enum ErrorCode { INVALID_COOKIE_NAME(HttpStatus.NOT_FOUND, "JwtFilter > resolveToken 에서 쿠키 이름이 다릅니다", "cookie.getName()으로 쿠키 이름을 확인해주세요"), MISS_DEADLINE(HttpStatus.FORBIDDEN, "해당 스터디의 모집 마감일이 지났습니다.", "해당 스터디는 모집이 종료되었습니다." ), MEMBER_NOT_IN_ALARM(HttpStatus.NOT_FOUND, "해당 수신자에 대한 알림 데이터가 없습니다." , "알림이 생기면 확인해주세요"), - ALARM_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 알람을 찾을 수 없습니다.", "alarmId를 확인해주세요"); + ALARM_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 알람을 찾을 수 없습니다.", "alarmId를 확인해주세요"), + MEMBER_HAS_NOT_ALARM(HttpStatus.NOT_FOUND, "해당 회원은 알림이 존재하지 않습니다.", "알림이 있는지 확인해주세요"); private final HttpStatus httpStatus;