Skip to content

Commit

Permalink
Merge pull request #186 from FOREGG-DEV/dev
Browse files Browse the repository at this point in the history
♻️ refactor: Challenge 기능 추가
  • Loading branch information
DongJun1110 authored Dec 16, 2024
2 parents 6f45989 + 5fe1b59 commit 462f033
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import foregg.foreggserver.dto.challengeDTO.ChallengeRequestDTO.ChallengeCreateRequestDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeRequestDTO.ChallengeNameRequestDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeParticipantsDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.MyChallengeTotalDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.MyChallengeTotalDTO.MyChallengeDTO;
Expand Down Expand Up @@ -164,7 +165,7 @@ public ApiResponse<String> createChallenge(@RequestBody ChallengeCreateRequestDT
return ApiResponse.onSuccess();
}

@Operation(summary = "챌린지 제작하기")
@Operation(summary = "챌린지 검색하기")
@GetMapping("/search")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
Expand Down Expand Up @@ -211,4 +212,14 @@ public ApiResponse<String> cheer(@PathVariable(name = "challengeId") Long challe
return ApiResponse.onSuccess();
}

@Operation(summary = "챌린지 상세")
@GetMapping("/{challengeId}")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "CHALLENGE4002", description = "존재하지 않는 챌린지입니다"),
})
public ApiResponse<ChallengeDTO> detail(@PathVariable(name = "challengeId") Long challengeId){
ChallengeDTO result = challengeQueryService.detail(challengeId);
return ApiResponse.onSuccess(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,22 @@ public static ChallengeParticipantsDTO toChallengeParticipantDTO(ChallengePartic
.isSupported(isSupported).build();
}

public static ChallengeDTO toChallengeDTO(Challenge challenge, ChallengeParticipation challengeParticipation, User user) {
boolean isOpen = false;
boolean isParticipating = false;
if (challengeParticipation != null) {
isOpen = challengeParticipation.isOpen();
isParticipating = challengeParticipation.isParticipating();
}
return ChallengeDTO.builder()
.id(challenge.getId())
.point(user.getPoint())
.image(challenge.getImage())
.name(challenge.getName())
.description(challenge.getDescription())
.participants(challenge.getChallengeParticipations().size())
.isOpen(isOpen)
.isParticipating(isParticipating).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;
import java.util.Optional;

public interface ChallengeParticipationRespository extends JpaRepository<ChallengeParticipation, Long> {
public interface ChallengeParticipationRepository extends JpaRepository<ChallengeParticipation, Long> {

Optional<List<ChallengeParticipation>> findByChallenge(Challenge challenge);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface NotificationRepository extends JpaRepository<Notification, Long

Notification findBySenderAndReceiverAndDateAndNotificationType(String senderNickname, User receiver, String date, NotificationType notificationType);

List<Notification> findBySenderAndDateAndNotificationType(String senderNickname, String date, NotificationType notificationType);
List<Notification> findBySenderAndDateAndNotificationType(String senderNickname, String date, NotificationType notificationType);

List<Notification> findByReceiver(User receiver);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeParticipantsDTO;
import foregg.foreggserver.repository.NotificationRepository;
import foregg.foreggserver.repository.ChallengeParticipationRespository;
import foregg.foreggserver.repository.ChallengeParticipationRepository;
import foregg.foreggserver.repository.ChallengeRepository;
import foregg.foreggserver.service.userService.UserQueryService;
import foregg.foreggserver.util.DateUtil;
Expand All @@ -32,7 +32,7 @@
public class ChallengeQueryService {

private final ChallengeRepository challengeRepository;
private final ChallengeParticipationRespository challengeParticipationRespository;
private final ChallengeParticipationRepository challengeParticipationRepository;
private final UserQueryService userQueryService;
private final NotificationRepository notificationRepository;

Expand All @@ -46,7 +46,7 @@ public ChallengeResponseDTO challengeMain() {
//일단 관리자 id는 -1
List<Challenge> challenges = getMainChallenge();
for (Challenge challenge : challenges) {
Optional<ChallengeParticipation> cp = challengeParticipationRespository.findByUserAndChallenge(user, challenge);
Optional<ChallengeParticipation> cp = challengeParticipationRepository.findByUserAndChallenge(user, challenge);
ChallengeDTO challengeResponseDTO = ChallengeConverter.toChallengeResponseDTO(challenge, user, cp);
result.add(challengeResponseDTO);
}
Expand All @@ -58,7 +58,7 @@ public ChallengeResponseDTO getAllChallenges() {
List<ChallengeDTO> resultList = new ArrayList<>();
List<Challenge> mainChallenge = challengeRepository.findByProducerId(-1L);
for (Challenge challenge : mainChallenge) {
Optional<ChallengeParticipation> cp = challengeParticipationRespository.findByUserAndChallenge(user, challenge);
Optional<ChallengeParticipation> cp = challengeParticipationRepository.findByUserAndChallenge(user, challenge);
ChallengeDTO challengeResponseDTO = ChallengeConverter.toChallengeResponseDTO(challenge, user, cp);
resultList.add(challengeResponseDTO);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public ChallengeResponseDTO searchChallenge(String keyword) {
List<Challenge> challenges = challengeRepository.findByNameContaining(keyword);
List<ChallengeDTO> resultList = new ArrayList<>();
for (Challenge challenge : challenges) {
Optional<ChallengeParticipation> cp = challengeParticipationRespository.findByUserAndChallenge(user, challenge);
Optional<ChallengeParticipation> cp = challengeParticipationRepository.findByUserAndChallenge(user, challenge);
ChallengeDTO challengeResponseDTO = ChallengeConverter.toChallengeResponseDTO(challenge, user, cp);
resultList.add(challengeResponseDTO);
}
Expand All @@ -128,25 +128,32 @@ public ChallengeResponseDTO searchChallenge(String keyword) {
public Challenge isParticipating(Long challengeId) {
User user = userQueryService.getUser();
Challenge challenge = challengeRepository.findById(challengeId).orElseThrow(() -> new ChallengeHandler(CHALLENGE_NOT_FOUND));
ChallengeParticipation challengeParticipation = challengeParticipationRespository.findByUserAndChallenge(user, challenge).orElseThrow(() -> new ChallengeHandler(NO_PARTICIPATING_CHALLENGE));
ChallengeParticipation challengeParticipation = challengeParticipationRepository.findByUserAndChallenge(user, challenge).orElseThrow(() -> new ChallengeHandler(NO_PARTICIPATING_CHALLENGE));
if (!challengeParticipation.isParticipating()) {
throw new ChallengeHandler((NO_PARTICIPATING_CHALLENGE));
}
return challenge;
}

public ChallengeDTO detail(Long challengeId) {
User user = userQueryService.getUser();
Challenge challenge = challengeRepository.findById(challengeId).orElseThrow(() -> new ChallengeHandler(CHALLENGE_NOT_FOUND));
ChallengeParticipation cp = challengeParticipationRepository.findByUserAndChallenge(user, challenge).orElse(null);
return ChallengeConverter.toChallengeDTO(challenge, cp, user);
}

private List<Challenge> getMainChallenge() {
User user = userQueryService.getUser();
List<Challenge> mainChallenge = challengeRepository.findByProducerId(-1L);
List<Challenge> result = new ArrayList<>();
List<Challenge> tmp = new ArrayList<>();

// 해당 사용자의 ChallengeParticipation을 가져와서 Challenge와 매핑
List<ChallengeParticipation> userParticipations = challengeParticipationRespository.findByUser(user)
List<ChallengeParticipation> userParticipations = challengeParticipationRepository.findByUser(user)
.orElse(Collections.emptyList());

for (Challenge challenge : mainChallenge) {
Optional<ChallengeParticipation> foundChallenge = challengeParticipationRespository.findByUserAndChallenge(user, challenge);
Optional<ChallengeParticipation> foundChallenge = challengeParticipationRepository.findByUserAndChallenge(user, challenge);
if (foundChallenge.isEmpty()) {
tmp.add(challenge); // 참여하지 않은 챌린지
} else {
Expand All @@ -156,8 +163,8 @@ private List<Challenge> getMainChallenge() {

// 참여 중인 챌린지를 ChallengeParticipation의 생성 시간(createdDate) 기준으로 정렬
result.sort((c1, c2) -> {
Optional<ChallengeParticipation> cp1 = challengeParticipationRespository.findByUserAndChallenge(user, c1);
Optional<ChallengeParticipation> cp2 = challengeParticipationRespository.findByUserAndChallenge(user, c2);
Optional<ChallengeParticipation> cp1 = challengeParticipationRepository.findByUserAndChallenge(user, c1);
Optional<ChallengeParticipation> cp2 = challengeParticipationRepository.findByUserAndChallenge(user, c2);

return cp1.get().getCreatedAt().compareTo(cp2.get().getCreatedAt());
});
Expand All @@ -178,7 +185,7 @@ private List<ChallengeDTO> getCustomChallenge() {
List<ChallengeDTO> result = new ArrayList<>();

for (Challenge challenge : challenges) {
Optional<ChallengeParticipation> cParticipation = challengeParticipationRespository.findByUserAndChallenge(user, challenge);
Optional<ChallengeParticipation> cParticipation = challengeParticipationRepository.findByUserAndChallenge(user, challenge);
if (cParticipation.isEmpty()) {
notParticipatingChallenge.add(challenge);
continue;
Expand All @@ -190,7 +197,7 @@ private List<ChallengeDTO> getCustomChallenge() {
resultChallenges.addAll(notParticipatingChallenge);

for (Challenge challenge : resultChallenges) {
Optional<ChallengeParticipation> cp = challengeParticipationRespository.findByUserAndChallenge(user, challenge);
Optional<ChallengeParticipation> cp = challengeParticipationRepository.findByUserAndChallenge(user, challenge);
ChallengeDTO challengeResponseDTO = ChallengeConverter.toChallengeResponseDTO(challenge, user, cp);
result.add(challengeResponseDTO);
}
Expand All @@ -199,15 +206,15 @@ private List<ChallengeDTO> getCustomChallenge() {

private List<ChallengeParticipation> getMyCParticipation() {
User user = userQueryService.getUser();
List<ChallengeParticipation> cParticipation = challengeParticipationRespository.findByUser(user).orElse(null);
List<ChallengeParticipation> cParticipation = challengeParticipationRepository.findByUser(user).orElse(null);
cParticipation.removeIf(cp -> !cp.isParticipating());
List<Challenge> result = new ArrayList<>();
return cParticipation;
}

public int getChallengeParticipants(ChallengeParticipation cp) {
Challenge challenge = cp.getChallenge();
Optional<List<ChallengeParticipation>> challengeParticipation = challengeParticipationRespository.findByChallenge(challenge);
Optional<List<ChallengeParticipation>> challengeParticipation = challengeParticipationRepository.findByChallenge(challenge);
if (challengeParticipation.isPresent()) {
return challengeParticipation.get().size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import foregg.foreggserver.domain.User;
import foregg.foreggserver.domain.enums.NotificationType;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.MyChallengeTotalDTO.MyChallengeDTO;
import foregg.foreggserver.repository.ChallengeParticipationRespository;
import foregg.foreggserver.repository.ChallengeParticipationRepository;
import foregg.foreggserver.repository.ChallengeRepository;
import foregg.foreggserver.repository.NotificationRepository;
import foregg.foreggserver.repository.UserRepository;
Expand All @@ -36,7 +36,7 @@
public class ChallengeService {

private final ChallengeRepository challengeRepository;
private final ChallengeParticipationRespository challengeParticipationRepository;
private final ChallengeParticipationRepository challengeParticipationRepository;
private final UserQueryService userQueryService;
private final UserRepository userRepository;
private final ChallengeQueryService challengeQueryService;
Expand Down

0 comments on commit 462f033

Please sign in to comment.