Skip to content

Commit

Permalink
Merge pull request #106 from KB4-3-sinsaimdang/FEAT]-#98
Browse files Browse the repository at this point in the history
Feat] #98 - Add ChallengeUser Count
  • Loading branch information
JonghanJeon authored Sep 18, 2023
2 parents 8e0400d + c48536a commit ad79a0f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/kb/wgwg/controller/ChallengeUserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ public ResponseEntity<BaseResponseDTO> checkChallengeUser(@RequestBody CheckChal
}

}

@PostMapping("/cnt")
public ResponseEntity<BaseResponseDTO> countUser(@RequestBody ReadChallengeUserRequestDTO dto){
BaseResponseDTO<challengeUserCntResponse> response = new BaseResponseDTO<>();
try {
challengeUserCntResponse result = challengeUserService.countUser(dto);
response.setMessage("참여자수 카운트 성공.");
response.setStatus(StatusCode.OK);
response.setSuccess(true);
response.setData(result);
return ResponseEntity.ok(response);
}catch (Exception e){
e.printStackTrace();
response.setMessage(e.getMessage());
response.setStatus(StatusCode.INTERNAL_SERVER_ERROR);
response.setSuccess(false);
return ResponseEntity.internalServerError().body(response);
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/kb/wgwg/dto/ChallengeUserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.*;

import java.time.LocalDateTime;

public class ChallengeUserDTO {

@Getter
Expand Down Expand Up @@ -35,5 +37,18 @@ public static final class CheckChallengeUserRequestDTO{
@Builder
public static final class CheckChallengeUserResponseDTO{
private String participantType;
private String challengeType;
}


@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public static final class challengeUserCntResponse{
private int allParticipantCnt;
private int survivorCnt;
private int failureCnt;
}
}
3 changes: 3 additions & 0 deletions src/main/java/kb/wgwg/repository/ChallengeRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

@Query(value = "SELECT c.CHALLENGE_ID as CHALLENGE_ID, TOTAL_DEPOSIT FROM NCHALLENGE cc JOIN CHALLENGE c ON cc.CHALLENGE_ID = c.CHALLENGE_ID WHERE c.STATUS = \'진행\'", nativeQuery = true)
List<Object[]> findOngoingNChallenge();

@Query(value = "select challenge_type from challenge where challenge_id = ?1", nativeQuery = true)
String findChallengeTypeByChallengeId(Long challengeId);
}
7 changes: 7 additions & 0 deletions src/main/java/kb/wgwg/repository/ChallengeUserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import kb.wgwg.domain.ChallengeUser;
import kb.wgwg.domain.User;
import kb.wgwg.dto.ChallengeUserDTO;
import kb.wgwg.dto.ChallengeDTO.*;
import kb.wgwg.dto.ChallengeUserDTO.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -53,4 +54,10 @@ public interface ChallengeUserRepository extends JpaRepository<ChallengeUser, Lo

@Query(value = "select user_id from challenge_user where challenge_id = ?1", nativeQuery = true)
List<Long> findParticipantIdByChallengeId(Long challengeId);

@Query(value = "select count(*) from challenge_user where challenge_id = ?1", nativeQuery = true)
Integer allParticipantCnt(Long challengeId);

@Query(value = "select count(is_success) from challenge_user where is_success in (1, 2) and challenge_id = ?1", nativeQuery = true)
Integer survivorCnt(Long challengeId);
}
18 changes: 18 additions & 0 deletions src/main/java/kb/wgwg/service/ChallengeUserService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package kb.wgwg.service;

import kb.wgwg.domain.Challenge.*;
import kb.wgwg.domain.ChallengeUser;
import kb.wgwg.domain.User;
import kb.wgwg.dto.ChallengeDTO;
import kb.wgwg.dto.ChallengeUserDTO;
import kb.wgwg.dto.ChallengeUserDTO.*;
import kb.wgwg.repository.ChallengeRepository;
import kb.wgwg.repository.ChallengeUserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -20,6 +23,7 @@
public class ChallengeUserService {

private final ChallengeUserRepository challengeUserRep;
private final ChallengeRepository challengeRep;

public Page<ReadChallengeUserResponseDTO> readChallengeUserByChallengeId(ReadChallengeUserRequestDTO dto, Pageable pageable){
Page<ReadChallengeUserResponseDTO> page = challengeUserRep.findAllByChallenge(dto.getChallengeId(), pageable);
Expand All @@ -29,9 +33,11 @@ public Page<ReadChallengeUserResponseDTO> readChallengeUserByChallengeId(ReadCha

public CheckChallengeUserResponseDTO checkChallengeUser(CheckChallengeUserRequestDTO dto){
Long ownerId = challengeUserRep.findOwnerIdByChallengeId(dto.getChallengeId());
String challengeType = challengeRep.findChallengeTypeByChallengeId(dto.getChallengeId());
if(ownerId == dto.getUserSeq()){
CheckChallengeUserResponseDTO result = CheckChallengeUserResponseDTO.builder()
.participantType("개설자")
.challengeType(challengeType)
.build();
return result;
}
Expand All @@ -40,6 +46,7 @@ public CheckChallengeUserResponseDTO checkChallengeUser(CheckChallengeUserReques
if(seq == dto.getUserSeq()){
CheckChallengeUserResponseDTO result = CheckChallengeUserResponseDTO.builder()
.participantType("참여자")
.challengeType(challengeType)
.build();
return result;
}
Expand All @@ -49,4 +56,15 @@ public CheckChallengeUserResponseDTO checkChallengeUser(CheckChallengeUserReques
.build();
return result;
}

public challengeUserCntResponse countUser(ReadChallengeUserRequestDTO dto){
int allParticipantCnt = challengeUserRep.allParticipantCnt(dto.getChallengeId());
int survivorCnt = challengeUserRep.survivorCnt(dto.getChallengeId());
challengeUserCntResponse response = challengeUserCntResponse.builder()
.allParticipantCnt(allParticipantCnt)
.survivorCnt(survivorCnt)
.failureCnt(allParticipantCnt - survivorCnt)
.build();
return response;
}
}

0 comments on commit ad79a0f

Please sign in to comment.