Skip to content

Commit

Permalink
feat: 공지 알람 발송 및 조회 API 구현 (#324)
Browse files Browse the repository at this point in the history
* refactor(CustomException): 생성자 추가 (#316)

* refactor(Nickname): 블랙리스트 추가 (#316)

* feat: 공지 알람 발송 및 조회 API 구현 (#316)

* refactor: 코드 포맷팅 (#316)
  • Loading branch information
kdkdhoho authored Nov 11, 2024
1 parent 4592773 commit 4c5c154
Show file tree
Hide file tree
Showing 32 changed files with 235 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.listywave.list.application.domain.comment.Comment;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.domain.reply.Reply;
import com.listywave.notice.application.domain.Notice;
import com.listywave.user.application.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class Alarm {
@JoinColumn(name = "send_user_id", nullable = false)
private User sendUser;

@Column(nullable = false)
@Column(nullable = true)
private Long receiveUserId;

@ManyToOne(fetch = LAZY)
Expand All @@ -59,6 +60,10 @@ public class Alarm {
@JoinColumn(name = "reply_id", nullable = true, foreignKey = @ForeignKey(name = "alarm_reply_fk"))
private Reply reply;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "notice_id", nullable = true, foreignKey = @ForeignKey(name = "alarm_notice_fk"))
private Notice notice;

@Column(nullable = false)
@Enumerated(value = STRING)
private AlarmType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static com.listywave.alarm.application.domain.AlarmType.COLLECT;
import static com.listywave.alarm.application.domain.AlarmType.COMMENT;
import static com.listywave.alarm.application.domain.AlarmType.FOLLOW;
import static com.listywave.alarm.application.domain.AlarmType.NOTICE;
import static com.listywave.alarm.application.domain.AlarmType.REPLY;

import com.listywave.list.application.domain.comment.Comment;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.domain.reply.Reply;
import com.listywave.mention.Mention;
import com.listywave.notice.application.domain.Notice;
import com.listywave.user.application.domain.User;
import java.util.List;
import lombok.Builder;
Expand All @@ -21,6 +23,7 @@ public record AlarmCreateEvent(
Comment comment,
Reply reply,
List<Mention> mentions,
Notice notice,
AlarmType alarmType
) {

Expand All @@ -32,6 +35,7 @@ public Alarm toEntity() {
.comment(comment)
.reply(reply)
.type(alarmType)
.notice(notice)
.isChecked(false)
.build();
}
Expand Down Expand Up @@ -76,6 +80,14 @@ public static AlarmCreateEvent collect(User publisher, ListEntity list) {
.build();
}

public static AlarmCreateEvent notice(User user, Notice notice) {
return AlarmCreateEvent.builder()
.publisher(user)
.notice(notice)
.alarmType(NOTICE)
.build();
}

public boolean isToMyself() {
return this.publisher.isSame(listenerId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public enum AlarmType {
REPLY,
MENTION,
COLLABORATOR,
NOTICE,
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.domain.reply.Reply;
import com.listywave.mention.Mention;
import com.listywave.notice.application.domain.Notice;
import com.listywave.user.application.domain.User;
import jakarta.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
Expand All @@ -18,9 +20,10 @@ public record AlarmFindResponse(
@JsonProperty("checked") boolean isChecked,
String type,
UserDto sendUser,
ListDto list,
CommentDto comment,
ReplyDto reply
@Nullable ListDto list,
@Nullable CommentDto comment,
@Nullable ReplyDto reply,
@Nullable NoticeDto notice
) {

public static List<AlarmFindResponse> toList(List<Alarm> alarms) {
Expand All @@ -34,6 +37,7 @@ public static List<AlarmFindResponse> toList(List<Alarm> alarms) {
.list(ListDto.of(alarm.getList()))
.comment(CommentDto.of(alarm.getComment()))
.reply(ReplyDto.of(alarm.getReply()))
.notice(NoticeDto.of(alarm.getNotice()))
.build()
).toList();
}
Expand All @@ -54,7 +58,7 @@ public record ListDto(
String title
) {

public static ListDto of(ListEntity list) {
public static ListDto of(@Nullable ListEntity list) {
if (list == null) {
return null;
}
Expand All @@ -68,7 +72,7 @@ public record CommentDto(
List<MentionDto> mentions
) {

public static CommentDto of(Comment comment) {
public static CommentDto of(@Nullable Comment comment) {
if (comment == null) {
return null;
}
Expand All @@ -82,7 +86,7 @@ public record ReplyDto(
List<MentionDto> mentions
) {

public static ReplyDto of(Reply reply) {
public static ReplyDto of(@Nullable Reply reply) {
if (reply == null) {
return null;
}
Expand All @@ -105,5 +109,28 @@ public static List<MentionDto> toList(List<Mention> mentions) {
}
}

// TODO: notice
@Builder
public record NoticeDto(
Long id,
String categoryCode,
String categoryViewName,
String title,
String description,
LocalDateTime createdDate
) {

public static NoticeDto of(@Nullable Notice notice) {
if (notice == null) {
return null;
}
return NoticeDto.builder()
.id(notice.getId())
.categoryCode(String.valueOf(notice.getType().getCode()))
.categoryViewName(notice.getType().getViewName())
.title(notice.getTitle().getValue())
.description(notice.getDescription().getValue())
.createdDate(notice.getCreatedDate())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.listywave.alarm.application.domain.AlarmType.COMMENT;
import static com.listywave.alarm.application.domain.AlarmType.MENTION;
import static com.listywave.alarm.application.domain.AlarmType.NOTICE;
import static com.listywave.alarm.application.domain.AlarmType.REPLY;
import static org.springframework.transaction.annotation.Propagation.REQUIRED;
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;
Expand Down Expand Up @@ -126,7 +127,7 @@ public void save(AlarmCreateEvent event) {
@Transactional(readOnly = true)
public List<AlarmFindResponse> findAllBy(Long userId) {
userRepository.getById(userId);
List<Alarm> alarms = alarmRepository.findAllBy(userId, LocalDateTime.now().minusDays(30));
List<Alarm> alarms = alarmRepository.findAllBy(userId, LocalDateTime.now().minusDays(30), NOTICE);
return AlarmFindResponse.toList(alarms);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.listywave.alarm.repository;

import com.listywave.alarm.application.domain.Alarm;
import com.listywave.alarm.application.domain.AlarmType;
import com.listywave.alarm.repository.custom.CustomAlarmRepository;
import com.listywave.common.exception.CustomException;
import com.listywave.common.exception.ErrorCode;
Expand Down Expand Up @@ -49,14 +50,13 @@ select case when count(*) > 0 then false else true end
left join ListEntity l on a.list = l
left join Comment c on a.comment = c
left join Reply r on a.reply = r
where a.receiveUserId = :receiveUserId and a.createdDate >= :thirtyDaysAgo
left join Notice n on a.notice = n
where a.createdDate >= :thirtyDaysAgo and (a.receiveUserId = :receiveUserId or a.type = :type)
order by a.createdDate desc
""")
List<Alarm> findAllBy(Long receiveUserId, LocalDateTime thirtyDaysAgo);
List<Alarm> findAllBy(Long receiveUserId, LocalDateTime thirtyDaysAgo, AlarmType type);

List<Alarm> findAllByReply(Reply reply);

void deleteByComment(Comment comment);

void deleteAllByCommentAndReceiveUserId(Comment comment, Long receiveUserId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.listywave.collection.application.domain;

import static com.listywave.common.exception.ErrorCode.INVALID_ACCESS;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.common.exception.CustomException;
import jakarta.persistence.Column;
Expand All @@ -10,9 +13,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static com.listywave.common.exception.ErrorCode.INVALID_ACCESS;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.listywave.collection.application.domain;

import static com.listywave.common.exception.ErrorCode.LENGTH_EXCEEDED_EXCEPTION;

import com.listywave.common.exception.CustomException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand All @@ -8,8 +10,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static com.listywave.common.exception.ErrorCode.LENGTH_EXCEEDED_EXCEPTION;

@Getter
@Embeddable
@EqualsAndHashCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public record FindFolderResponse(
List<FolderResponse> folders
) {

public static FindFolderResponse of(List<FolderResponse> list){
public static FindFolderResponse of(List<FolderResponse> list) {
return new FindFolderResponse(list);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.collection.presentation.dto;

public record FolderCreateRequest(
String folderName
String folderName
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.collection.presentation.dto;

public record FolderUpdateRequest(
String folderName
String folderName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.listywave.collection.application.domain.Folder;
import com.listywave.collection.repository.custom.CustomCollectionRepository;
import com.listywave.list.application.domain.list.ListEntity;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;

public interface CollectionRepository extends JpaRepository<Collect, Long>, CustomCollectionRepository {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.listywave.collection.repository.custom;

import com.listywave.collection.application.dto.FolderResponse;

import java.util.List;

public interface CustomFolderRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.listywave.collection.repository.custom.impl;

import static com.listywave.collection.application.domain.QCollect.*;
import static com.listywave.collection.application.domain.QFolder.*;
import static com.querydsl.jpa.JPAExpressions.*;
import static com.listywave.collection.application.domain.QCollect.collect;
import static com.listywave.collection.application.domain.QFolder.folder;
import static com.querydsl.jpa.JPAExpressions.select;

import com.listywave.collection.application.dto.FolderResponse;
import com.listywave.collection.repository.custom.CustomFolderRepository;
Expand All @@ -11,8 +11,8 @@
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import java.util.List;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class CustomFolderRepositoryImpl implements CustomFolderRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.listywave.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CustomException extends RuntimeException {

private final ErrorCode errorCode;

public CustomException(ErrorCode errorCode) {
super(errorCode.getDetail());
this.errorCode = errorCode;
}

public CustomException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum ErrorCode {
DUPLICATE_FOLDER_NAME_EXCEPTION(BAD_REQUEST, "중복된 폴더명입니다."),
NULL_OR_BLANK_EXCEPTION(BAD_REQUEST, "값이 null이거나 공백일 수 없습니다."),
NOT_EXIST_CODE(BAD_REQUEST, "존재하지 않는 코드입니다."),
ALREADY_SENT_ALARM_NOTICE(BAD_REQUEST, "이미 발송된 공지입니다."),

// S3
S3_DELETE_OBJECTS_EXCEPTION(INTERNAL_SERVER_ERROR, "S3의 이미지를 삭제 요청하는 과정에서 에러가 발생했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.listywave.list.application.domain.item.Item;
import com.listywave.list.application.domain.list.ListEntity;
import lombok.Builder;

import java.util.List;
import lombok.Builder;

@Builder
public record RecommendedListResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.listywave.notice.application.domain;

import static com.listywave.common.exception.ErrorCode.ALREADY_SENT_ALARM_NOTICE;
import static jakarta.persistence.CascadeType.ALL;
import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.common.exception.CustomException;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -67,4 +69,11 @@ public void update(NoticeType type, NoticeTitle title, NoticeDescription descrip
public void changeExposure() {
this.isExposed = !this.isExposed;
}

public void sendAlarm() {
if (this.didSendAlarm) {
throw new CustomException(ALREADY_SENT_ALARM_NOTICE);
}
this.didSendAlarm = true;
}
}
Loading

0 comments on commit 4c5c154

Please sign in to comment.