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
4 changes: 3 additions & 1 deletion src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,6 @@ include::votes.adoc[]

include::comments.adoc[]

include::comment-likes.adoc[]
include::comment-likes.adoc[]

include::notifications.adoc[]
7 changes: 7 additions & 0 deletions src/docs/asciidoc/notifications.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[์•Œ๋ฆผ-API]]
== ์•Œ๋ฆผ API

[[์•Œ๋ฆผ-์กฐํšŒ]]
=== `GET` ์•Œ๋ฆผ ์กฐํšŒ

operation::comment-controller-test/find-notifications[snippets='http-request,curl-request,path-parameters,request-headers,query-parameters,http-response,response-fields']
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,40 @@
import com.chooz.commentLike.domain.CommentLike;
import com.chooz.commentLike.domain.CommentLikeRepository;
import com.chooz.commentLike.presentation.dto.CommentLikeIdResponse;
import com.chooz.common.event.EventPublisher;
import com.chooz.common.exception.BadRequestException;
import com.chooz.common.exception.ErrorCode;
import com.chooz.notification.domain.event.CommentLikedEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Slf4j
public class CommentLikeCommandService {

private final CommentLikeRepository commentLikeRepository;
private final EventPublisher eventPublisher;

public CommentLikeIdResponse createCommentLike(Long commentId, Long userId) {
if(commentLikeRepository.existsByCommentIdAndUserId(commentId, userId)){
throw new BadRequestException(ErrorCode.COMMENT_LIKE_NOT_FOUND);
}
CommentLike commentLike = commentLikeRepository.save(CommentLike.create(commentId, userId));

eventPublisher.publish(new CommentLikedEvent(
commentId,
commentLike.getId(),
userId,
LocalDateTime.now()
));
return new CommentLikeIdResponse(
commentLikeRepository.save(CommentLike.create(commentId, userId)).getId(),
commentLike.getId(),
commentLikeRepository.countByCommentId(commentId)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.chooz.notification.application;

import com.chooz.notification.application.dto.CommentLikedContent;
import com.chooz.notification.domain.Notification;
import com.chooz.notification.domain.TargetType;
import com.chooz.notification.domain.event.CommentLikedEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@RequiredArgsConstructor
public class CommentLikeNotificationListener {

private final NotificationCommandService notificationCommandService;
private final NotificationContentAssembler notificationContentAssembler;

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onCommentLiked(CommentLikedEvent e) {
CommentLikedContent commentLikedContent = notificationContentAssembler.forCommentLiked(e.commentId(), e.likerId());
Notification.create(
commentLikedContent.getCommentAuthorId(),
commentLikedContent.getCommentAuthorName(),
e.likerId(),
commentLikedContent.getActorName(),
commentLikedContent.getActorProfileImageUrl(),
e.commentId(),
TargetType.COMMENT,
commentLikedContent.getTargetThumbnailUrl(),
e.eventAt()
).ifPresent(notificationCommandService::create);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.chooz.notification.application;

import com.chooz.notification.domain.Notification;
import com.chooz.notification.domain.NotificationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class NotificationCommandService {

private final NotificationRepository notificationRepository;

@Transactional(propagation = Propagation.REQUIRES_NEW)
public Notification create(Notification notification) {
return notificationRepository.save(notification);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.chooz.notification.application;

import com.chooz.common.exception.BadRequestException;
import com.chooz.common.exception.ErrorCode;
import com.chooz.notification.application.dto.CommentLikedContent;
import com.chooz.notification.application.dto.TargetPostDto;
import com.chooz.notification.application.dto.TargetUserDto;
import com.chooz.notification.domain.NotificationQueryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class NotificationContentAssembler {

private final NotificationQueryRepository notificationQueryDslRepository;

public CommentLikedContent forCommentLiked(Long commentId, Long likerId) {
TargetUserDto targetUserDto = notificationQueryDslRepository.getUser(likerId)
.orElseThrow(() -> new BadRequestException(ErrorCode.USER_NOT_FOUND));
TargetUserDto commentAuthorDto = notificationQueryDslRepository.getUserByCommentId(commentId)
.orElseThrow(() -> new BadRequestException(ErrorCode.USER_NOT_FOUND));
TargetPostDto targetPostDto = notificationQueryDslRepository.getPost(commentId)
.orElseThrow(() -> new BadRequestException(ErrorCode.POST_NOT_FOUND));

return new CommentLikedContent(
targetUserDto.nickname(),
targetUserDto.profileUrl(),
targetPostDto.imageUrl(),
commentAuthorDto.id(),
commentAuthorDto.nickname()
);
}

// public NotificationContent forVoteClosed(Long postId) {
// String title = postPort.getPostTitle(postId).orElse("ํˆฌํ‘œ ๋งˆ๊ฐ");
// String body = "์ฐธ์—ฌํ•œ ํˆฌํ‘œ๊ฐ€ ๋งˆ๊ฐ๋˜์—ˆ์–ด์š”.";
// String thumbnail = postPort.getPostThumbnailUrl(postId).orElse(null);
// return new NotificationContent(title, body, thumbnail);
// }
//
// public NotificationContent forPostParticipated(Long postId, Long voterId) {
// String title = postPort.getPostTitle(postId).orElse("์ƒˆ๋กœ์šด ์ฐธ์—ฌ");
// String voter = userPort.getDisplayName(voterId).orElse("๋ˆ„๊ตฐ๊ฐ€");
// String body = voter + "๋‹˜์ด ๋‚ด ํˆฌํ‘œ์— ์ฐธ์—ฌํ–ˆ์–ด์š”.";
// String thumbnail = userPort.getAvatarUrl(voterId).orElse(null);
// return new NotificationContent(title, body, thumbnail);
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.chooz.notification.application;

import com.chooz.common.dto.CursorBasePaginatedResponse;
import com.chooz.notification.application.dto.NotificationDto;
import com.chooz.notification.domain.Notification;
import com.chooz.notification.domain.NotificationQueryRepository;
import com.chooz.notification.presentation.dto.NotificationResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class NotificationQueryService {

private final NotificationQueryRepository notificationQueryRepository;

// public CursorBasePaginatedResponse<NotificationResponse> findNotifications(Long userId, Long cursor, int size) {
// Slice<Notification> notificationSlice = notificationQueryRepository.findNotifications(userId, cursor, PageRequest.ofSize(size));
// return CursorBasePaginatedResponse.of(notificationSlice.map(NotificationResponse::of));
// }
public CursorBasePaginatedResponse<NotificationResponse> findNotifications(Long userId, Long cursor, int size) {
Slice<NotificationDto> notificationSlice = notificationQueryRepository.findNotifications(userId, cursor, PageRequest.ofSize(size));
return CursorBasePaginatedResponse.of(notificationSlice.map(NotificationResponse::of));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.chooz.notification.application.dto;

import lombok.Getter;

@Getter
public class CommentLikedContent extends NotificationContent {

private final Long commentAuthorId;
private final String commentAuthorName;

public CommentLikedContent(
String actorName,
String actorProfileImageUrl,
String targetThumbnailUrl,
Long commentAuthorId,
String commentAuthorName
) {
super(actorName, targetThumbnailUrl, actorProfileImageUrl);
this.commentAuthorId = commentAuthorId;
this.commentAuthorName = commentAuthorName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.chooz.notification.application.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public abstract class NotificationContent {
private final String actorName;
private final String actorProfileImageUrl;
private final String targetThumbnailUrl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.chooz.notification.application.dto;


import com.chooz.notification.domain.TargetType;
import com.querydsl.core.annotations.QueryProjection;

import java.time.LocalDateTime;

@QueryProjection
public record NotificationDto(
Long id,
Long postId,
Long receiverId,
String receiverNickname,
Long actorId,
String actorNickname,
String actorProfileUrl,
Long targetId,
TargetType targetType,
String targetImageUrl,
boolean isRead,
LocalDateTime eventAt
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.chooz.notification.application.dto;


import com.querydsl.core.annotations.QueryProjection;

@QueryProjection
public record TargetPostDto(
Long id,
String imageUrl
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.chooz.notification.application.dto;

import com.querydsl.core.annotations.QueryProjection;

@QueryProjection
public record TargetUserDto(
Long id,
String nickname,
String profileUrl
) {}
25 changes: 25 additions & 0 deletions src/main/java/com/chooz/notification/domain/Actor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.chooz.notification.domain;


import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Embeddable
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Actor {
@Column(name = "actor_id", nullable = false)
private Long id;

@Column(name = "actor_nickname", nullable = false)
private String nickname;

@Column(name = "actor_profile_url", nullable = false)
private String profileUrl;
}
78 changes: 78 additions & 0 deletions src/main/java/com/chooz/notification/domain/Notification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.chooz.notification.domain;

import com.chooz.common.domain.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Optional;

@Getter
@Entity
@Table(name = "notifications")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class Notification extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Embedded
private Receiver receiver;

@Embedded
private Actor actor;

@Embedded
private Target target;

@Column(name = "is_read", nullable = false)
private boolean isRead;

@Column(name = "event_at", nullable = false)
private LocalDateTime eventAt;

public static Optional<Notification> create(
Long receiverId,
String receiverNickname,
Long actorId,
String actorNickname,
String actorProfileUrl,
Long targetId,
TargetType targetType,
String targetImageUrl,
LocalDateTime eventAt
) {
if (checkMine(actorId, receiverId)) {
return Optional.empty();
}
return Optional.of(Notification.builder()
.receiver(new Receiver(receiverId, receiverNickname))
.actor(new Actor(actorId, actorNickname, actorProfileUrl))
.target(new Target(targetId, targetType, targetImageUrl))
.isRead(false)
.eventAt(eventAt)
.build());
}
private static boolean checkMine(Long actorId, Long receiverId) {
return actorId != null && actorId.equals(receiverId);
}

public void markRead() {
if (!isRead) {
this.isRead = true;
}
}
}
Loading