Skip to content

Commit 88c6a50

Browse files
refactor: 관리자 이벤트 대시보드 페이징 처리
* refactor: admin dashboard paging * refactor: 설정롤백
1 parent be91708 commit 88c6a50

6 files changed

Lines changed: 76 additions & 42 deletions

File tree

backend/src/main/java/com/back/api/event/controller/AdminEventApi.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.back.api.event.controller;
22

3-
import java.util.List;
4-
3+
import org.springframework.data.domain.Page;
54
import org.springframework.web.bind.annotation.PathVariable;
65
import org.springframework.web.bind.annotation.RequestBody;
6+
import org.springframework.web.bind.annotation.RequestParam;
77

88
import com.back.api.event.dto.request.EventCreateRequest;
99
import com.back.api.event.dto.request.EventUpdateRequest;
@@ -62,10 +62,16 @@ ApiResponse<Void> deleteEvent(
6262

6363
@Operation(
6464
summary = "전체 이벤트 대시보드 현황 조회 (관리자)",
65-
description = "관리자 대시보드에서 모든 이벤트의 전체 현황을 조회합니다. "
65+
description = "관리자 대시보드에서 모든 이벤트의 전체 현황을 페이징을 통해 조회합니다. "
6666
+ "각 이벤트별 상태, 사전등록 인원 수, 총 판매 좌석, 총 판매 금액을 포함합니다."
6767
)
68-
ApiResponse<List<AdminEventDashboardResponse>> getAllEventsDashboard();
68+
ApiResponse<Page<AdminEventDashboardResponse>> getAllEventsDashboard(
69+
@Parameter(description = "페이지 번호 (0부터 시작)")
70+
@RequestParam(defaultValue = "0") int page,
71+
72+
@Parameter(description = "페이지 크기")
73+
@RequestParam(defaultValue = "20") int size
74+
);
6975

7076

7177
@Operation(

backend/src/main/java/com/back/api/event/controller/AdminEventController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.back.api.event.controller;
22

3-
import java.util.List;
4-
3+
import org.springframework.data.domain.Page;
54
import org.springframework.security.access.prepost.PreAuthorize;
65
import org.springframework.web.bind.annotation.DeleteMapping;
76
import org.springframework.web.bind.annotation.GetMapping;
@@ -10,6 +9,7 @@
109
import org.springframework.web.bind.annotation.PutMapping;
1110
import org.springframework.web.bind.annotation.RequestBody;
1211
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestParam;
1313
import org.springframework.web.bind.annotation.RestController;
1414

1515
import com.back.api.event.dto.request.EventCreateRequest;
@@ -57,8 +57,14 @@ public ApiResponse<Void> deleteEvent(
5757

5858
@Override
5959
@GetMapping("/dashboard")
60-
public ApiResponse<List<AdminEventDashboardResponse>> getAllEventsDashboard() {
61-
List<AdminEventDashboardResponse> responses = adminEventService.getAllEventsDashboard();
60+
public ApiResponse<Page<AdminEventDashboardResponse>> getAllEventsDashboard(
61+
@RequestParam(defaultValue = "0") int page,
62+
@RequestParam(defaultValue = "20") int size
63+
) {
64+
Page<AdminEventDashboardResponse> responses = adminEventService.getAllEventsDashboard(
65+
page,
66+
size
67+
);
6268
return ApiResponse.ok("이벤트 현황 조회 성공", responses);
6369
}
6470

backend/src/main/java/com/back/api/event/service/AdminEventService.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.back.api.event.service;
22

3-
import java.util.List;
4-
import java.util.stream.Collectors;
5-
3+
import org.springframework.data.domain.Page;
4+
import org.springframework.data.domain.PageRequest;
5+
import org.springframework.data.domain.Pageable;
66
import org.springframework.stereotype.Service;
77
import org.springframework.transaction.annotation.Transactional;
88

@@ -44,11 +44,11 @@ public void deleteEvent(Long eventId) {
4444
eventService.deleteEvent(eventId);
4545
}
4646

47-
public List<AdminEventDashboardResponse> getAllEventsDashboard() {
48-
List<Event> events = eventRepository.findAll();
47+
public Page<AdminEventDashboardResponse> getAllEventsDashboard(int page, int size) {
48+
Pageable pageable = PageRequest.of(page, size);
49+
Page<Event> eventPage = eventRepository.findAll(pageable);
4950

50-
return events.stream()
51-
.map(event -> {
51+
return eventPage.map(event -> {
5252
Long eventId = event.getId();
5353

5454
// 1. 이벤트별 현재 사전등록 인원 수 조회
@@ -72,8 +72,7 @@ public List<AdminEventDashboardResponse> getAllEventsDashboard() {
7272
totalSalesAmount != null ? totalSalesAmount : 0L,
7373
event.isDeleted()
7474
);
75-
})
76-
.collect(Collectors.toList());
75+
});
7776
}
7877

7978
@Transactional(readOnly = true)

backend/src/main/java/com/back/global/init/UserDataInit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private List<User> createTestUsers(int count) {
6363
.email("test" + i + "@test.com")
6464
.password(passwordEncoder.encode("abc12345"))
6565
.nickname("test" + i)
66-
.fullName("test2")
66+
.fullName("test" + i)
6767
.role(UserRole.NORMAL)
6868
.birthDate(LocalDate.of(2000, 1, 1))
6969
.activeStatus(UserActiveStatus.ACTIVE)

backend/src/test/java/com/back/api/event/controller/AdminEventControllerTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,16 @@ void getAllEventsDashboard_Success() throws Exception {
295295
.header("Authorization", "Bearer " + token))
296296
.andDo(print())
297297
.andExpect(status().isOk())
298-
.andExpect(jsonPath("$.data", hasSize(2)))
299-
.andExpect(jsonPath("$.data[0].title", notNullValue()))
300-
.andExpect(jsonPath("$.data[0].status", notNullValue()))
301-
.andExpect(jsonPath("$.data[0].preRegisterCount", notNullValue()))
302-
.andExpect(jsonPath("$.data[0].totalSoldSeats", notNullValue()))
303-
.andExpect(jsonPath("$.data[0].totalSalesAmount", notNullValue()));
298+
.andExpect(jsonPath("$.data.content", hasSize(2)))
299+
.andExpect(jsonPath("$.data.content[0].title", notNullValue()))
300+
.andExpect(jsonPath("$.data.content[0].status", notNullValue()))
301+
.andExpect(jsonPath("$.data.content[0].preRegisterCount", notNullValue()))
302+
.andExpect(jsonPath("$.data.content[0].totalSoldSeats", notNullValue()))
303+
.andExpect(jsonPath("$.data.content[0].totalSalesAmount", notNullValue()))
304+
.andExpect(jsonPath("$.data.totalElements").value(2))
305+
.andExpect(jsonPath("$.data.totalPages").value(1))
306+
.andExpect(jsonPath("$.data.size").value(20))
307+
.andExpect(jsonPath("$.data.number").value(0));;
304308
}
305309

306310
@Test
@@ -311,7 +315,9 @@ void getAllEventsDashboard_EmptyList() throws Exception {
311315
.header("Authorization", "Bearer " + token))
312316
.andDo(print())
313317
.andExpect(status().isOk())
314-
.andExpect(jsonPath("$.data", hasSize(0)));
318+
.andExpect(jsonPath("$.data.content", hasSize(0)))
319+
.andExpect(jsonPath("$.data.totalElements").value(0))
320+
.andExpect(jsonPath("$.data.totalPages").value(0));
315321
}
316322
}
317323

backend/src/test/java/com/back/api/event/service/AdminEventServiceTest.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.mockito.BDDMockito.*;
55

66
import java.util.List;
7-
import java.util.Optional;
87

98
import org.junit.jupiter.api.DisplayName;
109
import org.junit.jupiter.api.Nested;
@@ -13,20 +12,21 @@
1312
import org.mockito.InjectMocks;
1413
import org.mockito.Mock;
1514
import org.mockito.junit.jupiter.MockitoExtension;
15+
import org.springframework.data.domain.Page;
16+
import org.springframework.data.domain.PageImpl;
17+
import org.springframework.data.domain.PageRequest;
18+
import org.springframework.data.domain.Pageable;
1619

1720
import com.back.api.event.dto.request.EventCreateRequest;
1821
import com.back.api.event.dto.request.EventUpdateRequest;
1922
import com.back.api.event.dto.response.AdminEventDashboardResponse;
2023
import com.back.api.event.dto.response.EventResponse;
2124
import com.back.domain.event.entity.Event;
22-
import com.back.domain.event.entity.EventCategory;
23-
import com.back.domain.event.entity.EventStatus;
2425
import com.back.domain.event.repository.EventRepository;
2526
import com.back.domain.preregister.entity.PreRegisterStatus;
2627
import com.back.domain.preregister.repository.PreRegisterRepository;
2728
import com.back.domain.seat.entity.SeatStatus;
2829
import com.back.domain.seat.repository.SeatRepository;
29-
import com.back.global.error.exception.ErrorException;
3030
import com.back.support.factory.EventFactory;
3131

3232
@ExtendWith(MockitoExtension.class)
@@ -123,52 +123,69 @@ void getAllEventsDashboard_Success() {
123123
Event event2 = EventFactory.fakeEvent("이벤트2");
124124
List<Event> events = List.of(event1, event2);
125125

126-
given(eventRepository.findAll()).willReturn(events);
126+
Pageable pageable = PageRequest.of(0, 20);
127+
Page<Event> eventPage = new PageImpl<>(events, pageable, events.size());
128+
129+
130+
given(eventRepository.findAll(any(Pageable.class))).willReturn(eventPage); // <- 변경!
127131
given(preRegisterRepository.countByEvent_IdAndPreRegisterStatus(
128132
any(), eq(PreRegisterStatus.REGISTERED))).willReturn(10L);
129133
given(seatRepository.countByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(5L);
130134
given(seatRepository.sumPriceByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(50000L);
131135

132136
// when
133-
List<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard();
137+
Page<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(0, 20);
134138

135139
// then
136-
assertThat(results).hasSize(2);
137-
assertThat(results.get(0).preRegisterCount()).isEqualTo(10L);
138-
assertThat(results.get(0).totalSoldSeats()).isEqualTo(5L);
139-
assertThat(results.get(0).totalSalesAmount()).isEqualTo(50000L);
140+
assertThat(results.getContent()).hasSize(2);
141+
assertThat(results.getTotalElements()).isEqualTo(2);
142+
assertThat(results.getTotalPages()).isEqualTo(1);
143+
assertThat(results.getSize()).isEqualTo(20);
144+
assertThat(results.getNumber()).isEqualTo(0);
145+
146+
assertThat(results.getContent().get(0).preRegisterCount()).isEqualTo(10L);
147+
assertThat(results.getContent().get(0).totalSoldSeats()).isEqualTo(5L);
148+
assertThat(results.getContent().get(0).totalSalesAmount()).isEqualTo(50000L);
140149
}
141150

142151
@Test
143152
@DisplayName("이벤트가 없으면 빈 리스트를 반환한다")
144153
void getAllEventsDashboard_EmptyList() {
145154
// given
146-
given(eventRepository.findAll()).willReturn(List.of());
155+
Pageable pageable = PageRequest.of(0, 20);
156+
Page<Event> emptyPage = new PageImpl<>(List.of(), pageable, 0);
157+
158+
given(eventRepository.findAll(any(Pageable.class))).willReturn(emptyPage);
147159

148160
// when
149-
List<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard();
161+
Page<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(0, 20);
150162

151163
// then
152-
assertThat(results).isEmpty();
164+
assertThat(results.getContent()).isEmpty();
165+
assertThat(results.getTotalElements()).isEqualTo(0);
166+
assertThat(results.getTotalPages()).isEqualTo(0);
153167
}
154168

155169
@Test
156170
@DisplayName("총 판매 금액이 null이면 0을 반환한다")
157171
void getAllEventsDashboard_NullSalesAmount() {
158172
// given
159173
Event event = EventFactory.fakeEvent("이벤트");
160-
given(eventRepository.findAll()).willReturn(List.of(event));
174+
Pageable pageable = PageRequest.of(0, 20);
175+
Page<Event> eventPage = new PageImpl<>(List.of(event), pageable, 1);
176+
177+
given(eventRepository.findAll(any(Pageable.class))).willReturn(eventPage);
161178
given(preRegisterRepository.countByEvent_IdAndPreRegisterStatus(
162179
any(), eq(PreRegisterStatus.REGISTERED))).willReturn(0L);
163180
given(seatRepository.countByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(0L);
164181
given(seatRepository.sumPriceByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(null);
165182

166183
// when
167-
List<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard();
184+
Page<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(0, 20);
168185

169186
// then
170-
assertThat(results).hasSize(1);
171-
assertThat(results.get(0).totalSalesAmount()).isEqualTo(0L);
187+
assertThat(results.getContent()).hasSize(1);
188+
assertThat(results.getContent().get(0).totalSalesAmount()).isEqualTo(0L);
172189
}
173190
}
174191
}

0 commit comments

Comments
 (0)