Skip to content

Commit

Permalink
Merge pull request #55 from taco-official/KL-154/알림-응답-형식-변경
Browse files Browse the repository at this point in the history
refactor(KL-154): use querydsl at notification
  • Loading branch information
min3m authored Aug 20, 2024
2 parents aec771a + 463418d commit 2a9c130
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;

import com.querydsl.core.annotations.QueryInit;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -38,6 +40,7 @@ public class Notification {

@OneToOne
@JoinColumn(name = "comment_id")
@QueryInit("product.user")
private Comment comment;

private Notification(final Comment comment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import taco.klkl.domain.comment.domain.Comment;
import taco.klkl.domain.notification.dao.NotificationRepository;
import taco.klkl.domain.notification.domain.Notification;
import taco.klkl.domain.notification.domain.QNotification;
import taco.klkl.domain.notification.dto.response.NotificationResponse;
import taco.klkl.domain.notification.exception.NotificationNotFoundException;
import taco.klkl.domain.user.domain.QUser;
import taco.klkl.domain.user.domain.User;
import taco.klkl.global.util.UserUtil;

Expand All @@ -29,12 +33,23 @@
public class NotificationServiceImpl implements NotificationService {

private final NotificationRepository notificationRepository;
private final JPAQueryFactory queryFactory;
private final UserUtil userUtil;

@Override
public List<NotificationResponse> findAllNotifications() {
final QNotification notification = QNotification.notification;
final QUser user = QUser.user;
final User receiver = findReceiver();
final List<Notification> notifications = notificationRepository.findAllByComment_Product_User(receiver);

final List<Notification> notifications = queryFactory
.selectFrom(notification)
.join(notification.comment.product.user, user)
.where(user.id.eq(receiver.getId()))
.orderBy(notification.createdAt.desc(),
notification.id.desc())
.fetch();

return notifications.stream()
.map(NotificationResponse::from)
.toList();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/taco/klkl/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Getter
@NoArgsConstructor
@Entity(name = "user")
@Entity(name = "klkl_user")
public class User {

@Id
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/database/data.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* User */
INSERT INTO user(user_id, profile, name, gender, age, description, created_at)
INSERT INTO klkl_user(user_id, profile, name, gender, age, description, created_at)
VALUES (1, 'image/test.jpg', 'testUser', '', 20, '테스트입니다.', now());

/* Like */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.transaction.annotation.Transactional;

import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;

import taco.klkl.domain.category.domain.Category;
import taco.klkl.domain.category.domain.CategoryName;
import taco.klkl.domain.category.domain.Subcategory;
import taco.klkl.domain.category.domain.SubcategoryName;
import taco.klkl.domain.comment.domain.Comment;
import taco.klkl.domain.notification.dao.NotificationRepository;
import taco.klkl.domain.notification.domain.Notification;
import taco.klkl.domain.notification.domain.QNotification;
import taco.klkl.domain.notification.dto.response.NotificationResponse;
import taco.klkl.domain.notification.exception.NotificationNotFoundException;
import taco.klkl.domain.product.domain.Product;
Expand All @@ -38,6 +44,7 @@
import taco.klkl.domain.region.domain.Region;
import taco.klkl.domain.region.domain.RegionType;
import taco.klkl.domain.user.domain.Gender;
import taco.klkl.domain.user.domain.QUser;
import taco.klkl.domain.user.domain.User;
import taco.klkl.global.util.UserUtil;

Expand All @@ -48,6 +55,9 @@ public class NotificationServiceTest {
@Mock
private NotificationRepository notificationRepository;

@Mock
private JPAQueryFactory queryFactory;

@Mock
private UserUtil userUtil;

Expand Down Expand Up @@ -113,14 +123,22 @@ public void setUp() {
@DisplayName("댓글 알림이 비어있지 않을경우 조회 성공")
public void testFindAllNotifications() {
//given
JPAQuery<Notification> mockQuery = mock(JPAQuery.class);
List<Notification> notificationList = List.of(mockNotification);
QNotification notification = QNotification.notification;
QUser user = QUser.user;

when(userUtil.findTestUser()).thenReturn(mockUser);
when(mockNotification.getId()).thenReturn(1L);
when(mockNotification.getIsRead()).thenReturn(false);
when(mockNotification.getCreatedAt()).thenReturn(LocalDateTime.now());
when(mockNotification.getComment()).thenReturn(comment);

List<Notification> notificationList = List.of(mockNotification);
when(notificationRepository.findAllByComment_Product_User(mockUser)).thenReturn(notificationList);
when(queryFactory.selectFrom(any(QNotification.class))).thenReturn(mockQuery);
when(mockQuery.join(notification.comment.product.user, user)).thenReturn(mockQuery);
when(mockQuery.where(any(BooleanExpression.class))).thenReturn(mockQuery);
when(mockQuery.orderBy(any(OrderSpecifier.class), any(OrderSpecifier.class))).thenReturn(mockQuery);
when(mockQuery.fetch()).thenReturn(notificationList);

//when
List<NotificationResponse> response = notificationService.findAllNotifications();
Expand All @@ -135,10 +153,18 @@ public void testFindAllNotifications() {
@DisplayName("댓글 알림이 빈경우 조회 성공")
public void testGetBlankNotifications() {
//given
JPAQuery<Notification> mockQuery = mock(JPAQuery.class);
List<Notification> notificationList = Collections.emptyList();
QNotification notification = QNotification.notification;
QUser user = QUser.user;

when(userUtil.findTestUser()).thenReturn(mockUser);

List<Notification> notificationList = Collections.emptyList();
when(notificationRepository.findAllByComment_Product_User(mockUser)).thenReturn(notificationList);
when(queryFactory.selectFrom(any(QNotification.class))).thenReturn(mockQuery);
when(mockQuery.join(notification.comment.product.user, user)).thenReturn(mockQuery);
when(mockQuery.where(any(BooleanExpression.class))).thenReturn(mockQuery);
when(mockQuery.orderBy(any(OrderSpecifier.class), any(OrderSpecifier.class))).thenReturn(mockQuery);
when(mockQuery.fetch()).thenReturn(notificationList);

//when
List<NotificationResponse> response = notificationService.findAllNotifications();
Expand Down

0 comments on commit 2a9c130

Please sign in to comment.