Skip to content
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

Feature/#141-알림 목록 조회 API 구현 #162

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion backend/emm-sale/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ include::{snippets}/find-profile/http-response.adoc[]
.HTTP response 설명
include::{snippets}/find-profile/response-fields.adoc[]


== Event

=== `GET` : 행사 상세정보 조회
Expand Down Expand Up @@ -257,3 +256,13 @@ include::{snippets}/find-notification/response-fields.adoc[]
.HTTP response
include::{snippets}/find-notification/http-response.adoc[]

=== `GET` : 사용자가 받은 모든 알림 목록 조회

.HTTP request
include::{snippets}/find-all-notifications/http-request.adoc[]

.HTTP response 설명
include::{snippets}/find-all-notifications/response-fields.adoc[]

.HTTP response
include::{snippets}/find-all-notifications/http-response.adoc[]
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.emmsale.notification.api;

import com.emmsale.member.domain.Member;
import com.emmsale.notification.application.NotificationCommandService;
import com.emmsale.notification.application.NotificationQueryService;
import com.emmsale.notification.application.dto.FcmTokenRequest;
import com.emmsale.notification.application.dto.NotificationModifyRequest;
import com.emmsale.notification.application.dto.NotificationRequest;
import com.emmsale.notification.application.dto.NotificationResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -49,4 +51,10 @@ public NotificationResponse find(
) {
return notificationQueryService.findNotificationBy(notificationId);
}

@GetMapping("/notifications")
@ResponseStatus(HttpStatus.OK)
public List<NotificationResponse> findAll(final Member member) {
return notificationCommandService.findAllNotifications(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.emmsale.notification.exception.NotificationExceptionType.BAD_REQUEST_MEMBER_ID;
import static com.emmsale.notification.exception.NotificationExceptionType.NOT_FOUND_NOTIFICATION;

import com.emmsale.member.domain.Member;
import com.emmsale.member.domain.MemberRepository;
import com.emmsale.member.exception.MemberException;
import com.emmsale.notification.application.dto.FcmTokenRequest;
Expand All @@ -16,6 +17,7 @@
import com.emmsale.notification.domain.NotificationRepository;
import com.emmsale.notification.exception.NotificationException;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -95,4 +97,13 @@ public void modify(

savedNotification.modifyStatus(notificationModifyRequest.getUpdatedStatus());
}

public List<NotificationResponse> findAllNotifications(final Member member) {
final List<Notification> notifications = notificationRepository.findAllByReceiverId(
member.getId());

return notifications.stream()
.map(NotificationResponse::from)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.emmsale.notification.domain;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NotificationRepository extends JpaRepository<Notification, Long> {

List<Notification> findAllByReceiverId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.emmsale.notification.application.dto.NotificationRequest;
import com.emmsale.notification.application.dto.NotificationResponse;
import com.emmsale.notification.domain.NotificationStatus;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand Down Expand Up @@ -184,4 +185,35 @@ void test_find() throws Exception {
.andDo(print())
.andDo(document("find-notification", responseFields, pathParameters));
}

@Test
@DisplayName("사용자가 받은 알림의 목록을 성공적으로 반환하면 200 OK를 반환한다.")
void test_findAll() throws Exception {
//given
final String accessToken = "Bearer access_token";

final long memberId = 1L;

final List<NotificationResponse> expectResponses = List.of(
new NotificationResponse(931L, 3342L, memberId, "같이 가요~", 312L),
new NotificationResponse(932L, 1345L, memberId, "소통해요~", 123L)
);

when(notificationCommandService.findAllNotifications(any())).thenReturn(expectResponses);

final ResponseFieldsSnippet responseFields = responseFields(
fieldWithPath("[].notificationId").description("저장된 알림 ID"),
fieldWithPath("[].senderId").description("보내는 사람 ID"),
fieldWithPath("[].receiverId").description("받는 사람 ID"),
fieldWithPath("[].message").description("알림 보낼 때 메시지"),
fieldWithPath("[].eventId").description("행사 ID")
);

//when & then
mockMvc.perform(get("/notifications")
.header("Authorization", accessToken))
.andExpect(status().isOk())
.andDo(print())
.andDo(document("find-all-notifications", responseFields));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.emmsale.helper.ServiceIntegrationTestHelper;
import com.emmsale.member.domain.Member;
import com.emmsale.member.domain.MemberRepository;
import com.emmsale.notification.application.dto.FcmTokenRequest;
import com.emmsale.notification.application.dto.NotificationModifyRequest;
import com.emmsale.notification.application.dto.NotificationRequest;
Expand All @@ -18,12 +20,15 @@
import com.emmsale.notification.domain.NotificationStatus;
import com.emmsale.notification.exception.NotificationException;
import com.emmsale.notification.exception.NotificationExceptionType;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;

@Sql("/data-test.sql")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServiceTest에서는 ServiceIntegerationTest를 상속받고 있어서, 굳이 @Sql(data-test.sql)을 명시할 필요는 없을거에요

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅇㅈ

class NotificationCommandServiceTest extends ServiceIntegrationTestHelper {

@Autowired
Expand All @@ -32,6 +37,8 @@ class NotificationCommandServiceTest extends ServiceIntegrationTestHelper {
private FcmTokenRepository fcmTokenRepository;
@Autowired
private NotificationRepository notificationRepository;
@Autowired
private MemberRepository memberRepository;

@Test
@DisplayName("create() : 알림을 새로 생성할 수 있다.")
Expand Down Expand Up @@ -157,4 +164,30 @@ void test_modify() throws Exception {
//then
assertEquals(request.getUpdatedStatus(), updatedNotification.getStatus());
}

@Test
@DisplayName("Member가 받은 모든 알림 목록을 조회한다.")
void test_findAllNotifications() {
//given
final Member sender = memberRepository.findById(1L).get();
final Member receiver = memberRepository.findById(2L).get();

final String message1 = "message123";
final String message2 = "message321";

notificationRepository.save(
new Notification(sender.getId(), receiver.getId(), 123L, message1)
);
notificationRepository.save(
new Notification(sender.getId(), receiver.getId(), 321L, message2)
);

//when
final List<NotificationResponse> notifications = notificationCommandService.findAllNotifications(
receiver);

//then
assertThat(notifications).extracting("message", String.class)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요기는 Notification::getMessage를 사용했어도 좋았을 것 같아요.

.containsExactly(message1, message2);
}
}
1 change: 1 addition & 0 deletions backend/emm-sale/src/test/resources/data-test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ truncate table tag;
truncate table event_tag;
truncate table member_tag;
truncate table event_member;
truncate table notification;

insert into activity(id, type, name)
values (1, 'CLUB', 'YAPP');
Expand Down
Loading