Skip to content

Commit

Permalink
Merge pull request #143 from softeerbootcamp4th/feature/#116-admin-lo…
Browse files Browse the repository at this point in the history
…ttery-event-delete-expectation

Feature/#116 admin lottery event delete expectation
  • Loading branch information
k000927 authored Aug 15, 2024
2 parents e592316 + 978eba3 commit 554042a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import JGS.CasperEvent.domain.event.dto.RequestDto.lotteryEventDto.LotteryEventRequestDto;
import JGS.CasperEvent.domain.event.dto.RequestDto.rushEventDto.RushEventRequestDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.ImageUrlResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventDetailResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventExpectationResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventParticipantsListResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.*;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.AdminRushEventOptionResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.AdminRushEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.LotteryEventWinnerListResponseDto;
Expand Down Expand Up @@ -162,8 +159,10 @@ public ResponseEntity<LotteryEventDetailResponseDto> updateLotteryEvent(@Request

// 추첨 이벤트 특정 사용자의 기대평 조회
@GetMapping("/event/lottery/participants/{participantId}/expectations")
public ResponseEntity<List<LotteryEventExpectationResponseDto>> getLotteryEventExpectations(@PathVariable("participantId") Long participantId) {
List<LotteryEventExpectationResponseDto> lotteryEventExpectationResponseDtoList = adminService.getLotteryEventExpectations(participantId);
public ResponseEntity<LotteryEventExpectationsResponseDto> getLotteryEventExpectations(@PathVariable("participantId") Long participantId,
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
@RequestParam(name = "size", required = false, defaultValue = "10") int size) {
LotteryEventExpectationsResponseDto lotteryEventExpectationResponseDtoList = adminService.getLotteryEventExpectations(page, size, participantId);

return ResponseEntity.ok(lotteryEventExpectationResponseDtoList);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto;

import java.util.List;

public record LotteryEventExpectationsResponseDto(List<LotteryEventExpectationResponseDto> expectations,
Boolean isLastPage, long totalExpectations) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class CasperBot extends BaseEntity {
private int sticker;
private String name;
private String expectation;
private boolean isDeleted;

public CasperBot() {

Expand All @@ -41,15 +42,7 @@ public CasperBot(CasperBotRequestDto requestDto, String phoneNumber) {
this.expectation = requestDto.getExpectation();
}

public CasperBot deleteExpectation() {
if (!this.expectation.isEmpty()) {
this.expectation = "삭제된 기대평입니다.";
}

return this;
}

public boolean isDeleted() {
return this.expectation.equals("삭제된 기대평입니다.");
public void deleteExpectation() {
this.isDeleted = true;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package JGS.CasperEvent.domain.event.repository;

import JGS.CasperEvent.domain.event.entity.casperBot.CasperBot;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CasperBotRepository extends JpaRepository<CasperBot, Long> {
List<CasperBot> findByPhoneNumber(String phoneNumber);
@Query("SELECT c FROM CasperBot c WHERE c.phoneNumber = :phoneNumber AND c.isDeleted = false AND c.expectation <> ''")
Page<CasperBot> findByPhoneNumberAndActiveExpectations(@Param("phoneNumber") String phoneNumber, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static JGS.CasperEvent.global.util.RepositoryErrorHandler.findByIdOrElseThrow;

Expand All @@ -58,6 +60,7 @@ public class AdminService {
private final S3Service s3Service;
private final CasperBotRepository casperBotRepository;
private final LotteryWinnerRepository lotteryWinnerRepository;
private final RedisTemplate<String, CasperBotResponseDto> casperBotRedisTemplate;

public Admin verifyAdmin(AdminRequestDto adminRequestDto) {
return adminRepository.findByIdAndPassword(adminRequestDto.getAdminId(), adminRequestDto.getPassword()).orElseThrow(NoSuchElementException::new);
Expand Down Expand Up @@ -435,15 +438,30 @@ public AdminRushEventOptionResponseDto getRushEventOptions(Long rushEventId) {
);
}

public List<LotteryEventExpectationResponseDto> getLotteryEventExpectations(Long participantId) {
public LotteryEventExpectationsResponseDto getLotteryEventExpectations(int page, int size, Long participantId) {
LotteryParticipants lotteryParticipant = lotteryParticipantsRepository.findById(participantId).orElseThrow(
() -> new CustomException(CustomErrorCode.USER_NOT_FOUND)
);

List<CasperBot> casperBotList = casperBotRepository.findByPhoneNumber(lotteryParticipant.getBaseUser().getId());

// 기대평을 작성하지 않은 경우(기대평이 빈 문자열인 경우, 삭제된 경우)는 제외하여 반환합니다.
return casperBotList.stream().filter(casperBot -> !casperBot.getExpectation().isEmpty() && !casperBot.isDeleted()).map(LotteryEventExpectationResponseDto::of).toList();
Pageable pageable = PageRequest.of(page, size);
Page<CasperBot> casperBotPage = casperBotRepository.findByPhoneNumberAndActiveExpectations(lotteryParticipant.getBaseUser().getId(), pageable);

// DTO로 변환합니다.
List<LotteryEventExpectationResponseDto> lotteryEventExpectationResponseDtoList = casperBotPage.getContent().stream()
.map(casperBot -> new LotteryEventExpectationResponseDto(
casperBot.getCasperId(),
casperBot.getExpectation(),
casperBot.getCreatedAt().toLocalDate(),
casperBot.getCreatedAt().toLocalTime()
))
.collect(Collectors.toList());

// 마지막 페이지 여부 계산
boolean isLastPage = casperBotPage.isLast();

// 결과를 반환합니다.
return new LotteryEventExpectationsResponseDto(lotteryEventExpectationResponseDtoList, isLastPage, casperBotPage.getTotalElements());
}

@Transactional
Expand All @@ -452,7 +470,47 @@ public void deleteLotteryEventExpectation(Long casperId) {
() -> new CustomException(CustomErrorCode.CASPERBOT_NOT_FOUND)
);

// todo: 전체 설정에서 가져오도록 변경
final String LIST_KEY = "recentData";

// 긍정적인 문구 리스트
List<String> positiveMessages = List.of("사랑해 캐스퍼", "캐스퍼 최고!", "캐스퍼와 함께해요!", "캐스퍼 짱!", "캐스퍼는 나의 친구!");

// 랜덤으로 긍정적인 문구 선택
String randomPositiveMessage = positiveMessages.get(new Random().nextInt(positiveMessages.size()));

// isDeleted = true 로 업데이트
casperBot.deleteExpectation();

// Redis에서 모든 데이터를 가져옵니다.
List<CasperBotResponseDto> allData = casperBotRedisTemplate.opsForList().range(LIST_KEY, 0, -1);

if (allData != null) {
// 해당하는 CasperBotId의 데이터를 업데이트합니다.
List<CasperBotResponseDto> updatedData = allData.stream()
.map(data -> {
if (casperId.equals(data.casperId())) {
return new CasperBotResponseDto(
data.casperId(),
data.eyeShape(),
data.eyePosition(),
data.mouthShape(),
data.color(),
data.sticker(),
data.name(),
randomPositiveMessage // 랜덤으로 선택된 긍정적인 문구로 기대평 필드 변경
);
}
return data;
})
.collect(Collectors.toList());

// Redis에서 현재 리스트를 삭제합니다.
casperBotRedisTemplate.delete(LIST_KEY);

// 업데이트된 리스트를 Redis에 다시 추가합니다.
casperBotRedisTemplate.opsForList().leftPushAll(LIST_KEY, updatedData);
}
}

public static <T> Page<T> paginateList(List<T> list, int page, int size) {
Expand Down

0 comments on commit 554042a

Please sign in to comment.