-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] 매칭 완료 알람 기능 구현(#761) #768
Changes from 7 commits
f00e047
3b76bc9
17e2f02
d3b1d0f
99bd529
fb014df
f4eec6b
e031d91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package corea.alarm.domain; | ||
|
||
import corea.member.domain.Member; | ||
|
||
public interface Alarm { | ||
|
||
boolean isStatus(boolean status); | ||
|
||
boolean isNotReceiver(Member member); | ||
|
||
void read(); | ||
|
||
String getActionType(); | ||
|
||
Long getId(); | ||
|
||
Long getActorId(); | ||
|
||
Long getInteractionId(); | ||
|
||
Long getReceiverId(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
public enum AlarmActionType { | ||
REVIEW_COMPLETE, | ||
REVIEW_URGE, | ||
MATCH_COMPLETE, | ||
MATCH_FAIL, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package corea.alarm.domain; | ||
|
||
import corea.global.BaseTimeEntity; | ||
import corea.member.domain.Member; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public class ServerToUserAlarm extends BaseTimeEntity implements Alarm { | ||
|
||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private Long id; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private AlarmActionType alarmActionType; | ||
|
||
private Long receiverId; | ||
|
||
private Long interactionId; | ||
|
||
private boolean isRead; | ||
|
||
public ServerToUserAlarm(AlarmActionType alarmActionType, long receiverId, long interactionId, boolean isRead) { | ||
this(null, alarmActionType, receiverId, interactionId, isRead); | ||
} | ||
|
||
@Override | ||
public boolean isStatus(boolean status) { | ||
return isRead == status; | ||
} | ||
|
||
@Override | ||
public String getActionType() { | ||
return alarmActionType.name(); | ||
} | ||
|
||
@Override | ||
public Long getActorId() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isNotReceiver(Member member) { | ||
return !receiverId.equals(member.getId()); | ||
} | ||
|
||
@Override | ||
public void read() { | ||
isRead = true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package corea.alarm.domain; | ||
|
||
import corea.alarm.repository.ServerToUserAlarmRepository; | ||
import corea.exception.CoreaException; | ||
import corea.exception.ExceptionType; | ||
import corea.global.annotation.Reader; | ||
import corea.member.domain.Member; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.EnumMap; | ||
import java.util.stream.Collectors; | ||
|
||
@Reader | ||
@RequiredArgsConstructor | ||
public class ServerToUserAlarmReader { | ||
|
||
private final ServerToUserAlarmRepository serverToUserAlarmRepository; | ||
|
||
public long countReceivedAlarm(Member member, boolean isRead) { | ||
return serverToUserAlarmRepository.findAllByReceiverId(member.getId()) | ||
.stream() | ||
.filter(alarm -> alarm.isStatus(isRead)) | ||
.count(); | ||
} | ||
|
||
public ServerToUserAlarm find(long actionId) { | ||
return serverToUserAlarmRepository.findById(actionId) | ||
.orElseThrow(() -> new CoreaException(ExceptionType.NOT_RECEIVED_ALARM)); | ||
} | ||
|
||
public AlarmsByActionType findAllByReceiver(Member member) { | ||
return new AlarmsByActionType(serverToUserAlarmRepository.findAllByReceiverId(member.getId()) | ||
.stream() | ||
.collect(Collectors.groupingBy( | ||
ServerToUserAlarm::getAlarmActionType, | ||
() -> new EnumMap<>(AlarmActionType.class), | ||
Collectors.toList() | ||
))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package corea.alarm.domain; | ||
|
||
import corea.alarm.repository.ServerToUserAlarmRepository; | ||
import corea.exception.CoreaException; | ||
import corea.exception.ExceptionType; | ||
import corea.global.annotation.Writer; | ||
import corea.member.domain.Member; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Writer | ||
@RequiredArgsConstructor | ||
public class ServerToUserAlarmWriter { | ||
|
||
private final ServerToUserAlarmRepository serverToUserAlarmRepository; | ||
|
||
public ServerToUserAlarm create(ServerToUserAlarm serverToUserAlarm) { | ||
return serverToUserAlarmRepository.save(serverToUserAlarm); | ||
} | ||
|
||
public ServerToUserAlarm check(Member member, ServerToUserAlarm serverToUserAlarm) { | ||
if (serverToUserAlarm.isNotReceiver(member)) { | ||
throw new CoreaException(ExceptionType.NOT_RECEIVED_ALARM); | ||
} | ||
serverToUserAlarm.read(); | ||
return serverToUserAlarm; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package corea.alarm.dto; | ||
|
||
import corea.alarm.domain.AlarmType; | ||
import corea.alarm.domain.ServerToUserAlarm; | ||
import corea.alarm.domain.UserToUserAlarm; | ||
import corea.member.domain.Member; | ||
import corea.room.domain.Room; | ||
|
@@ -31,7 +32,13 @@ public record AlarmResponse( | |
String alarmType | ||
) { | ||
|
||
public static AlarmResponse from(UserToUserAlarm alarm, Member member, Room room) { | ||
return new AlarmResponse(alarm.getId(), alarm.getActionType(), MemberResponse.from(member), InteractionResponse.from(room), alarm.isRead(), alarm.getCreateAt(), AlarmType.USER.name()); | ||
public static AlarmResponse of(UserToUserAlarm alarm, Member member, Room room) { | ||
return new AlarmResponse(alarm.getId(), alarm.getActionType(), MemberResponse.from(member), | ||
InteractionResponse.from(room), alarm.isRead(), alarm.getCreateAt(), AlarmType.USER.name()); | ||
} | ||
|
||
public static AlarmResponse of(ServerToUserAlarm alarm, Room room) { | ||
return new AlarmResponse(alarm.getId(), alarm.getActionType(), null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
InteractionResponse.from(room), alarm.isRead(), alarm.getCreateAt(), AlarmType.SERVER.name()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package corea.alarm.dto; | ||
|
||
import corea.alarm.domain.Alarm; | ||
import corea.alarm.domain.ServerToUserAlarm; | ||
import corea.alarm.domain.UserToUserAlarm; | ||
import corea.exception.CoreaException; | ||
import corea.exception.ExceptionType; | ||
import corea.member.domain.Member; | ||
import corea.room.domain.Room; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
@@ -13,10 +17,18 @@ public record AlarmResponses( | |
@Schema(description = "알림 리스트") | ||
List<AlarmResponse> data) { | ||
|
||
public static AlarmResponses from(List<UserToUserAlarm> responses, Map<Long, Member> members, Map<Long, Room> rooms) { | ||
public static AlarmResponses of(List<Alarm> alarms, Map<Long, Member> members, Map<Long, Room> userAlarmRooms, Map<Long, Room> serverAlarmRooms) { | ||
//@formatter:off | ||
return new AlarmResponses(responses.stream() | ||
.map(alarm -> AlarmResponse.from(alarm, members.get(alarm.getActorId()), rooms.get(alarm.getInteractionId()))) | ||
return new AlarmResponses(alarms.stream() | ||
.map(alarm -> { | ||
if (alarm instanceof UserToUserAlarm userAlarm) { | ||
return AlarmResponse.of(userAlarm, members.get(userAlarm.getActorId()), userAlarmRooms.get(userAlarm.getInteractionId())); | ||
} | ||
if (alarm instanceof ServerToUserAlarm serverAlarm){ | ||
return AlarmResponse.of(serverAlarm, serverAlarmRooms.get(serverAlarm.getInteractionId())); | ||
} | ||
throw new CoreaException(ExceptionType.UNDEFINED_ALARM_TYPE); | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instanceof 를 이용하여 외부에서 타입을 검사하는 것 대신, 둘 다 종류가 많아질수록 길어지는 건 매한가지 일 것 같긴한데, 지금 타입이 2개밖에 없는 상황에서 type 을 정의하는 Enum 으로 분리하는 것도 과한작업 같다고 느껴지긴하네요. |
||
.sorted(Comparator.comparing(AlarmResponse::createAt).reversed()) | ||
.toList()); | ||
//@formatter:on | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package corea.alarm.dto; | ||
|
||
import corea.alarm.domain.AlarmActionType; | ||
import corea.alarm.domain.ServerToUserAlarm; | ||
|
||
public record CreateServerToUserAlarmInput(AlarmActionType alarmType, | ||
long receiverId, | ||
long interactionId) { | ||
|
||
public ServerToUserAlarm toEntity() { | ||
return new ServerToUserAlarm( | ||
alarmType, | ||
receiverId, | ||
interactionId, | ||
false | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package corea.alarm.repository; | ||
|
||
import corea.alarm.domain.ServerToUserAlarm; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface ServerToUserAlarmRepository extends JpaRepository<ServerToUserAlarm, Long> { | ||
|
||
List<ServerToUserAlarm> findAllByReceiverId(long receiverId); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isRead 대신 isStatus 를 한 이유가 있나요?