Skip to content

Commit

Permalink
Merge pull request #46 from softeerbootcamp4th/feat/userList_paging
Browse files Browse the repository at this point in the history
[Feat] admin 페이지 응모자 목록 페이징 및 이벤트 수정 response 내용 추가
  • Loading branch information
eckrin authored Aug 14, 2024
2 parents 6a1adf9 + 14a568a commit b3da4fc
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ public CommonResponse<EventDto> lotsEventConfig(@RequestBody @Valid EventConfigR

@PutMapping("/arrival/rewardconfig")
@Operation(summary = "선착순 이벤트 상품 수정 Api")
public CommonResponse<ArrivalUserListDto> arrivalEventRewardConfig(@RequestBody @Valid EventRewardConfigRequestDto dto){
public CommonResponse<EventRewardConfigResponseDto> arrivalEventRewardConfig(@RequestBody @Valid EventRewardConfigRequestDto dto){
return new CommonResponse<>(adminService.configArrivalEventReward(dto));
}

@PutMapping("/lots/rewardconfig")
@Operation(summary = "랜덤추첨 이벤트 상품 수정 Api")
public CommonResponse<LotsUserListDto> lotsEventRewardConfig(@RequestBody @Validated(LotsValidationSequence.class) EventRewardConfigRequestDto dto){
public CommonResponse<EventRewardConfigResponseDto> lotsEventRewardConfig(@RequestBody @Validated(LotsValidationSequence.class) EventRewardConfigRequestDto dto){
return new CommonResponse<>(adminService.configLotsEventReward(dto));
}

@GetMapping("/arrival/applicationList")
@Operation(summary = "선착순 응모 인원 반환 Api")
public CommonResponse<ArrivalUserListDto> arrivalApplicationList(){
return new CommonResponse<>(adminService.getArrivalApplicationList());
public CommonResponse<ArrivalUserListDto> arrivalApplicationList(@RequestParam(required = false, defaultValue = "0", value = "page") int pageNo){
return new CommonResponse<>(adminService.getArrivalApplicationList(pageNo));
}

@GetMapping("/lots/applicationList")
@Operation(summary = "랜덤추첨 응모 인원 반환 Api")
public CommonResponse<LotsUserListDto> lotsApplicationList(){
return new CommonResponse<>(adminService.getLotsApplicationList());
public CommonResponse<LotsUserListDto> lotsApplicationList(@RequestParam(required = false, defaultValue = "0", value = "page") int pageNo){
return new CommonResponse<>(adminService.getLotsApplicationList(pageNo));
}

@GetMapping("/lots/pickrandom")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.softeer.podo.admin.model.dto;

import com.softeer.podo.admin.model.dto.user.UserListDto;
import jakarta.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;

@Data
@AllArgsConstructor
public class EventRewardConfigResponseDto {
private List<EventRewardDto> eventRewards;
@Nullable
private EventWeightDto eventWeight;
private UserListDto userListPage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.softeer.podo.admin.model.dto.EventWeightDto;
import com.softeer.podo.admin.model.entity.Event;
import com.softeer.podo.admin.model.entity.EventReward;
import com.softeer.podo.admin.model.entity.EventWeight;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
Expand Down Expand Up @@ -49,5 +50,11 @@ public static EventListResponseDto eventListToEventListResponseDto(List<Event> e
return new EventListResponseDto(eventDtoList);
}

public static EventRewardDto eventRewardToEventRewardDto(EventReward eventReward){
return new EventRewardDto(eventReward.getRewardRank(), eventReward.getNumWinners(), eventReward.getReward());
}

public static EventWeightDto eventWeightToEventWeightDto(EventWeight eventWeight){
return new EventWeightDto(eventWeight.getTimes(), eventWeight.getWeightCondition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import com.softeer.podo.admin.model.dto.user.LotsUserListDto;
import com.softeer.podo.admin.model.entity.ArrivalUser;
import com.softeer.podo.admin.model.entity.LotsUser;
import org.springframework.stereotype.Component;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;

public class UserMapper {

public static ArrivalUserListDto ArrivalUserListToArrivalUserListDto(List<ArrivalUser> userList) {
public static ArrivalUserListDto ArrivalUserPageToArrivalUserListDto(Page<ArrivalUser> userPage) {
List<ArrivalUserDto> arrivalUserDtoList = new ArrayList<>();
for(ArrivalUser user : userList) {
for(ArrivalUser user : userPage.getContent()) {
//reward를 제외한 내용 추가
arrivalUserDtoList.add(
ArrivalUserDto.builder()
Expand All @@ -28,12 +28,12 @@ public static ArrivalUserListDto ArrivalUserListToArrivalUserListDto(List<Arriva
.build()
);
}
return new ArrivalUserListDto(arrivalUserDtoList);
return new ArrivalUserListDto(userPage.getTotalPages(), userPage.getNumber(), arrivalUserDtoList);
}

public static LotsUserListDto LotsUserListToLotsUserListDto(List<LotsUser> userList) {
public static LotsUserListDto LotsUserPageToLotsUserListDto(Page<LotsUser> userPage) {
List<LotsUserDto> lotsUserDtoList = new ArrayList<>();
for(LotsUser user : userList) {
for(LotsUser user : userPage.getContent()) {
//reward를 제외한 내용 추가
lotsUserDtoList.add(
LotsUserDto.builder()
Expand All @@ -45,6 +45,6 @@ public static LotsUserListDto LotsUserListToLotsUserListDto(List<LotsUser> userL
.build()
);
}
return new LotsUserListDto(lotsUserDtoList);
return new LotsUserListDto(userPage.getTotalPages(), userPage.getNumber(), lotsUserDtoList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

@Data
@AllArgsConstructor
public class ArrivalUserListDto {
public class ArrivalUserListDto implements UserListDto {
int totalPage;
int currentPage;
List<ArrivalUserDto> arrivalUserList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

@Data
@AllArgsConstructor
public class LotsUserListDto {
public class LotsUserListDto implements UserListDto {
int totalPage;
int currentPage;
List<LotsUserDto> lotsUserList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.softeer.podo.admin.model.dto.user;

public interface UserListDto {
}
57 changes: 41 additions & 16 deletions src/main/java/com/softeer/podo/admin/service/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.softeer.podo.admin.model.dto.mapper.EventMapper;
import com.softeer.podo.admin.model.dto.mapper.UserMapper;
import com.softeer.podo.admin.model.dto.user.LotsUserListDto;
import com.softeer.podo.admin.model.entity.ArrivalUser;
import com.softeer.podo.admin.model.entity.Event;
import com.softeer.podo.admin.model.entity.EventReward;
import com.softeer.podo.admin.model.exception.EventNotFoundException;
Expand All @@ -16,6 +17,10 @@
import com.softeer.podo.admin.repository.EventRewardRepository;
import com.softeer.podo.admin.repository.LotsUserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -31,6 +36,7 @@ public class AdminService {

private final Long arrivalEventId = 1L;
private final Long lotsEventId = 2L;
private final int PAGE_SIZE = 10;

@Transactional
public EventListResponseDto getEventList() {
Expand All @@ -50,32 +56,40 @@ public EventDto configLotsEvent(EventConfigRequestDto dto) {
}

@Transactional
public ArrivalUserListDto configArrivalEventReward(EventRewardConfigRequestDto dto) {
public EventRewardConfigResponseDto configArrivalEventReward(EventRewardConfigRequestDto dto) {
Event arrivalEvent = eventRepository.findById(arrivalEventId).orElseThrow(EventNotFoundException::new);
List<EventReward> arrivalRewards = eventRewardRepository.findByEvent(arrivalEvent);
eventRewardRepository.deleteAllInBatch(arrivalRewards);

List<EventReward> lotsRewardList = new ArrayList<>();
for(EventRewardDto rewardDto : dto.getEventRewardList()) {
eventRewardRepository.save(
EventReward.builder()
.event(arrivalEvent)
lotsRewardList.add(
EventReward.builder().event(arrivalEvent)
.reward(rewardDto.getReward())
.rewardRank(rewardDto.getRank())
.numWinners(rewardDto.getNumWinners())
.build()
);
.build());

}
eventRewardRepository.flush(); // 즉시 데이터베이스에 반영
eventRewardRepository.saveAllAndFlush(lotsRewardList);

List<EventRewardDto> eventRewardDtoList =
lotsRewardList.stream()
.map(EventMapper::eventRewardToEventRewardDto)
.toList();

return getArrivalApplicationList();
return new EventRewardConfigResponseDto(eventRewardDtoList, null, getArrivalApplicationList(0));
}

@Transactional
public LotsUserListDto configLotsEventReward(EventRewardConfigRequestDto dto) {
public EventRewardConfigResponseDto configLotsEventReward(EventRewardConfigRequestDto dto) {
Event lotsEvent = eventRepository.findById(lotsEventId).orElseThrow(EventNotFoundException::new);
List<EventReward> lotsRewards = eventRewardRepository.findByEvent(lotsEvent);
eventRewardRepository.deleteAllInBatch(lotsRewards);

List<EventReward> lotsRewardList = new ArrayList<>();
for(EventRewardDto rewardDto : dto.getEventRewardList()) {
eventRewardRepository.save(
lotsRewardList.add(
EventReward.builder()
.event(lotsEvent)
.reward(rewardDto.getReward())
Expand All @@ -84,16 +98,25 @@ public LotsUserListDto configLotsEventReward(EventRewardConfigRequestDto dto) {
.build()
);
}
eventRewardRepository.saveAll(lotsRewardList);
lotsEvent.getEventWeight().updateWeightCondition(dto.getEventWeight().getCondition());
lotsEvent.getEventWeight().updateTimes(dto.getEventWeight().getTimes());
eventRewardRepository.flush(); // 즉시 데이터베이스에 반영

return getLotsApplicationList();
List<EventRewardDto> eventRewardDtoList =
lotsRewardList.stream()
.map(EventMapper::eventRewardToEventRewardDto)
.toList();
EventWeightDto eventWeightDto = EventMapper.eventWeightToEventWeightDto(lotsEvent.getEventWeight());

return new EventRewardConfigResponseDto(eventRewardDtoList, eventWeightDto, getLotsApplicationList(0));
}

@Transactional
public ArrivalUserListDto getArrivalApplicationList() {
ArrivalUserListDto arrivalUserListDto = UserMapper.ArrivalUserListToArrivalUserListDto(arrivalUserRepository.findAll());
public ArrivalUserListDto getArrivalApplicationList(int pageNo) {
Pageable pageable = PageRequest.of(pageNo, PAGE_SIZE, Sort.by(Sort.Direction.DESC, "id"));
Page<ArrivalUser> page = arrivalUserRepository.findAll(pageable);
ArrivalUserListDto arrivalUserListDto = UserMapper.ArrivalUserPageToArrivalUserListDto(page);
//선착순 이벤트 id
Event arrivalEvent = eventRepository.findById(arrivalEventId).orElseThrow(EventNotFoundException::new);
List<EventReward> eventRewardList = arrivalEvent.getEventRewardList();
Expand All @@ -115,8 +138,10 @@ public ArrivalUserListDto getArrivalApplicationList() {
}

@Transactional
public LotsUserListDto getLotsApplicationList() {
return UserMapper.LotsUserListToLotsUserListDto(lotsUserRepository.findAll());
public LotsUserListDto getLotsApplicationList(int pageNo) {
Pageable pageable = PageRequest.of(pageNo, PAGE_SIZE, Sort.by(Sort.Direction.DESC, "id"));
Page<LotsUser> page = lotsUserRepository.findAll(pageable);
return UserMapper.LotsUserPageToLotsUserListDto(page);
}

@Transactional
Expand Down Expand Up @@ -174,7 +199,7 @@ public LotsUserListDto getLotsResult() {
lotsUserRepository.save(lotsUserList.get(i));
}

return getLotsApplicationList();
return getLotsApplicationList(0);
}

private Event updateEventByConfigDto(Long eventId, EventConfigRequestDto dto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void arrivalEventRewardConfig() throws Exception {
assertEquals(true, response.get("isSuccess"));
assertEquals(200, response.get("code"));

JSONArray applicationArray = response.getJSONObject("result").getJSONArray("arrivalUserList");
JSONArray applicationArray = response.getJSONObject("result").getJSONObject("userListPage").getJSONArray("arrivalUserList");
for(int i = 0; i < applicationArray.length(); i++){ //보상 확인
JSONObject user = applicationArray.getJSONObject(i);
int rank = user.getInt("rank");
Expand Down
45 changes: 29 additions & 16 deletions src/test/java/com/softeer/podo/admin/service/AdminServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.*;


import java.time.LocalDateTime;
Expand Down Expand Up @@ -225,18 +225,21 @@ void configLotsEvent() {
@DisplayName("선착순 이벤트 보상 수정 service")
void configArrivalEventReward() {
//given
Pageable pageable = PageRequest.of(0, 100, Sort.by(Sort.Direction.DESC, "id"));
Page<ArrivalUser> arrivalUserPage = new PageImpl<>(arrivalUserList, pageable, arrivalUserList.size());
when(arrivalUserRepository.findAll((Pageable) any())).thenReturn(arrivalUserPage);
when(eventRepository.findById(1L)).thenReturn(Optional.of(arrivalEvent));
when(arrivalUserRepository.findAll()).thenReturn(arrivalUserList);
when(eventRewardRepository.findByEvent(arrivalEvent)).thenReturn(arrivalEvent.getEventRewardList());
doAnswer(invocation -> {
arrivalEvent.getEventRewardList().clear();
return null;
}).when(eventRewardRepository).deleteAllInBatch(Mockito.anyList());
when(eventRewardRepository.save(any())).thenAnswer(new Answer<EventReward>() {
when(eventRewardRepository.saveAllAndFlush(any())).thenAnswer(new Answer<List<EventReward>>() {
@Override
public EventReward answer(InvocationOnMock invocationOnMock) throws Throwable {
EventReward eventReward = (EventReward) invocationOnMock.getArguments()[0];
arrivalEvent.getEventRewardList().add(eventReward);
public List<EventReward> answer(InvocationOnMock invocationOnMock) throws Throwable {
List<EventReward> eventReward = (List<EventReward>) invocationOnMock.getArguments()[0];
arrivalEvent.updateEventRewardList(eventReward);
return null;
}
});
Expand All @@ -251,13 +254,14 @@ public EventReward answer(InvocationOnMock invocationOnMock) throws Throwable {
requestDto.setEventRewardList(eventRewardList);

//when
ArrivalUserListDto responseDto = adminService.configArrivalEventReward(requestDto);
EventRewardConfigResponseDto responseDto = adminService.configArrivalEventReward(requestDto);

//then
assertEquals(rewardNum, arrivalEvent.getEventRewardList().size());
assertEquals(eventRewardList, responseDto.getEventRewards());

for(int i = 0; i < responseDto.getArrivalUserList().size(); i++){ //보상 확인
ArrivalUserDto user = responseDto.getArrivalUserList().get(i);
ArrivalUserListDto arrivalUserListPageDto = (ArrivalUserListDto) responseDto.getUserListPage();
for(int i = 0; i < arrivalUserListPageDto.getArrivalUserList().size(); i++){ //보상 확인
ArrivalUserDto user = arrivalUserListPageDto.getArrivalUserList().get(i);
int rank = user.getRank();

int winSum = 0;
Expand All @@ -276,18 +280,21 @@ public EventReward answer(InvocationOnMock invocationOnMock) throws Throwable {
@DisplayName("랜덤추첨 이벤트 보상 수정 service")
void configLotsEventReward() {
//given
Pageable pageable = PageRequest.of(0, 100, Sort.by(Sort.Direction.DESC, "id"));
Page<LotsUser> lotsUserPage = new PageImpl<>(lotsUserList, pageable, lotsUserList.size());
when(lotsUserRepository.findAll((Pageable) any())).thenReturn(lotsUserPage);
when(eventRepository.findById(2L)).thenReturn(Optional.of(lotsEvent));
when(lotsUserRepository.findAll()).thenReturn(lotsUserList);
when(eventRewardRepository.findByEvent(lotsEvent)).thenReturn(lotsEvent.getEventRewardList());
doAnswer(invocation -> {
lotsEvent.getEventRewardList().clear();
return null;
}).when(eventRewardRepository).deleteAllInBatch(Mockito.anyList());
when(eventRewardRepository.save(any())).thenAnswer(new Answer<EventReward>() {
when(eventRewardRepository.saveAll(any())).thenAnswer(new Answer<List<EventReward>>() {
@Override
public EventReward answer(InvocationOnMock invocationOnMock) throws Throwable {
EventReward eventReward = (EventReward) invocationOnMock.getArguments()[0];
lotsEvent.getEventRewardList().add(eventReward);
public List<EventReward> answer(InvocationOnMock invocationOnMock) throws Throwable {
List<EventReward> eventRewards = (List<EventReward>) invocationOnMock.getArguments()[0];
lotsEvent.updateEventRewardList(eventRewards);
return null;
}
});
Expand All @@ -299,14 +306,17 @@ public EventReward answer(InvocationOnMock invocationOnMock) throws Throwable {
}
EventRewardConfigRequestDto requestDto = new EventRewardConfigRequestDto();
requestDto.setEventRewardList(eventRewardList);
requestDto.setEventWeight(new EventWeightDto(3, "comment"));

EventWeightDto eventWeight = new EventWeightDto(3, "comment");
requestDto.setEventWeight(eventWeight);


//when
LotsUserListDto responseDto = adminService.configLotsEventReward(requestDto);
EventRewardConfigResponseDto responseDto = adminService.configLotsEventReward(requestDto);

//then
assertEquals(rewardNum, lotsEvent.getEventRewardList().size());
assertEquals(eventRewardList, responseDto.getEventRewards());
assertEquals(eventWeight, responseDto.getEventWeight());
}

@Test
Expand All @@ -320,8 +330,11 @@ void getLotsApplicationList() {
@Test
void getLotsResult() {
//given
Pageable pageable = PageRequest.of(0, 100, Sort.by(Sort.Direction.DESC, "id"));
Page<LotsUser> lotsUserPage = new PageImpl<>(lotsUserList, pageable, lotsUserList.size());
when(eventRepository.findById(2L)).thenReturn(Optional.of(lotsEvent));
when(lotsUserRepository.findAll()).thenReturn(lotsUserList);
when(lotsUserRepository.findAll((Pageable) any())).thenReturn(lotsUserPage);

//when
LotsUserListDto responseDto = adminService.getLotsResult();
Expand Down

0 comments on commit b3da4fc

Please sign in to comment.