Skip to content

Commit

Permalink
Merge pull request #9 from KNU-HAEDAL/issue/#5
Browse files Browse the repository at this point in the history
Issue/#5
  • Loading branch information
momnpa333 authored May 31, 2024
2 parents bda807a + 7848cbc commit fd7152f
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.haedal.zzansuni.controller.PagingRequest;
import org.haedal.zzansuni.controller.PagingResponse;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeService;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeService;
import org.haedal.zzansuni.global.jwt.JwtUser;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -16,23 +22,30 @@
@Tag(name = "challenge", description = "챌린지 API")
@RequiredArgsConstructor
@RestController
@Slf4j
public class ChallengeController {

private final ChallengeService challengeService;
private final UserChallengeService userChallengeService;

@Operation(summary = "챌린지 참여", description = "챌린지에 참여한다.")
@PostMapping("/api/challenges/{challengeId}/join")
public ApiResponse<Void> challengeParticipation(
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
UserChallengeCommand.Participate command = new UserChallengeCommand.Participate(challengeId,
jwtUser.getId());
userChallengeService.participateChallenge(jwtUser.getId(), command);
return ApiResponse.success(null, "챌린지 참여에 성공하였습니다.");
}

@Operation(summary = "챌린지 인증", description = "챌린지에 인증한다.")
@PostMapping("/api/challenges/{challengeId}/verification")
public ApiResponse<ChallengeRes.ChallengeVerificationResponse> challengeVerification(
@PathVariable Long challengeId,
@RequestPart("body") ChallengeReq.ChallengeVerificationRequest request,
@RequestPart("image") MultipartFile image
@PathVariable Long challengeId,
@RequestPart("body") ChallengeReq.ChallengeVerificationRequest request,
@RequestPart("image") MultipartFile image
) {
throw new RuntimeException("Not implemented");
}
Expand All @@ -41,44 +54,45 @@ public ApiResponse<ChallengeRes.ChallengeVerificationResponse> challengeVerifica
@Operation(summary = "챌린지 리뷰 작성", description = "챌린지 리뷰를 작성한다.")
@PostMapping("/api/challenges/{challengeId}/reviews")
public ApiResponse<Long> challengeReviewCreate(
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser,
@RequestBody ChallengeReq.ChallengeReviewCreateRequest request
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser,
@RequestBody ChallengeReq.ChallengeReviewCreateRequest request
) {
throw new RuntimeException("Not implemented");
}

@Operation(summary = "챌린지 기록 조회", description = "챌린지 기록을 조회한다.")
@GetMapping("/api/challenges/{challengeId}/record")
public ApiResponse<ChallengeRes.ChallengeRecordResponse> getChallengeRecord(
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
}

@Operation(summary = "챌린지 기록 상세 조회", description = "챌린지 기록 상세를 조회한다.")
@GetMapping("/api/challenges/record/{recordId}")
public ApiResponse<ChallengeRes.ChallengeRecordDetailDto> getChallengeRecordDetail(
@PathVariable Long recordId,
@AuthenticationPrincipal JwtUser jwtUser
@PathVariable Long recordId,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
}

@Operation(summary = "진행중인 챌린지 조회", description = "진행중인 챌린지 조회한다.")
@GetMapping("/api/user/challenges/currents")
public ApiResponse<PagingResponse<ChallengeRes.ChallengeCurrentDto>> getChallengeCurrentsPaging(
@Valid PagingRequest pagingRequest,
@AuthenticationPrincipal JwtUser jwtUser
@Valid PagingRequest pagingRequest,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
}

@Operation(summary = "완료한 챌린지 조회", description = "완료한 챌린지 페이징 조회한다.")
@GetMapping("/api/user/challenges/completes")
public ApiResponse<PagingResponse<ChallengeRes.ChallengeCompleteDto>> getChallengeCompletesPaging(
@Valid PagingRequest pagingRequest,
@AuthenticationPrincipal JwtUser jwtUser
@Valid PagingRequest pagingRequest,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.haedal.zzansuni.domain.challengegroup.challenge;

import java.util.Optional;

public interface ChallengeReader {

Challenge getById(Long id);

Optional<Challenge> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.haedal.zzansuni.domain.challengegroup.challenge;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -15,8 +13,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.domain.BaseTimeEntity;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.DayType;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;

@Entity
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.haedal.zzansuni.domain.challengegroup.challenge;

import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.user.UserReader;
import org.haedal.zzansuni.domain.user.UserStore;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class ChallengeService {

private final ChallengeReader challengeReader;
private final ChallengeStore challengeStore;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.haedal.zzansuni.domain.challengegroup.challenge;

public interface ChallengeStore {

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.domain.BaseTimeEntity;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.DayType;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;

@Entity
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.haedal.zzansuni.domain.challengegroup.challenge;
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand All @@ -15,8 +15,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.domain.BaseTimeEntity;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.DayType;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeStatus;
import org.haedal.zzansuni.domain.user.User;

@Entity
Expand All @@ -40,4 +40,12 @@ public class UserChallenge extends BaseTimeEntity {

@Enumerated(EnumType.STRING)
private ChallengeStatus status;

public static UserChallenge from(Challenge challenge, User user) {
return UserChallenge.builder()
.challenge(challenge)
.user(user)
.status(ChallengeStatus.PROCEEDING)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

import lombok.Builder;
import lombok.Getter;
import org.haedal.zzansuni.core.utils.SelfValidating;

public class UserChallengeCommand {

@Getter
@Builder
public static class Participate extends SelfValidating<Participate> {

private Long challengeId;
private Long userId;

public Participate(Long challengeId, Long userId) {
this.challengeId = challengeId;
this.userId = userId;
this.validateSelf();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

import java.util.Optional;

public interface UserChallengeReader {

UserChallenge getById(Long id);

Optional<UserChallenge> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeReader;
import org.haedal.zzansuni.domain.user.User;
import org.haedal.zzansuni.domain.user.UserReader;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserChallengeService {

private final UserChallengeStore userChallengeStore;
private final UserChallengeReader userChallengeReader;
private final UserReader userReader;
private final ChallengeReader challengeReader;

/**
* 챌린지 참여하기 1. 유저와 챌린지 정보를 받아서 UserChallenge 테이블에 데이터 추가
*/
public void participateChallenge(Long userId,
UserChallengeCommand.Participate participateInfo) {
User user = userReader.getById(userId);
Challenge challenge = challengeReader.getById(participateInfo.getChallengeId());
UserChallenge userChallenge = UserChallenge.from(challenge, user);
userChallengeStore.store(userChallenge);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

public interface UserChallengeStore {

UserChallenge store(UserChallenge userChallenge);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.haedal.zzansuni.exception;

public class ResourceNotFoundException extends RuntimeException {

public ResourceNotFoundException(String datasource, long id) {
super(datasource + "에서 ID " + id + "를 찾을 수 없습니다.");
}

public ResourceNotFoundException(String datasource, String id) {
super(datasource + "에서 ID " + id + "를 찾을 수 없습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.haedal.zzansuni.infrastructure.challengegroup.challenge;

import java.util.NoSuchElementException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeReader;
import org.haedal.zzansuni.exception.ResourceNotFoundException;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ChallengeReaderImpl implements ChallengeReader {

private final ChallengeRepository challengeRepository;

@Override
public Challenge getById(Long id) {
return findById(id).orElseThrow(
() -> new ResourceNotFoundException("Challenge", id));
}

@Override
public Optional<Challenge> findById(Long id) {
return challengeRepository.findById(id);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.haedal.zzansuni.infrastructure.challengegroup.challenge;

import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.haedal.zzansuni.infrastructure.challengegroup.challenge;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeStore;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ChallengeStoreImpl implements ChallengeStore {

private final ChallengeRepository challengeRepository;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.haedal.zzansuni.infrastructure.challengegroup.userchallenge;

import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeReader;
import org.haedal.zzansuni.exception.ResourceNotFoundException;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserChallengeReaderImpl implements UserChallengeReader {

private final UserChallengeRepository userChallengeRepository;

@Override
public UserChallenge getById(Long id) {
return findById(id).orElseThrow(
() -> new ResourceNotFoundException("UserChallenge", id));
}

@Override
public Optional<UserChallenge> findById(Long id) {
return userChallengeRepository.findById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.haedal.zzansuni.infrastructure.challengegroup.userchallenge;

import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserChallengeRepository extends JpaRepository<UserChallenge, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.haedal.zzansuni.infrastructure.challengegroup.userchallenge;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeStore;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserChallengeStoreImpl implements UserChallengeStore {

private final UserChallengeRepository userChallengeRepository;

@Override
public UserChallenge store(UserChallenge userChallenge) {
return userChallengeRepository.save(userChallenge);
}
}

0 comments on commit fd7152f

Please sign in to comment.