Skip to content

Commit

Permalink
Merge pull request #19 from KNU-HAEDAL/refactor/domain
Browse files Browse the repository at this point in the history
Refactor/domain
  • Loading branch information
bayy1216 authored Jun 1, 2024
2 parents 9ea7ccf + e9f3fb0 commit 4c3a6f5
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.haedal.zzansuni.controller.PagingRequest;
import org.haedal.zzansuni.controller.PagingResponse;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.domain.ImageUploader;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeService;
Expand All @@ -26,6 +27,7 @@ public class ChallengeController {

private final ChallengeService challengeService;
private final UserChallengeService userChallengeService;
private final ImageUploader imageUploader;

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "챌린지 참여", description = "챌린지에 참여한다.")
Expand All @@ -47,8 +49,9 @@ public ApiResponse<ChallengeRes.ChallengeVerificationResponse> challengeVerifica
@RequestPart("image") MultipartFile image
) {
ChallengeCommand.Verificate command = request.toCommand(image);
ChallengeModel.ChallengeVerificationResult model = userChallengeService.verification(
userChallengeId, command);
String imageUrl = imageUploader.upload(command.getImage());
ChallengeCommand.VerificationCreate afterUpload = command.afterUpload(imageUrl);
var model = userChallengeService.verification(userChallengeId, afterUpload);
var response = ChallengeRes.ChallengeVerificationResponse.from(model);
return ApiResponse.success(response, "챌린지 인증에 성공하였습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.haedal.zzansuni.domain;

import org.springframework.web.multipart.MultipartFile;

public interface ImageUploader {
String upload(MultipartFile imageFile);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.haedal.zzansuni.domain.challengegroup;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.domain.BaseTimeEntity;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;

import java.util.ArrayList;
import java.util.List;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -31,6 +30,10 @@ public class ChallengeGroup extends BaseTimeEntity {

private String content;

private String guide;

private Integer cumulativeCount;

@OneToMany(mappedBy = "challengeGroup", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Challenge> challenges = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ public Verificate(String content, MultipartFile image) {
this.image = image;
this.validateSelf();
}

public VerificationCreate afterUpload(String imageUrl) {
return VerificationCreate.builder()
.content(content)
.imageUrl(imageUrl)
.build();
}
}

@Getter
@Builder
public static class VerificationCreate extends SelfValidating<Verificate> {
@NotBlank(message = "내용은 필수입니다.")
private final String content;
private final String imageUrl;

public VerificationCreate(String content, String imageUrl) {
this.content = content;
this.imageUrl = imageUrl;
this.validateSelf();
}
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
package org.haedal.zzansuni.domain.challengegroup.challengeverification;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import jakarta.persistence.*;
import lombok.*;
import org.haedal.zzansuni.domain.BaseTimeEntity;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand.Verificate;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeVerificationStatus;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;

Expand All @@ -41,11 +29,10 @@ public class ChallengeVerification extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private ChallengeVerificationStatus status;

public static ChallengeVerification create(Verificate command,
UserChallenge userChallenge) {
public static ChallengeVerification create(ChallengeCommand.VerificationCreate command, UserChallenge userChallenge) {
return ChallengeVerification.builder()
.userChallenge(userChallenge)
.imageUrl(null)
.imageUrl(command.getImageUrl())
.content(command.getContent())
.status(ChallengeVerificationStatus.APPROVED)
.build();
Expand Down

This file was deleted.

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

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.domain.BaseTimeEntity;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeStatus;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerification;
import org.haedal.zzansuni.domain.user.User;

import java.util.ArrayList;
import java.util.List;

@Entity
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -41,26 +38,31 @@ public class UserChallenge extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private ChallengeStatus status;

@OneToMany(mappedBy = "userChallenge", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<ChallengeVerification> challengeVerifications = new ArrayList<>();

public static UserChallenge create(Challenge challenge, User user) {
return UserChallenge.builder()
.challenge(challenge)
.user(user)
.status(ChallengeStatus.PROCEEDING)
.build();
.challenge(challenge)
.user(user)
.status(ChallengeStatus.PROCEEDING)
.build();
}

private void complete() {
this.status = ChallengeStatus.SUCCESS;
}
public void addChallengeVerification(ChallengeCommand.VerificationCreate command) {
ChallengeVerification challengeVerification = ChallengeVerification.create(command, this);
this.challengeVerifications.add(challengeVerification);

/**
* 챌린지 인증 참여횟수와 필요참여획수가 같으면 챌린지 완료로 변경
*/
public void tryComplete(Integer currentCount, Integer requiredCount) {
if (currentCount.equals(requiredCount)) {
// 만약 챌린지 인증 참여횟수와 필요참여획수가 같으면 챌린지 완료로 변경
if(this.challengeVerifications.size() == this.challenge.getRequiredCount()) {
complete();
}
}


private void complete() {
this.status = ChallengeStatus.SUCCESS;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public interface UserChallengeReader {

UserChallenge getById(Long id);
UserChallenge getByIdWithVerificationAndChallenge(Long id);

Optional<UserChallenge> findById(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand.Verificate;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeReader;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerification;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerificationReader;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerificationStore;
import org.haedal.zzansuni.domain.user.User;
import org.haedal.zzansuni.domain.user.UserReader;
import org.springframework.stereotype.Service;
Expand All @@ -24,9 +21,6 @@ public class UserChallengeService {
private final UserReader userReader;
private final ChallengeReader challengeReader;

private final ChallengeVerificationStore challengeVerificationStore;
private final ChallengeVerificationReader challengeVerificationReader;

/**
* 챌린지 참여하기 1. 유저와 챌린지 정보를 받아서 UserChallenge 테이블에 데이터 추가
*/
Expand Down Expand Up @@ -54,26 +48,19 @@ public void participateChallenge(Long userId, Long challengeId) {
* UserChallenge 엔티티에서 참여횟수 가져오기 <br> 4. 참여횟수와 필요참여횟수가 같으면 챌린지 완료로 변경
*/
@Transactional
public ChallengeModel.ChallengeVerificationResult verification(Long userChallengeId,
Verificate verificate) {
UserChallenge userChallenge = userChallengeReader.getById(userChallengeId);
// TODO 이미지 업로드 로직 필요
ChallengeVerification challengeVerification = ChallengeVerification.create(verificate,
userChallenge);
challengeVerificationStore.store(challengeVerification);
public ChallengeModel.ChallengeVerificationResult verification(
Long userChallengeId,
ChallengeCommand.VerificationCreate command
) {
UserChallenge userChallenge = userChallengeReader.getByIdWithVerificationAndChallenge(userChallengeId);
userChallenge.addChallengeVerification(command);

// 챌린지 RequiredCount 가져오기 위해 챌린지 정보 가져온다

// 챌린지 RequiredCount 가져오기 위해 챌린지 정보 가져온다
Challenge challenge = userChallenge.getChallenge();

//사용자가 챌린지를 몇번 수행했는지 가져온다
Integer currentCount = challengeVerificationReader.countByUserChallengeId(userChallengeId);
log.info("currentCount: {}", currentCount);

//참여횟수와 필요참여횟수가 같으면 챌린지 완료로 변경
userChallenge.tryComplete(currentCount, challenge.getRequiredCount());

return ChallengeModel.ChallengeVerificationResult
.of(challenge.getRequiredCount(), currentCount, challenge.getOnceExp());
.of(challenge.getRequiredCount(), userChallenge.getChallengeVerifications().size(), challenge.getOnceExp());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.haedal.zzansuni.infrastructure;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.ImageUploader;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
@RequiredArgsConstructor
public class FakeUploader implements ImageUploader {
@Override
public String upload(MultipartFile imageFile) {
return "https://loremflickr.com/640/480?lock=3881982306025472";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,5 @@

public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

@Query(
"SELECT new org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCurrentDto("
+
"c.id, cg.title, " +
"(SELECT COUNT(cv) FROM ChallengeVerification cv WHERE cv.userChallenge.challenge.id = c.id AND cv.userChallenge.user.id = :userId), "
+
"c.requiredCount, " +
"uc.createdAt, c.startDate, c.endDate, " +
"cg.category, " +
"(SELECT COUNT(cr) > 0 FROM ChallengeReview cr WHERE cr.userChallenge.challenge.id = c.id AND cr.userChallenge.user.id = :userId)"
+
") " +
"FROM Challenge c " +
"JOIN c.challengeGroup cg " +
"JOIN UserChallenge uc ON uc.challenge.id = c.id " +
"WHERE uc.user.id = :userId " +
"AND uc.status = org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeStatus.PROCEEDING ")
Page<ChallengeCurrentDto> findAllCurrentChallenges(@Param("userId") Long userId,
Pageable pageable);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.NoSuchElementException;
import java.util.Optional;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeReader;
Expand All @@ -18,15 +19,22 @@ public UserChallenge getById(Long id) {
return findById(id).orElseThrow(NoSuchElementException::new);
}

@Override
public UserChallenge getByIdWithVerificationAndChallenge(Long id) {
return userChallengeRepository
.findByIdWithFetchLazy(id)
.orElseThrow(NoSuchElementException::new);
}

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

@Override
public UserChallenge getByUserIdAndChallengeId(Long userId, Long challengeId) {
return findByUserIdAndChallengeId(userId, challengeId).orElseThrow(
NoSuchElementException::new);
return findByUserIdAndChallengeId(userId, challengeId)
.orElseThrow(NoSuchElementException::new);
}

@Override
Expand Down
Loading

0 comments on commit 4c3a6f5

Please sign in to comment.