Skip to content

Commit

Permalink
Merge pull request #18 from KNU-HAEDAL/issue/#5-2
Browse files Browse the repository at this point in the history
Issue/#5 2
  • Loading branch information
momnpa333 authored Jun 1, 2024
2 parents c3ea10d + 64f077a commit 9ea7ccf
Show file tree
Hide file tree
Showing 31 changed files with 662 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
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.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeService;
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;
Expand All @@ -26,26 +27,30 @@ public class ChallengeController {
private final ChallengeService challengeService;
private final UserChallengeService userChallengeService;

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

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "챌린지 인증", description = "챌린지에 인증한다.")
@PostMapping("/api/challenges/{challengeId}/verification")
@PostMapping("/api/challenges/{userChallengeId}/verification")
public ApiResponse<ChallengeRes.ChallengeVerificationResponse> challengeVerification(
@PathVariable Long challengeId,
@PathVariable Long userChallengeId,
@RequestPart("body") ChallengeReq.ChallengeVerificationRequest request,
@RequestPart("image") MultipartFile image
) {
throw new RuntimeException("Not implemented");
ChallengeCommand.Verificate command = request.toCommand(image);
ChallengeModel.ChallengeVerificationResult model = userChallengeService.verification(
userChallengeId, command);
var response = ChallengeRes.ChallengeVerificationResponse.from(model);
return ApiResponse.success(response, "챌린지 인증에 성공하였습니다.");
}

@ResponseStatus(HttpStatus.CREATED)
Expand All @@ -56,33 +61,45 @@ public ApiResponse<Long> challengeReviewCreate(
@AuthenticationPrincipal JwtUser jwtUser,
@RequestBody ChallengeReq.ChallengeReviewCreateRequest request
) {
throw new RuntimeException("Not implemented");
Long response = challengeService.createReview(request.toCommand(), challengeId,
jwtUser.getId());
return ApiResponse.success(response, "챌린지 리뷰 작성에 성공하였습니다.");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "챌린지 기록 조회", description = "챌린지 기록을 조회한다.")
@GetMapping("/api/challenges/{challengeId}/record")
public ApiResponse<ChallengeRes.ChallengeRecordResponse> getChallengeRecord(
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
ChallengeRes.ChallengeRecordResponse response = ChallengeRes.ChallengeRecordResponse.from(
challengeService.getChallengeRecord(jwtUser.getId(), challengeId)
);
return ApiResponse.success(response, "챌린지 기록 조회에 성공하였습니다.");
}


@Operation(summary = "챌린지 기록 상세 조회", description = "챌린지 기록 상세를 조회한다.")
@GetMapping("/api/challenges/record/{recordId}")
public ApiResponse<ChallengeRes.ChallengeRecordDetailDto> getChallengeRecordDetail(
@PathVariable Long recordId,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
ChallengeRes.ChallengeRecordDetailDto response = ChallengeRes.ChallengeRecordDetailDto.from(
challengeService.getChallengeRecordDetail(recordId)
);
log.info("response: {}", response);
return ApiResponse.success(response, "챌린지 기록 상세 조회에 성공하였습니다.");
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
package org.haedal.zzansuni.controller.challengegroup.challenge;

import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.springframework.web.multipart.MultipartFile;

public class ChallengeReq {

public record ChallengeVerificationRequest(
String content
String content
) {

public ChallengeCommand.Verificate toCommand(MultipartFile image) {
return ChallengeCommand.Verificate.builder()
.content(content)
.image(image)
.build();
}

}

public record ChallengeReviewCreateRequest(
String content,
Integer rating
String content,
Integer rating
) {

public ChallengeCommand.ReviewCreate toCommand() {
return ChallengeCommand.ReviewCreate.builder()
.content(content)
.rating(rating)
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeRecord;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerificationModel;

public class ChallengeRes {

Expand All @@ -16,6 +19,14 @@ public record ChallengeVerificationResponse(
Integer obtainExp
) {

public static ChallengeVerificationResponse from(
ChallengeModel.ChallengeVerificationResult result) {
return ChallengeVerificationResponse.builder()
.totalCount(result.totalCount())
.successCount(result.successCount())
.obtainExp(result.obtainExp())
.build();
}
}

@Builder
Expand All @@ -28,6 +39,16 @@ public record ChallengeRecordResponse(
List<Long> recordIds
) {

public static ChallengeRecordResponse from(ChallengeRecord dto) {
return ChallengeRecordResponse.builder()
.title(dto.title())
.totalCount(dto.totalCount())
.successCount(dto.successCount())
.startDate(dto.startDate())
.endDate(dto.endDate())
.recordIds(dto.recordIds())
.build();
}
}

@Builder
Expand All @@ -38,17 +59,27 @@ public record ChallengeRecordDetailDto(
String imageUrl
) {

public static ChallengeRecordDetailDto from(ChallengeVerificationModel model) {
return ChallengeRecordDetailDto.builder()
.id(model.getId())
.createdAt(model.getCreatedAt())
.content(model.getContent())
.imageUrl(model.getImageUrl())
.build();

}

}


@Builder
public record ChallengeCurrentDto(
Long id,
public record ChallengeCurrentResponse(
Long challengeId,
String title,
Integer successCount,
Integer totalCount,

LocalDate participationDate,
LocalDateTime participationDate,
LocalDate startDate,
LocalDate endDate,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -44,5 +45,9 @@ public class Challenge extends BaseTimeEntity {

private Integer difficulty;

private LocalDate startDate;

private LocalDate endDate;


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

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import org.haedal.zzansuni.core.utils.SelfValidating;
import org.springframework.web.multipart.MultipartFile;

public class ChallengeCommand {

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

@NotBlank(message = "내용은 필수입니다.")
private final String content;

@NotNull(message = "이미지는 필수입니다.")
private final MultipartFile image;

public Verificate(String content, MultipartFile image) {
this.content = content;
this.image = image;
this.validateSelf();
}
}

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

@NotBlank(message = "내용은 필수입니다.")
private final String content;

@NotNull(message = "평점은 필수입니다.")
private final Integer rating;

public ReviewCreate(String content, Integer rating) {
this.content = content;
this.rating = rating;
this.validateSelf();
}
}

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

import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.haedal.zzansuni.domain.challengegroup.ChallengeCategory;

@Getter
public class ChallengeCurrentDto {

private final Long challengeId;
private final String title;
private final Integer successCount;
private final Integer totalCount;
private final LocalDateTime participationDate;
private final LocalDate startDate;
private final LocalDate endDate;
private final ChallengeCategory category;
private final Boolean reviewWritten;

// 생성자 추가
public ChallengeCurrentDto(Long challengeId, String title, Integer successCount,
Integer totalCount, LocalDateTime participationDate, LocalDate startDate, LocalDate endDate,
ChallengeCategory category, Boolean reviewWritten) {
this.challengeId = challengeId;
this.title = title;
this.successCount = successCount;
this.totalCount = totalCount;
this.participationDate = participationDate;
this.startDate = startDate;
this.endDate = endDate;
this.category = category;
this.reviewWritten = reviewWritten;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.haedal.zzansuni.domain.challengegroup.challenge;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.Getter;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.DayType;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerification;

@Getter
@Builder
public class ChallengeModel {

private Long id;
private ChallengeGroup challengeGroup;
private Integer requiredCount;
private DayType dayType;
private Integer onceExp;
private Integer successExp;
private Integer difficulty;

public static ChallengeModel from(Challenge challenge) {
return ChallengeModel.builder()
.id(challenge.getId())
.challengeGroup(challenge.getChallengeGroup())
.requiredCount(challenge.getRequiredCount())
.dayType(challenge.getDayType())
.onceExp(challenge.getOnceExp())
.successExp(challenge.getSuccessExp())
.difficulty(challenge.getDifficulty())
.build();
}

@Builder
public record ChallengeVerificationResult(Integer totalCount, Integer successCount,
Integer obtainExp) {

public static ChallengeVerificationResult of(Integer totalCount, Integer successCount,
Integer obtainExp) {
return ChallengeVerificationResult.builder()
.totalCount(totalCount)
.successCount(successCount)
.obtainExp(obtainExp)
.build();
}

public static ChallengeVerificationResult from(Integer totalCount, Integer successCount,
Integer obtainExp) {
return ChallengeVerificationResult.builder()
.totalCount(totalCount)
.successCount(successCount)
.obtainExp(obtainExp)
.build();
}
}


@Builder
public record ChallengeRecord(String title, Integer totalCount, Integer successCount,
LocalDate startDate, LocalDate endDate, List<Long> recordIds) {

public static ChallengeRecord from(Challenge challenge, ChallengeGroup challengeGroup,
List<ChallengeVerification> challengeVerificationList) {
return ChallengeRecord.builder()
.title(challengeGroup.getTitle())
.totalCount(challenge.getRequiredCount())
.successCount(challengeVerificationList.size())
.startDate(challenge.getStartDate())
.endDate(challenge.getEndDate())
.recordIds(challengeVerificationList.stream()
.map(ChallengeVerification::getId)
.collect(Collectors.toList()))
.build();
}
}


}
Loading

0 comments on commit 9ea7ccf

Please sign in to comment.