Skip to content

Commit a6f9154

Browse files
committed
feat: 기관의 자신에게 온 쪽지 조회 메서드 구현
- 서비스 레이어 구현 - 엔드 포인트 구
1 parent a709adc commit a6f9154

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.somemore.note.controller;
2+
3+
import com.somemore.auth.annotation.CurrentUser;
4+
import com.somemore.global.common.response.ApiResponse;
5+
import com.somemore.note.repository.mapper.NoteReceiverViewForCenter;
6+
import com.somemore.note.usecase.NoteQueryUseCase;
7+
import io.swagger.v3.oas.annotations.Operation;
8+
import io.swagger.v3.oas.annotations.tags.Tag;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.Pageable;
12+
import org.springframework.security.access.annotation.Secured;
13+
import org.springframework.web.bind.annotation.*;
14+
15+
import java.util.UUID;
16+
17+
@Tag(name = "Note Query API", description = "쪽지 조회 API")
18+
@RequiredArgsConstructor
19+
@RequestMapping("/api/note")
20+
@RestController
21+
public class NoteQueryApiController {
22+
23+
private final NoteQueryUseCase noteQueryUseCase;
24+
25+
@Secured("ROLE_CENTER")
26+
@Operation(summary = "기관의 자신에게 온 쪽지 조회")
27+
@GetMapping("/center")
28+
public ApiResponse<Page<NoteReceiverViewForCenter>> getNotesByCenterId(@CurrentUser UUID centerId, Pageable pageable) {
29+
30+
Page<NoteReceiverViewForCenter> response = noteQueryUseCase.getNotesForCenter(centerId, pageable);
31+
32+
return ApiResponse.ok(200, response, "내 쪽지 조회 성공");
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.somemore.note.service;
2+
3+
import com.somemore.note.repository.NoteRepository;
4+
import com.somemore.note.repository.mapper.NoteReceiverViewForCenter;
5+
import com.somemore.note.usecase.NoteQueryUseCase;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.Pageable;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
import java.util.UUID;
13+
14+
@RequiredArgsConstructor
15+
@Service
16+
@Transactional(readOnly = true)
17+
public class NoteQueryService implements NoteQueryUseCase {
18+
19+
private final NoteRepository noteRepository;
20+
21+
@Override
22+
public Page<NoteReceiverViewForCenter> getNotesForCenter(UUID centerId, Pageable pageable) {
23+
return noteRepository.findNotesByReceiverIsCenter(centerId, pageable);
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.somemore.note.usecase;
2+
3+
import com.somemore.note.repository.mapper.NoteReceiverViewForCenter;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
7+
import java.util.UUID;
8+
9+
public interface NoteQueryUseCase {
10+
Page<NoteReceiverViewForCenter> getNotesForCenter(UUID centerId, Pageable pageable);
11+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.somemore.note.controller;
2+
3+
import com.somemore.ControllerTestSupport;
4+
import com.somemore.WithMockCustomUser;
5+
import com.somemore.note.repository.mapper.NoteReceiverViewForCenter;
6+
import com.somemore.note.usecase.NoteQueryUseCase;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.boot.test.mock.mockito.MockBean;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.PageImpl;
12+
import org.springframework.data.domain.PageRequest;
13+
import org.springframework.data.domain.Pageable;
14+
import org.springframework.http.MediaType;
15+
16+
import java.util.List;
17+
import java.util.UUID;
18+
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.ArgumentMatchers.eq;
21+
import static org.mockito.Mockito.when;
22+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25+
26+
class NoteQueryApiControllerTest extends ControllerTestSupport {
27+
28+
@MockBean
29+
private NoteQueryUseCase noteQueryUseCase;
30+
31+
@DisplayName("기관은 자신에게 온 쪽지를 페이지 형태로 확인할 수 있다. (Controller)")
32+
@Test
33+
@WithMockCustomUser(role = "CENTER")
34+
void getNotesByCenterId() throws Exception {
35+
// given
36+
Pageable pageable = PageRequest.of(0, 6);
37+
List<NoteReceiverViewForCenter> notes = List.of(
38+
NoteReceiverViewForCenter.builder()
39+
.id(1L)
40+
.title("문의 드립니다.")
41+
.senderName("봉사왕")
42+
.isRead(false)
43+
.build(),
44+
NoteReceiverViewForCenter.builder()
45+
.id(2L)
46+
.title("긴급 문의")
47+
.senderName("지원자")
48+
.isRead(true)
49+
.build()
50+
);
51+
Page<NoteReceiverViewForCenter> mockResponse = new PageImpl<>(notes, pageable, notes.size());
52+
53+
when(noteQueryUseCase.getNotesForCenter(any(UUID.class), eq(pageable)))
54+
.thenReturn(mockResponse);
55+
56+
// When & Then
57+
mockMvc.perform(get("/api/note/center")
58+
.param("page", "0")
59+
.param("size", "6")
60+
.accept(MediaType.APPLICATION_JSON))
61+
.andExpect(status().isOk())
62+
.andExpect(jsonPath("$.code").value(200))
63+
.andExpect(jsonPath("$.message").value("내 쪽지 조회 성공"))
64+
.andExpect(jsonPath("$.data.content").isArray())
65+
.andExpect(jsonPath("$.data.content[0].id").value(1))
66+
.andExpect(jsonPath("$.data.content[0].title").value("문의 드립니다."))
67+
.andExpect(jsonPath("$.data.content[0].sender_name").value("봉사왕"))
68+
.andExpect(jsonPath("$.data.content[0].is_read").value(false))
69+
.andExpect(jsonPath("$.data.content[1].id").value(2))
70+
.andExpect(jsonPath("$.data.content[1].title").value("긴급 문의"))
71+
.andExpect(jsonPath("$.data.content[1].sender_name").value("지원자"))
72+
.andExpect(jsonPath("$.data.content[1].is_read").value(true))
73+
.andExpect(jsonPath("$.data.pageable.pageNumber").value(0))
74+
.andExpect(jsonPath("$.data.pageable.pageSize").value(6))
75+
.andExpect(jsonPath("$.data.totalElements").value(2));
76+
}
77+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.somemore.note.service;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.note.domain.Note;
5+
import com.somemore.note.repository.NoteRepository;
6+
import com.somemore.note.repository.mapper.NoteReceiverViewForCenter;
7+
import com.somemore.volunteer.domain.Volunteer;
8+
import com.somemore.volunteer.repository.VolunteerJpaRepository;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.data.domain.Page;
14+
import org.springframework.data.domain.PageRequest;
15+
import org.springframework.data.domain.Pageable;
16+
import org.springframework.transaction.annotation.Transactional;
17+
18+
import java.util.UUID;
19+
20+
import static com.somemore.auth.oauth.OAuthProvider.NAVER;
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
@Transactional
24+
class NoteQueryServiceTest extends IntegrationTestSupport {
25+
26+
@Autowired
27+
private NoteQueryService noteQueryService;
28+
29+
@Autowired
30+
private NoteRepository noteRepository;
31+
32+
@Autowired
33+
private VolunteerJpaRepository volunteerJpaRepository;
34+
35+
private UUID centerId;
36+
37+
@BeforeEach
38+
void setUp() {
39+
centerId = UUID.randomUUID();
40+
createTestNotes(centerId);
41+
}
42+
43+
@DisplayName("기관은 자신에게 온 쪽지를 페이지 형태로 확인할 수 있다. (Service)")
44+
@Test
45+
void getNotesForCenter() {
46+
//given
47+
Pageable pageable = PageRequest.of(1, 6);
48+
49+
//when
50+
Page<NoteReceiverViewForCenter> result = noteQueryService.getNotesForCenter(centerId, pageable);
51+
52+
//then
53+
assertThat(result).isNotNull();
54+
assertThat(result.getContent()).hasSize(6);
55+
assertThat(result.getTotalElements()).isEqualTo(15);
56+
assertThat(result.getContent().getFirst().title()).isEqualTo("Note 9");
57+
}
58+
59+
private void createTestNotes(UUID centerId) {
60+
for (int i = 1; i <= 15; i++) {
61+
Volunteer volunteer = createVolunteer();
62+
volunteerJpaRepository.save(volunteer);
63+
64+
Note note = Note.create(volunteer.getId(), centerId, "Note " + i, "내용");
65+
noteRepository.save(note);
66+
}
67+
}
68+
69+
private Volunteer createVolunteer() {
70+
Volunteer volunteer = Volunteer.createDefault(NAVER, UUID.randomUUID().toString());
71+
return volunteerJpaRepository.save(volunteer);
72+
}
73+
}

0 commit comments

Comments
 (0)