Skip to content

Commit

Permalink
Merge pull request #82 from team-Ollie/develop
Browse files Browse the repository at this point in the history
[develop] 챌린지 인증코드 발급 API 구현
  • Loading branch information
Haeun-Y authored Jun 29, 2024
2 parents 45ea4b4 + dbaff5c commit 269df5e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,21 @@ public BaseResponse<List<GetChallengesRes>> getMyChallenges() throws BaseExcepti
return new BaseResponse<>(challengeService.getMyChallenges());
}

/*
* 챌린지 인증코드 발급
* */
@PostMapping("/attendance/{challengeIdx}")
@ResponseBody
public BaseResponse<GetAttendanceCodeReq> getAttendanceCode(@PathVariable(value = "challengeIdx") Long challengeIdx) {
return new BaseResponse<>(challengeService.getAttendanceCode(challengeIdx));

}


/*
* 챌린지 인증
* */
//TODO : PathVariable로 변경
@PostMapping("/attendance")
@ResponseBody
public BaseResponse<String> attendChallenge(@RequestBody AttendChallengeReq attendChallengeReq) throws BaseException {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ollie/wecare/challenge/dto/GetAttendanceCodeReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ollie.wecare.challenge.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class GetAttendanceCodeReq {
private String code;
}
3 changes: 3 additions & 0 deletions src/main/java/ollie/wecare/challenge/entity/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
@AllArgsConstructor
@DynamicInsert
public class Challenge extends BaseEntity {
public void updateAttendanceCode(String attendanceCode) {
this.attendanceCode = attendanceCode;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/ollie/wecare/challenge/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import ollie.wecare.challenge.repository.ChallengeAttendanceRepository;
import ollie.wecare.challenge.repository.ChallengeRepository;
import ollie.wecare.common.base.BaseException;
import ollie.wecare.common.enums.Role;
import ollie.wecare.user.entity.User;
import ollie.wecare.user.repository.UserRepository;
import ollie.wecare.user.service.AuthService;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -54,6 +56,24 @@ public List<GetChallengesRes> getMyChallenges() throws BaseException {
return challengesList.stream().map(challenge -> GetChallengesRes.fromChallenge(challenge, participationNum)).toList();
}

/*
* 챌린지 인증코드 발급
* */
public GetAttendanceCodeReq getAttendanceCode(Long challengeIdx) {
User user = userRepository.findById(authService.getUserIdx()).orElseThrow(()-> new BaseException(INVALID_USER_IDX));
if(!user.getRole().equals(Role.Admin)) throw new BaseException(INVALID_ROLE);

Challenge challenge = challengeRepository.findById(challengeIdx).orElseThrow(() -> new BaseException(INVALID_CHALLENGE_IDX));

Random random = new Random();
String code = "";
for (int i=0; i<6; i++) code += Integer.toString(random.nextInt(9));

challenge.updateAttendanceCode(code);
return GetAttendanceCodeReq.builder().code(code).build();
}


/*
* 챌린지 인증
* */
Expand Down

0 comments on commit 269df5e

Please sign in to comment.