-
Notifications
You must be signed in to change notification settings - Fork 1
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 #55 from Familing/develop
알림 조회 기능 구현 배포
- Loading branch information
Showing
20 changed files
with
689 additions
and
284 deletions.
There are no files selected for viewing
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
17 changes: 17 additions & 0 deletions
17
familing/src/main/java/com/pinu/familing/domain/alarm/AlarmType.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,17 @@ | ||
package com.pinu.familing.domain.alarm; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum AlarmType { | ||
|
||
SNAPSHOT_SUBJECT("스냅샷 주제등록"), | ||
SNAPSHOT_REGISTER("스냅샷 등록"), | ||
LOVECARD_RECEIVE("애정카드 받음"); | ||
|
||
|
||
private String value; | ||
} |
28 changes: 28 additions & 0 deletions
28
familing/src/main/java/com/pinu/familing/domain/alarm/controller/AlarmController.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,28 @@ | ||
package com.pinu.familing.domain.alarm.controller; | ||
|
||
|
||
import com.pinu.familing.domain.alarm.service.AlarmService; | ||
import com.pinu.familing.global.oauth.dto.CustomOAuth2User; | ||
import com.pinu.familing.global.oauth.dto.PrincipalDetails; | ||
import com.pinu.familing.global.util.ApiUtils; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1") | ||
public class AlarmController { | ||
private final AlarmService alarmService; | ||
|
||
//나의 알람을 불러온다. | ||
@GetMapping("alarms") | ||
public ApiUtils.ApiResult<?> getAlarm(@AuthenticationPrincipal PrincipalDetails principalDetails) { | ||
|
||
return ApiUtils.success(alarmService.loadAlarm(principalDetails.getUsername())); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
familing/src/main/java/com/pinu/familing/domain/alarm/dto/AlarmDto.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,25 @@ | ||
package com.pinu.familing.domain.alarm.dto; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import com.pinu.familing.domain.alarm.AlarmType; | ||
import com.pinu.familing.domain.alarm.entity.Alarm; | ||
|
||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public record AlarmDto( | ||
Long id, | ||
Boolean isRead, | ||
String message, | ||
AlarmType alarmType, | ||
String alarmImg | ||
) { | ||
public static AlarmDto fromEntity(Alarm alarm) { | ||
return new AlarmDto( | ||
alarm.getId(), | ||
alarm.getIsRead(), | ||
alarm.getMessage(), | ||
alarm.getAlarmType(), | ||
alarm.getAlarmImg() | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
familing/src/main/java/com/pinu/familing/domain/alarm/dto/AlarmResponseDto.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,14 @@ | ||
package com.pinu.familing.domain.alarm.dto; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
public record AlarmResponseDto( | ||
List<AlarmDto> read, | ||
List<AlarmDto> unread, | ||
List<AlarmDto> yesterday, | ||
List<AlarmDto> sevenday | ||
) { | ||
} |
51 changes: 51 additions & 0 deletions
51
familing/src/main/java/com/pinu/familing/domain/alarm/entity/Alarm.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,51 @@ | ||
package com.pinu.familing.domain.alarm.entity; | ||
|
||
import com.pinu.familing.domain.BaseEntity; | ||
import com.pinu.familing.domain.alarm.AlarmType; | ||
import com.pinu.familing.domain.user.entity.User; | ||
import jakarta.persistence.*; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class Alarm extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private Boolean isRead = false; // 기본 값은 false로 읽지 않은 상태이다. | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "sender_id") | ||
private User sender; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "receiver_id") | ||
private User receiver; | ||
|
||
private String message; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private AlarmType alarmType; | ||
|
||
private String AlarmImg; | ||
|
||
@Builder | ||
private Alarm(Boolean isRead, User sender, User receiver, String message, AlarmType alarmType, String alarmImg) { | ||
this.sender = sender; | ||
this.receiver = receiver; | ||
this.message = message; | ||
this.alarmType = alarmType; | ||
this.isRead = isRead != null ? isRead : false; // null일 경우 기본값으로 false 설정 | ||
this.AlarmImg = alarmImg; | ||
} | ||
|
||
public void readAlarm() { | ||
this.isRead = true; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
familing/src/main/java/com/pinu/familing/domain/alarm/repository/AlarmRepository.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,25 @@ | ||
package com.pinu.familing.domain.alarm.repository; | ||
|
||
import com.pinu.familing.domain.alarm.entity.Alarm; | ||
import com.pinu.familing.domain.user.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public interface AlarmRepository extends JpaRepository<Alarm, Long> { | ||
// 특정 수신자와 읽지 않은 상태의 알람 목록을 조회하는 메서드 | ||
List<Alarm> findByReceiverAndIsReadFalse(User receiver); | ||
List<Alarm> findByReceiverAndIsReadTrue(User receiver); | ||
|
||
// isRead가 true이고, 24시간 이내에 생성된 알람을 조회 | ||
@Query("SELECT a FROM Alarm a WHERE a.receiver = :receiver AND a.isRead = true AND a.createDateTime >= :since") | ||
List<Alarm> findReadAlarmsWithin24Hours(@Param("receiver") User receiver, @Param("since") LocalDateTime since); | ||
|
||
// isRead가 true이고, 24시간 이상 7일 이내에 생성된 알람을 조회 | ||
@Query("SELECT a FROM Alarm a WHERE a.receiver = :receiver AND a.isRead = true AND a.createDateTime < :until AND a.createDateTime >= :since") | ||
List<Alarm> findReadAlarmsBetween24HoursAnd7Days(@Param("receiver") User receiver, @Param("since") LocalDateTime since, @Param("until") LocalDateTime until); | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
familing/src/main/java/com/pinu/familing/domain/alarm/service/AlarmService.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,98 @@ | ||
package com.pinu.familing.domain.alarm.service; | ||
|
||
import com.pinu.familing.domain.alarm.AlarmType; | ||
import com.pinu.familing.domain.alarm.dto.AlarmDto; | ||
import com.pinu.familing.domain.alarm.dto.AlarmResponseDto; | ||
import com.pinu.familing.domain.alarm.entity.Alarm; | ||
import com.pinu.familing.domain.alarm.repository.AlarmRepository; | ||
import com.pinu.familing.domain.user.entity.User; | ||
import com.pinu.familing.domain.user.repository.UserRepository; | ||
import com.pinu.familing.global.error.CustomException; | ||
import com.pinu.familing.global.error.ExceptionCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.pinu.familing.global.error.ExceptionCode.USER_NOT_FOUND; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AlarmService { | ||
|
||
private final AlarmRepository alarmRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public void sendAlarm(User sender, User receiver, AlarmType alarmType) { | ||
|
||
String message = ""; | ||
String alarmImg = ""; | ||
|
||
if (alarmType == AlarmType.LOVECARD_RECEIVE) { | ||
message = sender.getNickname() + "님이 애정카드를 보냈어요."; | ||
alarmImg = sender.getProfileImg(); | ||
} | ||
else if (alarmType == AlarmType.SNAPSHOT_REGISTER) { | ||
message = sender.getNickname() + "님이 SnapShot에 사진을 등록했어요."; | ||
alarmImg = sender.getProfileImg(); | ||
} | ||
else if (alarmType == AlarmType.SNAPSHOT_SUBJECT) { | ||
message = "Snap Shot의 주제 등록되었어요."; | ||
alarmImg = ""; | ||
} | ||
|
||
alarmRepository.save(Alarm.builder() | ||
.sender(sender) | ||
.message(message) | ||
.receiver(receiver) | ||
.alarmType(alarmType) | ||
.alarmImg(alarmImg) | ||
.build()); | ||
} | ||
|
||
@Transactional | ||
public AlarmResponseDto loadAlarm(String username){ | ||
User user = userRepository.findByUsername(username) | ||
.orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
List<Alarm> byReceiverAndIsReadFalse = alarmRepository.findByReceiverAndIsReadFalse(user); | ||
List<Alarm> byReceiverAndIsReadTrue = alarmRepository.findByReceiverAndIsReadTrue(user); | ||
// 현재 시간 | ||
LocalDateTime now = LocalDateTime.now(); | ||
|
||
// 24시간 전의 시간 계산 | ||
LocalDateTime oneDayAgo = now.minusHours(24); | ||
// 7일 전 시간 계산 | ||
LocalDateTime sevenDaysAgo = now.minusDays(7); | ||
|
||
List<Alarm> alarmsWithin24Hours = alarmRepository.findReadAlarmsWithin24Hours(user, oneDayAgo); | ||
|
||
// 메서드 호출하여 24시간 이상 7일 이내의 알람 조회 | ||
List<Alarm> alarmsBetween24HoursAnd7Days = alarmRepository.findReadAlarmsBetween24HoursAnd7Days(user, sevenDaysAgo, oneDayAgo); | ||
|
||
AlarmResponseDto alarmResponseDto = AlarmResponseDto.builder() | ||
.read(byReceiverAndIsReadTrue.stream() | ||
.map(AlarmDto::fromEntity) | ||
.collect(Collectors.toList())) | ||
.unread(byReceiverAndIsReadFalse.stream() | ||
.map(AlarmDto::fromEntity) | ||
.collect(Collectors.toList())) | ||
.yesterday(alarmsWithin24Hours.stream() | ||
.map(AlarmDto::fromEntity) | ||
.collect(Collectors.toList())) | ||
.sevenday(alarmsBetween24HoursAnd7Days.stream() | ||
.map(AlarmDto::fromEntity) | ||
.collect(Collectors.toList())) | ||
.build(); | ||
// 조회한 알림 읽음으로 처리 | ||
byReceiverAndIsReadFalse.forEach(alarm -> { | ||
alarm.readAlarm(); | ||
alarmRepository.save(alarm); // 변경된 상태를 저장하기 위해 필요 | ||
}); | ||
return alarmResponseDto; | ||
} | ||
} |
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
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
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
Oops, something went wrong.