Skip to content

Commit

Permalink
Merge pull request #103 from KNU-HAEDAL/feat/review-cgi
Browse files Browse the repository at this point in the history
리뷰 upsert, 챌린지그룹 image 등록
  • Loading branch information
bayy1216 authored Sep 30, 2024
2 parents fbef814 + 63a7899 commit 4daed80
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.haedal.zzansuni.challengegroup.domain.application.ChallengeGroupService;
import org.haedal.zzansuni.common.controller.PagingRequest;
import org.haedal.zzansuni.common.controller.PagingResponse;
import org.haedal.zzansuni.common.domain.ImageUploader;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.user.domain.UserModel;
import org.haedal.zzansuni.user.domain.UserService;
Expand All @@ -18,6 +19,7 @@
import org.haedal.zzansuni.userchallenge.domain.application.ChallengeVerificationService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

Expand All @@ -30,6 +32,7 @@ public class AdminController {
private final ChallengeGroupService challengeGroupService;
private final UserService userService;
private final ChallengeVerificationService challengeVerificationService;
private final ImageUploader imageUploader;

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "매니저 등록", description = "매니저를 등록한다.")
Expand Down Expand Up @@ -91,4 +94,16 @@ public ApiResponse<Void> approveChallengeVerification(@PathVariable Long challen
return ApiResponse.success(null, "챌린지 인증 승인/거절 성공");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "챌린지 그룹 이미지 등록", description = "챌린지 그룹 이미지를 등록 UPSERT")
@PostMapping("/api/admin/challengeGroups/{challengeGroupId}/image")
public ApiResponse<Void> createChallengeGroupImage(@PathVariable Long challengeGroupId,
List<MultipartFile> images) {
List<String> imageUrls = images.stream()
.map(imageUploader::upload)
.toList();
challengeGroupService.updateChallengeGroupImages(challengeGroupId, imageUrls);
return ApiResponse.success(null, "챌린지 그룹 이미지 등록 성공");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public class ChallengeGroupImage extends BaseTimeEntity {
@JoinColumn(name = "challenge_group_id", nullable = false)
private ChallengeGroup challengeGroup;

public static ChallengeGroupImage create(ChallengeGroup challengeGroup, String imageUrl) {
return ChallengeGroupImage.builder()
.challengeGroup(challengeGroup)
.imageUrl(imageUrl)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.challengegroup.domain.ChallengeGroup;
import org.haedal.zzansuni.challengegroup.domain.ChallengeGroupCommand;
import org.haedal.zzansuni.challengegroup.domain.ChallengeGroupImage;
import org.haedal.zzansuni.challengegroup.domain.port.ChallengeGroupImageStore;
import org.haedal.zzansuni.challengegroup.domain.port.ChallengeGroupReader;
import org.haedal.zzansuni.challengegroup.domain.port.ChallengeGroupStore;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@RequiredArgsConstructor
public class ChallengeGroupService {
private final ChallengeGroupStore challengeGroupStore;
private final ChallengeGroupReader challengeGroupReader;
private final ChallengeGroupImageStore challengeGroupImageStore;

@Transactional
public void createChallengeGroup(ChallengeGroupCommand.Create command) {
Expand All @@ -33,4 +38,19 @@ public void updateChallengeGroup(ChallengeGroupCommand.Update command) {
ChallengeGroup challengeGroup = challengeGroupReader.getById(command.getId());
challengeGroup.update(command);
}

/**
* 1. 기존 이미지 삭제
* 2. 새로운 이미지 저장
*/
@Transactional
public void updateChallengeGroupImages(Long challengeGroupId, List<String> imageUrls) {
ChallengeGroup challengeGroup = challengeGroupReader.getById(challengeGroupId);

challengeGroupImageStore.deleteAllByChallengeGroupId(challengeGroupId);
List<ChallengeGroupImage> challengeGroupImages = imageUrls.stream()
.map(imageUrl -> ChallengeGroupImage.create(challengeGroup, imageUrl))
.toList();
challengeGroupImageStore.saveAll(challengeGroupImages);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.haedal.zzansuni.challengegroup.domain.port;

import org.haedal.zzansuni.challengegroup.domain.ChallengeGroupImage;

import java.util.List;

public interface ChallengeGroupImageStore {
void saveAll(List<ChallengeGroupImage> challengeGroupImages);
void deleteAllByChallengeGroupId(Long challengeGroupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public interface ChallengeGroupImageRepository extends JpaRepository<ChallengeGroupImage, Long> {
List<ChallengeGroupImage> findByChallengeGroupId(Long challengeGroupId);
void deleteAllByChallengeGroupId(Long challengeGroupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.challengegroup.domain.ChallengeGroupImage;
import org.haedal.zzansuni.challengegroup.domain.port.ChallengeGroupImageReader;
import org.haedal.zzansuni.challengegroup.domain.port.ChallengeGroupImageStore;
import org.haedal.zzansuni.challengegroup.infrastructure.ChallengeGroupImageRepository;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class ChallengeGroupImageReaderImpl implements ChallengeGroupImageReader {
public class ChallengeGroupImageReaderStoreImpl implements ChallengeGroupImageReader, ChallengeGroupImageStore {
private final ChallengeGroupImageRepository challengeGroupImageRepository;
@Override
public List<ChallengeGroupImage> getByChallengeGroupId(Long challengeGroupId) {
return challengeGroupImageRepository.findByChallengeGroupId(challengeGroupId);
}

@Override
public void saveAll(List<ChallengeGroupImage> challengeGroupImages) {
challengeGroupImageRepository.saveAll(challengeGroupImages);
}

@Override
public void deleteAllByChallengeGroupId(Long challengeGroupId) {
challengeGroupImageRepository.deleteAllByChallengeGroupId(challengeGroupId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public ApiResponse<PagingResponse<ChallengeReviewRes.InfoWithChallenge>> getChal
}

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "챌린지 리뷰 작성", description = "챌린지 리뷰를 작성한다.")
@Operation(summary = "챌린지 리뷰 Upsert", description = "챌린지 리뷰를 생성하거나 수정합니다.")
@PostMapping("/api/challenges/{challengeId}/reviews")
public ApiResponse<Long> challengeReviewCreate(
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser,
@RequestBody ChallengeReviewReq.Create request
) {
Long response = challengeReviewService.createReview(request.toCommand(), challengeId,
Long response = challengeReviewService.upsertReview(request.toCommand(), challengeId,
jwtUser.getId());
return ApiResponse.success(response, "챌린지 리뷰 작성에 성공하였습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public record Create(
Integer achievement
) {

public ChallengeReviewCommand.Create toCommand() {
return ChallengeReviewCommand.Create.builder()
public ChallengeReviewCommand.Upsert toCommand() {
return ChallengeReviewCommand.Upsert.builder()
.content(content)
.rating(rating)
.difficulty(difficulty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ public class ChallengeReview extends BaseTimeEntity {
@Column(nullable = false)
private Integer achievement;

public static ChallengeReview create(UserChallenge userChallenge, ChallengeReviewCommand.Create command) {
public void update(ChallengeReviewCommand.Upsert command) {
this.content = command.getContent();
this.rating = command.getRating();
this.difficulty = command.getDifficulty();
this.achievement = command.getAchievement();
}

public static ChallengeReview create(UserChallenge userChallenge, ChallengeReviewCommand.Upsert command) {
Long challengeGroupId = userChallenge.getChallenge().getChallengeGroupId();
return ChallengeReview.builder()
.userChallenge(userChallenge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class ChallengeReviewCommand {
@Getter
public static class Create extends SelfValidating<Create> {
public static class Upsert extends SelfValidating<Upsert> {

@NotBlank(message = "내용은 필수입니다.")
private final String content;
Expand All @@ -31,7 +31,7 @@ public static class Create extends SelfValidating<Create> {
private final Integer achievement;

@Builder
public Create(String content, Integer rating, Integer difficulty, Integer achievement) {
public Upsert(String content, Integer rating, Integer difficulty, Integer achievement) {
this.content = content;
this.rating = rating;
this.difficulty = difficulty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
Expand All @@ -21,19 +19,19 @@ public class ChallengeReviewService {


/**
* 챌린지 리뷰 작성하기
* 챌린지 리뷰 작성하기 / 수정하기
*/
@Transactional
public Long createReview(ChallengeReviewCommand.Create command, Long challengeId, Long userId) {
public Long upsertReview(ChallengeReviewCommand.Upsert command, Long challengeId, Long userId) {
UserChallenge userChallenge = userChallengeReader.findByUserIdAndChallengeId(userId,
challengeId).orElseThrow(() -> new IllegalStateException("해당 챌린지 참여 기록이 없습니다."));
challengeId).orElseThrow(() -> new IllegalStateException("해당 챌린지 참여 기록이 없습니다."));

//이미 리뷰를 작성했는지 확인
challengeReviewReader.findByUserChallengeId(userChallenge.getId())
.ifPresent(review -> {
throw new IllegalArgumentException("이미 리뷰를 작성했습니다.");
});
ChallengeReview challengeReview = ChallengeReview.create(userChallenge, command);
ChallengeReview challengeReview = challengeReviewReader.findByUserChallengeId(userChallenge.getId())
.map(review -> {
review.update(command);
return review;
})
.orElseGet(() -> ChallengeReview.create(userChallenge, command));
challengeReviewStore.store(challengeReview);
return challengeReview.getId();
}
Expand All @@ -43,9 +41,9 @@ public Long createReview(ChallengeReviewCommand.Create command, Long challengeId
*/
@Transactional(readOnly = true)
public Page<ChallengeReviewModel.ChallengeReviewWithChallenge> getChallengeReviewsByGroupId(
Long challengeGroupId, Pageable pageable) {
Long challengeGroupId, Pageable pageable) {
Page<ChallengeReview> challengeReviewPage = challengeReviewReader.getChallengeReviewPageByChallengeGroupId(
challengeGroupId, pageable);
challengeGroupId, pageable);

return challengeReviewPage.map(ChallengeReviewModel.ChallengeReviewWithChallenge::from);
}
Expand All @@ -56,7 +54,7 @@ public Page<ChallengeReviewModel.ChallengeReviewWithChallenge> getChallengeRevie
@Transactional(readOnly = true)
public Page<ChallengeReviewModel.ChallengeReviewWithUserInfo> getChallengeReviews(Pageable pageable) {
Page<ChallengeReview> challengeReviewPage = challengeReviewReader.getChallengeReviewPage(
pageable);
pageable);

return challengeReviewPage.map(ChallengeReviewModel.ChallengeReviewWithUserInfo::from);
}
Expand All @@ -66,7 +64,7 @@ public Page<ChallengeReviewModel.ChallengeReviewWithUserInfo> getChallengeReview
*/
@Transactional(readOnly = true)
public ChallengeReviewModel.Score getChallengeGroupReviewScore(
Long challengeGroupId) {
Long challengeGroupId) {
return challengeReviewReader.getScoreModelByChallengeGroupId(challengeGroupId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.springframework.data.domain.Pageable;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -145,7 +144,7 @@ void getChallengeRecordDetail() {
void createReview() {
Long userId = 1L;
Long challengeId = 1L;
ChallengeReviewCommand.Create command = new ChallengeReviewCommand.Create(
ChallengeReviewCommand.Upsert command = new ChallengeReviewCommand.Upsert(
"Great challenge!", 5,1,1);

when(userChallengeReader.findByUserIdAndChallengeId(userId, challengeId)).thenReturn(
Expand All @@ -167,7 +166,7 @@ void createReview() {
});

//TODO: ChallengeReview가 정적메서드라 테스트하기 어려움
Long reviewId = challengeReviewService.createReview(command, challengeId, userId);
Long reviewId = challengeReviewService.upsertReview(command, challengeId, userId);

//assertNotNull(reviewId); // 리뷰 ID가 null이 아닌지 확인
//assertEquals(1L, reviewId); // 리뷰 ID가 1L인지 확인
Expand Down

0 comments on commit 4daed80

Please sign in to comment.