Skip to content

Commit

Permalink
✅ 챌린지 토큰 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdals4716 committed Aug 5, 2024
1 parent 6d379fe commit 107d01a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.example.MangoWafflee.Service.ChallengeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand Down Expand Up @@ -45,8 +47,8 @@ public ResponseEntity<ChallengeDTO> updateChallengeStatus(@PathVariable Long cha

//유저 챌린지 참여
@PostMapping("/userchallenge/participate")
public ResponseEntity<UserChallengeDTO> participateInChallenge(@RequestBody UserChallengeDTO userChallengeDTO) {
UserChallengeDTO userChallenge = challengeService.participateInChallenge(userChallengeDTO.getUser().getId(), userChallengeDTO.getChallenge().getId(), userChallengeDTO.getParticipating());
public ResponseEntity<UserChallengeDTO> participateInChallenge(@RequestBody UserChallengeDTO userChallengeDTO, @AuthenticationPrincipal UserDetails userDetails) {
UserChallengeDTO userChallenge = challengeService.participateInChallenge(userChallengeDTO.getUser().getId(), userChallengeDTO.getChallenge().getId(), userChallengeDTO.getParticipating(), userDetails);
return ResponseEntity.ok(userChallenge);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.MangoWafflee.DTO.UserChallengeDTO;
import com.example.MangoWafflee.Entity.UserEntity;
import com.example.MangoWafflee.Enum.StatusEnum;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -14,7 +15,7 @@ public interface ChallengeService {
List<ChallengeDTO> getAllChallenges();
ChallengeDTO getChallengeById(Long challengeId);
ChallengeDTO updateChallengeStatus(Long challengeId, StatusEnum status);
UserChallengeDTO participateInChallenge(Long userId, Long challengeId, StatusEnum status);
UserChallengeDTO participateInChallenge(Long userId, Long challengeId, StatusEnum status, UserDetails userDetails);
UserChallengeDTO updateUserChallengeStatus(Long userChallengeId, StatusEnum status);
void checkAndUpdateChallengeStatus(Long userId);
List<UserChallengeDTO> getUserChallenges(Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
Expand Down Expand Up @@ -130,7 +131,15 @@ public ChallengeDTO updateChallengeStatus(Long challengeId, StatusEnum status) {

//유저 챌린지 참여
@Override
public UserChallengeDTO participateInChallenge(Long userId, Long challengeId, StatusEnum status) {
public UserChallengeDTO participateInChallenge(Long userId, Long challengeId, StatusEnum status, UserDetails userDetails) {
if (userDetails == null) {
throw new RuntimeException("인증된 유저가 아닙니다.");
}

String username = userDetails.getUsername();
UserEntity user = userRepository.findByUid(username)
.orElseThrow(() -> new RuntimeException("유저의 uid가 " + username + "인 사용자를 찾을 수 없습니다"));

ChallengeEntity challenge = challengeRepository.findById(challengeId)
.orElseThrow(() -> new RuntimeException("챌린지 ID가 " + challengeId + "인 챌린지를 찾을 수 없습니다."));

Expand All @@ -139,9 +148,6 @@ public UserChallengeDTO participateInChallenge(Long userId, Long challengeId, St
throw new RuntimeException("이 챌린지는 진행이 완료되어 더이상 참여할 수 없습니다.");
}

UserEntity user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("유저 ID가 " + userId + "인 사용자를 찾을 수 없습니다."));

//이미 참여 중인 챌린지가 있는지 확인하는 boolean 값 정의
boolean isAlreadyParticipating = userChallengeRepository.findByUserId(userId).stream()
.anyMatch(userChallenge -> userChallenge.getChallenge().getId().equals(challengeId));
Expand Down

0 comments on commit 107d01a

Please sign in to comment.