From 04a5929d4b8fb53fac833425f1e0420fb69149ec Mon Sep 17 00:00:00 2001 From: 5win <94297900+5win@users.noreply.github.com> Date: Sat, 2 Nov 2024 19:52:00 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EB=A6=AC=EB=B7=B0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=A4=91=EA=B0=84=20=EC=BB=A4=EB=B0=8B=20(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [#55] feat: 질문 리스트 반환 API 구현 * [#55] rename: Review에서 Question으로 명명 변경 * [#55] feat: 리뷰 기능 구현 중간 커밋 --- .../review/controller/QuestionController.java | 23 ++++++ .../review/controller/ReviewController.java | 31 ++++++++ .../java/com/gamsa/review/domain/Answer.java | 12 ++++ .../com/gamsa/review/domain/Question.java | 12 ++++ .../java/com/gamsa/review/domain/Review.java | 12 ++++ .../gamsa/review/dto/AnswerSaveRequest.java | 12 ++++ .../gamsa/review/dto/QuestionResponse.java | 20 ++++++ .../gamsa/review/dto/ReviewSaveRequest.java | 13 ++++ .../gamsa/review/entity/AnswerJpaEntity.java | 19 +++++ .../review/entity/QuestionJpaEntity.java | 38 ++++++++++ .../gamsa/review/entity/ReviewJpaEntity.java | 33 +++++++++ .../review/repository/AnswerRepository.java | 8 +++ .../repository/QuestionJpaRepository.java | 8 +++ .../review/repository/QuestionRepository.java | 12 ++++ .../repository/QuestionRepositoryImpl.java | 28 ++++++++ .../review/repository/ReviewRepository.java | 8 +++ .../gamsa/review/service/QuestionService.java | 21 ++++++ .../gamsa/review/service/ReviewService.java | 71 +++++++++++++++++++ 18 files changed, 381 insertions(+) create mode 100644 src/main/java/com/gamsa/review/controller/QuestionController.java create mode 100644 src/main/java/com/gamsa/review/controller/ReviewController.java create mode 100644 src/main/java/com/gamsa/review/domain/Answer.java create mode 100644 src/main/java/com/gamsa/review/domain/Question.java create mode 100644 src/main/java/com/gamsa/review/domain/Review.java create mode 100644 src/main/java/com/gamsa/review/dto/AnswerSaveRequest.java create mode 100644 src/main/java/com/gamsa/review/dto/QuestionResponse.java create mode 100644 src/main/java/com/gamsa/review/dto/ReviewSaveRequest.java create mode 100644 src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java create mode 100644 src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java create mode 100644 src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java create mode 100644 src/main/java/com/gamsa/review/repository/AnswerRepository.java create mode 100644 src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java create mode 100644 src/main/java/com/gamsa/review/repository/QuestionRepository.java create mode 100644 src/main/java/com/gamsa/review/repository/QuestionRepositoryImpl.java create mode 100644 src/main/java/com/gamsa/review/repository/ReviewRepository.java create mode 100644 src/main/java/com/gamsa/review/service/QuestionService.java create mode 100644 src/main/java/com/gamsa/review/service/ReviewService.java diff --git a/src/main/java/com/gamsa/review/controller/QuestionController.java b/src/main/java/com/gamsa/review/controller/QuestionController.java new file mode 100644 index 0000000..78ed9a5 --- /dev/null +++ b/src/main/java/com/gamsa/review/controller/QuestionController.java @@ -0,0 +1,23 @@ +package com.gamsa.review.controller; + +import com.gamsa.review.service.QuestionService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/reviews") +public class QuestionController { + + private final QuestionService questionService; + + @GetMapping("/questions") + public ResponseEntity findAllQuestion() { + return ResponseEntity.ok(questionService.findAllResponse()); + } + + +} diff --git a/src/main/java/com/gamsa/review/controller/ReviewController.java b/src/main/java/com/gamsa/review/controller/ReviewController.java new file mode 100644 index 0000000..d6646c8 --- /dev/null +++ b/src/main/java/com/gamsa/review/controller/ReviewController.java @@ -0,0 +1,31 @@ +package com.gamsa.review.controller; + + +import com.gamsa.common.utils.ExtractUserIdFromJwt; +import com.gamsa.review.dto.ReviewSaveRequest; +import com.gamsa.review.service.ReviewService; +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/reviews") +public class ReviewController { + + private final ReviewService reviewService; + + @PostMapping + public ResponseEntity saveReview(@RequestBody ReviewSaveRequest saveRequest, + HttpServletRequest request) { + Long userId = ExtractUserIdFromJwt.extract(request); + reviewService.saveReview(userId, saveRequest); + + return new ResponseEntity<>(HttpStatus.CREATED); + } +} diff --git a/src/main/java/com/gamsa/review/domain/Answer.java b/src/main/java/com/gamsa/review/domain/Answer.java new file mode 100644 index 0000000..a1c07e9 --- /dev/null +++ b/src/main/java/com/gamsa/review/domain/Answer.java @@ -0,0 +1,12 @@ +package com.gamsa.review.domain; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class Answer { + + private Question question; + private int score; +} diff --git a/src/main/java/com/gamsa/review/domain/Question.java b/src/main/java/com/gamsa/review/domain/Question.java new file mode 100644 index 0000000..66dfbf7 --- /dev/null +++ b/src/main/java/com/gamsa/review/domain/Question.java @@ -0,0 +1,12 @@ +package com.gamsa.review.domain; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class Question { + + private Integer questionId; + private String content; +} diff --git a/src/main/java/com/gamsa/review/domain/Review.java b/src/main/java/com/gamsa/review/domain/Review.java new file mode 100644 index 0000000..3495742 --- /dev/null +++ b/src/main/java/com/gamsa/review/domain/Review.java @@ -0,0 +1,12 @@ +package com.gamsa.review.domain; + +import java.util.List; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class Review { + + private List answers; +} diff --git a/src/main/java/com/gamsa/review/dto/AnswerSaveRequest.java b/src/main/java/com/gamsa/review/dto/AnswerSaveRequest.java new file mode 100644 index 0000000..e7738f8 --- /dev/null +++ b/src/main/java/com/gamsa/review/dto/AnswerSaveRequest.java @@ -0,0 +1,12 @@ +package com.gamsa.review.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class AnswerSaveRequest { + + private final Integer questionId; + private final int score; +} diff --git a/src/main/java/com/gamsa/review/dto/QuestionResponse.java b/src/main/java/com/gamsa/review/dto/QuestionResponse.java new file mode 100644 index 0000000..a56fb82 --- /dev/null +++ b/src/main/java/com/gamsa/review/dto/QuestionResponse.java @@ -0,0 +1,20 @@ +package com.gamsa.review.dto; + +import com.gamsa.review.domain.Question; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class QuestionResponse { + + private final Integer questionId; + private final String content; + + public static QuestionResponse from(Question question) { + return QuestionResponse.builder() + .questionId(question.getQuestionId()) + .content(question.getContent()) + .build(); + } +} diff --git a/src/main/java/com/gamsa/review/dto/ReviewSaveRequest.java b/src/main/java/com/gamsa/review/dto/ReviewSaveRequest.java new file mode 100644 index 0000000..01c6367 --- /dev/null +++ b/src/main/java/com/gamsa/review/dto/ReviewSaveRequest.java @@ -0,0 +1,13 @@ +package com.gamsa.review.dto; + +import java.util.List; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class ReviewSaveRequest { + + private final Long historyId; + private final List answers; +} diff --git a/src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java b/src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java new file mode 100644 index 0000000..49ddf84 --- /dev/null +++ b/src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java @@ -0,0 +1,19 @@ +package com.gamsa.review.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +@Table(name = "answer") +public class AnswerJpaEntity { + +} diff --git a/src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java b/src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java new file mode 100644 index 0000000..b14f56b --- /dev/null +++ b/src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java @@ -0,0 +1,38 @@ +package com.gamsa.review.entity; + +import com.gamsa.review.domain.Question; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +@Table(name = "question") +public class QuestionJpaEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "question_id") + private Integer questionId; + + @Column(name = "content", length = 255) + private String content; + + public Question toModel() { + return Question.builder() + .questionId(questionId) + .content(content) + .build(); + } +} diff --git a/src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java b/src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java new file mode 100644 index 0000000..3f9f074 --- /dev/null +++ b/src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java @@ -0,0 +1,33 @@ +package com.gamsa.review.entity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import java.util.List; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +@Table(name = "review") +public class ReviewJpaEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "review_id") + private Long reviewId; + + @OneToMany + private List answers; + +} diff --git a/src/main/java/com/gamsa/review/repository/AnswerRepository.java b/src/main/java/com/gamsa/review/repository/AnswerRepository.java new file mode 100644 index 0000000..360c910 --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/AnswerRepository.java @@ -0,0 +1,8 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.domain.Answer; + +public interface AnswerRepository { + + void save(Answer answer); +} diff --git a/src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java b/src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java new file mode 100644 index 0000000..761400d --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java @@ -0,0 +1,8 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.entity.QuestionJpaEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface QuestionJpaRepository extends JpaRepository { + +} diff --git a/src/main/java/com/gamsa/review/repository/QuestionRepository.java b/src/main/java/com/gamsa/review/repository/QuestionRepository.java new file mode 100644 index 0000000..b4eb8d9 --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/QuestionRepository.java @@ -0,0 +1,12 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.domain.Question; +import java.util.List; +import java.util.Optional; + +public interface QuestionRepository { + + List findAllQuestion(); + + Optional findById(Integer questionId); +} diff --git a/src/main/java/com/gamsa/review/repository/QuestionRepositoryImpl.java b/src/main/java/com/gamsa/review/repository/QuestionRepositoryImpl.java new file mode 100644 index 0000000..67e22f9 --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/QuestionRepositoryImpl.java @@ -0,0 +1,28 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.domain.Question; +import com.gamsa.review.entity.QuestionJpaEntity; +import java.util.List; +import java.util.Optional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +@RequiredArgsConstructor +@Repository +public class QuestionRepositoryImpl implements QuestionRepository { + + private final QuestionJpaRepository questionJpaRepository; + + @Override + public List findAllQuestion() { + return questionJpaRepository.findAll().stream() + .map(QuestionJpaEntity::toModel) + .toList(); + } + + @Override + public Optional findById(Integer questionId) { + return questionJpaRepository.findById(questionId) + .map(QuestionJpaEntity::toModel); + } +} diff --git a/src/main/java/com/gamsa/review/repository/ReviewRepository.java b/src/main/java/com/gamsa/review/repository/ReviewRepository.java new file mode 100644 index 0000000..dfe55c2 --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/ReviewRepository.java @@ -0,0 +1,8 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.domain.Review; + +public interface ReviewRepository { + + void save(Review review); +} diff --git a/src/main/java/com/gamsa/review/service/QuestionService.java b/src/main/java/com/gamsa/review/service/QuestionService.java new file mode 100644 index 0000000..a9ccda6 --- /dev/null +++ b/src/main/java/com/gamsa/review/service/QuestionService.java @@ -0,0 +1,21 @@ +package com.gamsa.review.service; + +import com.gamsa.review.dto.QuestionResponse; +import com.gamsa.review.repository.QuestionRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class QuestionService { + + private final QuestionRepository questionRepository; + + public List findAllResponse() { + return questionRepository.findAllQuestion().stream() + .map(QuestionResponse::from) + .toList(); + } + +} diff --git a/src/main/java/com/gamsa/review/service/ReviewService.java b/src/main/java/com/gamsa/review/service/ReviewService.java new file mode 100644 index 0000000..34a50c1 --- /dev/null +++ b/src/main/java/com/gamsa/review/service/ReviewService.java @@ -0,0 +1,71 @@ +package com.gamsa.review.service; + +import com.gamsa.activity.domain.Activity; +import com.gamsa.avatar.domain.Avatar; +import com.gamsa.history.domain.History; +import com.gamsa.history.repository.HistoryRepository; +import com.gamsa.review.domain.Answer; +import com.gamsa.review.domain.Question; +import com.gamsa.review.domain.Review; +import com.gamsa.review.dto.ReviewSaveRequest; +import com.gamsa.review.repository.AnswerRepository; +import com.gamsa.review.repository.QuestionRepository; +import com.gamsa.review.repository.ReviewRepository; +import com.gamsa.user.repository.UserRepository; +import java.util.List; +import java.util.NoSuchElementException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@RequiredArgsConstructor +@Service +public class ReviewService { + + private final UserRepository userRepository; + private final QuestionRepository questionRepository; + private final ReviewRepository reviewRepository; + private final AnswerRepository answerRepository; + private final HistoryRepository historyRepository; + + @Transactional + public void saveReview(Long userId, ReviewSaveRequest saveRequest) { + + userRepository.findById(userId) + .orElseThrow((() -> new NoSuchElementException("존재하지 않는 계정"))); + + History history = historyRepository.findById(saveRequest.getHistoryId()) + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 기록")); + + Avatar avatar = history.getAvatar(); + Activity activity = history.getActivity(); + + // 리뷰 여부 체크 + history.changeReviewed(true); + historyRepository.save(history); + + // save review + Review review = Review.builder() + .avatar(avatar) + .history(history) + .institute(activity.getInstitute()) + .build(); + + reviewRepository.save(review); + + // save answers + List answers = saveRequest.getAnswers().stream() + .map(answer -> { + Question question = questionRepository.findById(answer.getQuestionId()) + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 질문")); + + return Answer.builder() + .question(question) + .score(answer.getScore()) + .build(); + }) + .toList(); + + answers.forEach(answerRepository::save); + } +} From 45a61feed3401739f2b6d9ba60d67943f41ac3e5 Mon Sep 17 00:00:00 2001 From: Awhn <69659322+Awhn@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:19:50 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20Review=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20(#67)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gamsa/activity/domain/Activity.java | 6 +- .../activity/dto/ActivityDetailResponse.java | 52 +++++---- .../activity/service/ActivityService.java | 35 ++++-- .../history/dto/HistoryFindSliceResponse.java | 8 +- .../gamsa/history/service/HistoryService.java | 33 ++++-- .../java/com/gamsa/review/domain/Answer.java | 4 +- .../java/com/gamsa/review/domain/Review.java | 12 +- .../com/gamsa/review/dto/ReviewResponse.java | 21 ++++ .../gamsa/review/entity/AnswerJpaEntity.java | 37 ++++-- .../review/entity/QuestionJpaEntity.java | 26 ++--- .../gamsa/review/entity/ReviewJpaEntity.java | 51 +++++++-- .../repository/AnswerJpaRepository.java | 9 ++ .../repository/QuestionJpaRepository.java | 4 + .../repository/ReviewJpaRepository.java | 11 ++ .../review/repository/ReviewRepository.java | 4 + .../repository/ReviewRepositoryImpl.java | 26 +++++ .../gamsa/review/service/ReviewService.java | 52 ++++++--- .../history/service/HitstoryServiceTest.java | 42 ++++--- .../review/stub/StubAnswerRepository.java | 12 ++ .../review/stub/StubQuestionRepository.java | 34 ++++++ .../review/stub/StubReviewRepository.java | 108 ++++++++++++++++++ 21 files changed, 474 insertions(+), 113 deletions(-) create mode 100644 src/main/java/com/gamsa/review/dto/ReviewResponse.java create mode 100644 src/main/java/com/gamsa/review/repository/AnswerJpaRepository.java create mode 100644 src/main/java/com/gamsa/review/repository/ReviewJpaRepository.java create mode 100644 src/main/java/com/gamsa/review/repository/ReviewRepositoryImpl.java create mode 100644 src/test/java/com/gamsa/review/stub/StubAnswerRepository.java create mode 100644 src/test/java/com/gamsa/review/stub/StubQuestionRepository.java create mode 100644 src/test/java/com/gamsa/review/stub/StubReviewRepository.java diff --git a/src/main/java/com/gamsa/activity/domain/Activity.java b/src/main/java/com/gamsa/activity/domain/Activity.java index 96afa9f..34f3b96 100644 --- a/src/main/java/com/gamsa/activity/domain/Activity.java +++ b/src/main/java/com/gamsa/activity/domain/Activity.java @@ -1,10 +1,12 @@ package com.gamsa.activity.domain; import com.gamsa.activity.constant.Category; -import java.time.LocalDateTime; import lombok.Builder; import lombok.Getter; +import java.math.BigDecimal; +import java.time.LocalDateTime; + @Getter @Builder public class Activity { @@ -23,6 +25,8 @@ public class Activity { private boolean adultPossible; private boolean teenPossible; private boolean groupPossible; + private BigDecimal latitude; + private BigDecimal longitude; private int actWeek; private String actManager; private String actPhone; diff --git a/src/main/java/com/gamsa/activity/dto/ActivityDetailResponse.java b/src/main/java/com/gamsa/activity/dto/ActivityDetailResponse.java index 9e50ce7..ac421ac 100644 --- a/src/main/java/com/gamsa/activity/dto/ActivityDetailResponse.java +++ b/src/main/java/com/gamsa/activity/dto/ActivityDetailResponse.java @@ -3,11 +3,15 @@ import com.gamsa.activity.constant.Category; import com.gamsa.activity.domain.Activity; -import java.time.LocalDateTime; +import com.gamsa.review.dto.QuestionResponse; import lombok.Builder; import lombok.Getter; import lombok.RequiredArgsConstructor; +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.Map; + @Getter @Builder @RequiredArgsConstructor @@ -35,29 +39,29 @@ public class ActivityDetailResponse { private final InstituteDetailResponse institute; private final DistrictDetailResponse sidoGungu; - public static ActivityDetailResponse from(Activity activity) { + public static ActivityDetailResponse from(Activity activity, Map scores) { return ActivityDetailResponse.builder() - .actId(activity.getActId()) - .actTitle(activity.getActTitle()) - .actLocation(activity.getActLocation()) - .description(activity.getDescription()) - .noticeStartDate(activity.getNoticeStartDate()) - .noticeEndDate(activity.getNoticeEndDate()) - .actStartDate(activity.getActStartDate()) - .actEndDate(activity.getActEndDate()) - .actStartTime(activity.getActStartTime()) - .actEndTime(activity.getActEndTime()) - .recruitTotalNum(activity.getRecruitTotalNum()) - .adultPossible(activity.isAdultPossible()) - .teenPossible(activity.isTeenPossible()) - .groupPossible(activity.isGroupPossible()) - .actWeek(activity.getActWeek()) - .actManager(activity.getActManager()) - .actPhone(activity.getActPhone()) - .url(activity.getUrl()) - .category(activity.getCategory()) - .institute(InstituteDetailResponse.from(activity.getInstitute())) - .sidoGungu(DistrictDetailResponse.from(activity.getSidoGungu())) - .build(); + .actId(activity.getActId()) + .actTitle(activity.getActTitle()) + .actLocation(activity.getActLocation()) + .description(activity.getDescription()) + .noticeStartDate(activity.getNoticeStartDate()) + .noticeEndDate(activity.getNoticeEndDate()) + .actStartDate(activity.getActStartDate()) + .actEndDate(activity.getActEndDate()) + .actStartTime(activity.getActStartTime()) + .actEndTime(activity.getActEndTime()) + .recruitTotalNum(activity.getRecruitTotalNum()) + .adultPossible(activity.isAdultPossible()) + .teenPossible(activity.isTeenPossible()) + .groupPossible(activity.isGroupPossible()) + .actWeek(activity.getActWeek()) + .actManager(activity.getActManager()) + .actPhone(activity.getActPhone()) + .url(activity.getUrl()) + .category(activity.getCategory()) + .institute(InstituteDetailResponse.from(activity.getInstitute(), scores)) + .sidoGungu(DistrictDetailResponse.from(activity.getSidoGungu())) + .build(); } } diff --git a/src/main/java/com/gamsa/activity/service/ActivityService.java b/src/main/java/com/gamsa/activity/service/ActivityService.java index 88b1687..5d197b6 100644 --- a/src/main/java/com/gamsa/activity/service/ActivityService.java +++ b/src/main/java/com/gamsa/activity/service/ActivityService.java @@ -12,11 +12,18 @@ import com.gamsa.activity.repository.ActivityRepository; import com.gamsa.activity.repository.DistrictRepository; import com.gamsa.activity.repository.InstituteRepository; +import com.gamsa.review.dto.QuestionResponse; +import com.gamsa.review.service.QuestionService; +import com.gamsa.review.service.ReviewService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Map; + @RequiredArgsConstructor @Service public class ActivityService { @@ -24,33 +31,45 @@ public class ActivityService { private final ActivityRepository activityRepository; private final InstituteRepository instituteRepository; private final DistrictRepository districtRepository; + private final QuestionService questionService; + private final ReviewService reviewService; public void save(ActivitySaveRequest saveRequest) { // 중복 여부 확인 activityRepository.findById(saveRequest.getActId()) - .ifPresent(activity -> { - throw new ActivityException(ActivityErrorCode.ACTIVITY_ALREADY_EXISTS); - }); + .ifPresent(activity -> { + throw new ActivityException(ActivityErrorCode.ACTIVITY_ALREADY_EXISTS); + }); // 기관 존재 확인 Institute institute = instituteRepository.findById(saveRequest.getInstituteId()) - .orElseThrow(() -> new ActivityException(ActivityErrorCode.INSTITUTE_NOT_EXISTS)); + .orElseThrow(() -> new ActivityException(ActivityErrorCode.INSTITUTE_NOT_EXISTS)); // 시도, 군구 존재 확인 District sidoGungu = districtRepository.findBySidoGunguCode(saveRequest.getSidoGunguCode()) - .orElseThrow(() -> new ActivityException(ActivityErrorCode.DISTRICT_NOT_EXISTS)); + .orElseThrow(() -> new ActivityException(ActivityErrorCode.DISTRICT_NOT_EXISTS)); activityRepository.save(saveRequest.toModel(institute, sidoGungu)); } public Slice findSlice(ActivityFilterRequest request, - Pageable pageable) { + Pageable pageable) { Slice activities = activityRepository.findSlice(request, pageable); return activities.map(ActivityFindSliceResponse::from); } public ActivityDetailResponse findById(Long activityId) { Activity activity = activityRepository.findById(activityId) - .orElseThrow(() -> new ActivityException(ActivityErrorCode.ACTIVITY_NOT_EXISTS)); - return ActivityDetailResponse.from(activity); + .orElseThrow(() -> new ActivityException(ActivityErrorCode.ACTIVITY_NOT_EXISTS)); + + + Map scores = new HashMap<>(); + long instituteId = activity.getInstitute().getInstituteId(); + + questionService.findAllResponse().forEach(question -> { + BigDecimal score = reviewService.getAverageScore(instituteId, question.getQuestionId()); + scores.put(question, score); + }); + + return ActivityDetailResponse.from(activity, scores); } } diff --git a/src/main/java/com/gamsa/history/dto/HistoryFindSliceResponse.java b/src/main/java/com/gamsa/history/dto/HistoryFindSliceResponse.java index c01fb39..9b3c9de 100644 --- a/src/main/java/com/gamsa/history/dto/HistoryFindSliceResponse.java +++ b/src/main/java/com/gamsa/history/dto/HistoryFindSliceResponse.java @@ -3,10 +3,14 @@ import com.gamsa.activity.dto.ActivityDetailResponse; import com.gamsa.history.constant.ActivityStatus; import com.gamsa.history.domain.History; +import com.gamsa.review.dto.QuestionResponse; import lombok.Builder; import lombok.Getter; import lombok.RequiredArgsConstructor; +import java.math.BigDecimal; +import java.util.Map; + @Getter @Builder @RequiredArgsConstructor @@ -18,13 +22,13 @@ public class HistoryFindSliceResponse { private final ActivityStatus activityStatus; private final boolean reviewed; - public static HistoryFindSliceResponse from(final History history) { + public static HistoryFindSliceResponse from(History history, Map scores) { return HistoryFindSliceResponse.builder() .historyId(history.getHistoryId()) .avatarId(history.getAvatar().getAvatarId()) .activityStatus(history.getActivityStatus()) .reviewed(history.isReviewed()) - .activity(ActivityDetailResponse.from(history.getActivity())) + .activity(ActivityDetailResponse.from(history.getActivity(), scores)) .build(); } } diff --git a/src/main/java/com/gamsa/history/service/HistoryService.java b/src/main/java/com/gamsa/history/service/HistoryService.java index 6807550..2c0bcf7 100644 --- a/src/main/java/com/gamsa/history/service/HistoryService.java +++ b/src/main/java/com/gamsa/history/service/HistoryService.java @@ -8,39 +8,58 @@ import com.gamsa.history.dto.HistoryFindSliceResponse; import com.gamsa.history.dto.HistorySaveRequest; import com.gamsa.history.repository.HistoryRepository; -import java.time.LocalDateTime; -import java.util.NoSuchElementException; +import com.gamsa.review.dto.QuestionResponse; +import com.gamsa.review.service.QuestionService; +import com.gamsa.review.service.ReviewService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.Map; +import java.util.NoSuchElementException; + @RequiredArgsConstructor @Service public class HistoryService { private final HistoryRepository historyRepository; private final AvatarRepository avatarRepository; private final ActivityRepository activityRepository; + private final QuestionService questionService; + private final ReviewService reviewService; public void save(HistorySaveRequest saveRequest, Long userId) { Avatar avatar = avatarRepository.findByUserId(userId) - .orElseThrow(NoSuchElementException::new); + .orElseThrow(NoSuchElementException::new); Activity activity = activityRepository.findById(saveRequest.getActId()) - .orElseThrow(NoSuchElementException::new); + .orElseThrow(NoSuchElementException::new); History history = saveRequest.toModel(avatar, activity); historyRepository.save(history); } public Slice findSliceByAvatarId(Long userId, Pageable pageable) { Avatar avatar = avatarRepository.findByUserId(userId) - .orElseThrow(NoSuchElementException::new); + .orElseThrow(NoSuchElementException::new); Slice histories = historyRepository - .findSliceByAvatarId(avatar.getAvatarId(), pageable); + .findSliceByAvatarId(avatar.getAvatarId(), pageable); histories.forEach(this::checkDate); - return histories.map(HistoryFindSliceResponse::from); + return histories.map(history -> { + Map scores = new HashMap<>(); + long instituteId = history.getActivity().getInstitute().getInstituteId(); + + questionService.findAllResponse().forEach(question -> { + BigDecimal score = reviewService.getAverageScore(instituteId, question.getQuestionId()); + scores.put(question, score); + }); + + return HistoryFindSliceResponse.from(history, scores); + }); } public void delete(long historyId) { diff --git a/src/main/java/com/gamsa/review/domain/Answer.java b/src/main/java/com/gamsa/review/domain/Answer.java index a1c07e9..3c69b71 100644 --- a/src/main/java/com/gamsa/review/domain/Answer.java +++ b/src/main/java/com/gamsa/review/domain/Answer.java @@ -6,7 +6,7 @@ @Getter @Builder public class Answer { - - private Question question; + private Long answerId; private int score; + private Question question; } diff --git a/src/main/java/com/gamsa/review/domain/Review.java b/src/main/java/com/gamsa/review/domain/Review.java index 3495742..ba82f6f 100644 --- a/src/main/java/com/gamsa/review/domain/Review.java +++ b/src/main/java/com/gamsa/review/domain/Review.java @@ -1,12 +1,22 @@ package com.gamsa.review.domain; -import java.util.List; +import com.gamsa.activity.domain.Activity; +import com.gamsa.activity.domain.Institute; +import com.gamsa.avatar.domain.Avatar; +import com.gamsa.history.domain.History; import lombok.Builder; import lombok.Getter; +import java.util.List; + @Getter @Builder public class Review { + private Long reviewId; private List answers; + private Avatar avatar; + private Activity activity; + private Institute institute; + private History history; } diff --git a/src/main/java/com/gamsa/review/dto/ReviewResponse.java b/src/main/java/com/gamsa/review/dto/ReviewResponse.java new file mode 100644 index 0000000..00dabd2 --- /dev/null +++ b/src/main/java/com/gamsa/review/dto/ReviewResponse.java @@ -0,0 +1,21 @@ +package com.gamsa.review.dto; + +import com.gamsa.review.domain.Question; +import lombok.Builder; +import lombok.Getter; + +import java.math.BigDecimal; + +@Getter +@Builder +public class ReviewResponse { + private QuestionResponse question; + private BigDecimal score; + + public static ReviewResponse from(Question question, BigDecimal score) { + return ReviewResponse.builder() + .question(QuestionResponse.from(question)) + .score(score) + .build(); + } +} diff --git a/src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java b/src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java index 49ddf84..bf9da5f 100644 --- a/src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java +++ b/src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java @@ -1,12 +1,8 @@ package com.gamsa.review.entity; -import jakarta.persistence.Entity; -import jakarta.persistence.Table; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import com.gamsa.review.domain.Answer; +import jakarta.persistence.*; +import lombok.*; @Getter @Builder @@ -16,4 +12,31 @@ @Table(name = "answer") public class AnswerJpaEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "answer_id") + private Long answerId; + + @Column + private int score; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "question_id") + private QuestionJpaEntity question; + + public static AnswerJpaEntity from(Answer answer) { + return AnswerJpaEntity.builder() + .answerId(answer.getAnswerId()) + .score(answer.getScore()) + .question(QuestionJpaEntity.from(answer.getQuestion())) + .build(); + } + + public Answer toModel() { + return Answer.builder() + .answerId(answerId) + .score(score) + .question(question.toModel()) + .build(); + } } diff --git a/src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java b/src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java index b14f56b..6f0db0d 100644 --- a/src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java +++ b/src/main/java/com/gamsa/review/entity/QuestionJpaEntity.java @@ -1,17 +1,8 @@ package com.gamsa.review.entity; import com.gamsa.review.domain.Question; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import jakarta.persistence.*; +import lombok.*; @Getter @Builder @@ -29,10 +20,17 @@ public class QuestionJpaEntity { @Column(name = "content", length = 255) private String content; + public static QuestionJpaEntity from(Question question) { + return QuestionJpaEntity.builder() + .questionId(question.getQuestionId()) + .content(question.getContent()) + .build(); + } + public Question toModel() { return Question.builder() - .questionId(questionId) - .content(content) - .build(); + .questionId(questionId) + .content(content) + .build(); } } diff --git a/src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java b/src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java index 3f9f074..18d0896 100644 --- a/src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java +++ b/src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java @@ -1,18 +1,13 @@ package com.gamsa.review.entity; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; +import com.gamsa.activity.entity.ActivityJpaEntity; +import com.gamsa.activity.entity.InstituteJpaEntity; +import com.gamsa.history.entity.HistoryJpaEntity; +import com.gamsa.review.domain.Review; +import jakarta.persistence.*; +import lombok.*; + import java.util.List; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; @Getter @Builder @@ -30,4 +25,36 @@ public class ReviewJpaEntity { @OneToMany private List answers; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "activity_id") + private ActivityJpaEntity activity; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "institute_id") + private InstituteJpaEntity institute; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "history_id") + private HistoryJpaEntity history; + + public static ReviewJpaEntity from(Review review) { + return ReviewJpaEntity.builder() + .reviewId(review.getReviewId()) + .answers(review.getAnswers().stream().map(answer -> AnswerJpaEntity.from(answer)).toList()) + .activity(ActivityJpaEntity.from(review.getActivity())) + .institute(InstituteJpaEntity.from(review.getInstitute())) + .history(HistoryJpaEntity.from(review.getHistory())) + .build(); + } + + public Review toModel() { + return Review.builder() + .reviewId(reviewId) + .answers(answers.stream().map(AnswerJpaEntity::toModel).toList()) + .activity(activity.toModel()) + .institute(institute.toModel()) + .history(history.toModel()) + .build(); + + } } diff --git a/src/main/java/com/gamsa/review/repository/AnswerJpaRepository.java b/src/main/java/com/gamsa/review/repository/AnswerJpaRepository.java new file mode 100644 index 0000000..d6ceb00 --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/AnswerJpaRepository.java @@ -0,0 +1,9 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.entity.AnswerJpaEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AnswerJpaRepository extends JpaRepository { + +} + diff --git a/src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java b/src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java index 761400d..b4ed04a 100644 --- a/src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java +++ b/src/main/java/com/gamsa/review/repository/QuestionJpaRepository.java @@ -1,8 +1,12 @@ package com.gamsa.review.repository; +import com.gamsa.review.domain.Answer; import com.gamsa.review.entity.QuestionJpaEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +@Repository public interface QuestionJpaRepository extends JpaRepository { + public void save(Answer answer); } diff --git a/src/main/java/com/gamsa/review/repository/ReviewJpaRepository.java b/src/main/java/com/gamsa/review/repository/ReviewJpaRepository.java new file mode 100644 index 0000000..a3b0bb2 --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/ReviewJpaRepository.java @@ -0,0 +1,11 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.entity.ReviewJpaEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface ReviewJpaRepository extends JpaRepository { + List findByInstituteInstituteIdAndAnswersQuestionQuestionId(Long instituteId, Long questionId); + +} diff --git a/src/main/java/com/gamsa/review/repository/ReviewRepository.java b/src/main/java/com/gamsa/review/repository/ReviewRepository.java index dfe55c2..f3e2c88 100644 --- a/src/main/java/com/gamsa/review/repository/ReviewRepository.java +++ b/src/main/java/com/gamsa/review/repository/ReviewRepository.java @@ -2,7 +2,11 @@ import com.gamsa.review.domain.Review; +import java.util.List; + public interface ReviewRepository { void save(Review review); + + List findReviews(Long instituteId, Long questionId); } diff --git a/src/main/java/com/gamsa/review/repository/ReviewRepositoryImpl.java b/src/main/java/com/gamsa/review/repository/ReviewRepositoryImpl.java new file mode 100644 index 0000000..734552e --- /dev/null +++ b/src/main/java/com/gamsa/review/repository/ReviewRepositoryImpl.java @@ -0,0 +1,26 @@ +package com.gamsa.review.repository; + +import com.gamsa.review.domain.Review; +import com.gamsa.review.entity.ReviewJpaEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@RequiredArgsConstructor +@Repository +public class ReviewRepositoryImpl implements ReviewRepository { + private final ReviewJpaRepository reviewJpaRepository; + + @Override + public void save(Review review) { + reviewJpaRepository.save(ReviewJpaEntity.from(review)); + } + + @Override + public List findReviews(Long instituteId, Long questionId) { + return reviewJpaRepository.findByInstituteInstituteIdAndAnswersQuestionQuestionId(instituteId, questionId).stream().map(ReviewJpaEntity::toModel).toList(); + } + + +} diff --git a/src/main/java/com/gamsa/review/service/ReviewService.java b/src/main/java/com/gamsa/review/service/ReviewService.java index 34a50c1..c8e6501 100644 --- a/src/main/java/com/gamsa/review/service/ReviewService.java +++ b/src/main/java/com/gamsa/review/service/ReviewService.java @@ -12,12 +12,15 @@ import com.gamsa.review.repository.QuestionRepository; import com.gamsa.review.repository.ReviewRepository; import com.gamsa.user.repository.UserRepository; -import java.util.List; -import java.util.NoSuchElementException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.math.BigDecimal; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.OptionalDouble; + @RequiredArgsConstructor @Service public class ReviewService { @@ -32,10 +35,10 @@ public class ReviewService { public void saveReview(Long userId, ReviewSaveRequest saveRequest) { userRepository.findById(userId) - .orElseThrow((() -> new NoSuchElementException("존재하지 않는 계정"))); + .orElseThrow((() -> new NoSuchElementException("존재하지 않는 계정"))); History history = historyRepository.findById(saveRequest.getHistoryId()) - .orElseThrow(() -> new NoSuchElementException("존재하지 않는 기록")); + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 기록")); Avatar avatar = history.getAvatar(); Activity activity = history.getActivity(); @@ -46,26 +49,41 @@ public void saveReview(Long userId, ReviewSaveRequest saveRequest) { // save review Review review = Review.builder() - .avatar(avatar) - .history(history) - .institute(activity.getInstitute()) - .build(); + .avatar(avatar) + .history(history) + .institute(activity.getInstitute()) + .build(); reviewRepository.save(review); // save answers List answers = saveRequest.getAnswers().stream() - .map(answer -> { - Question question = questionRepository.findById(answer.getQuestionId()) - .orElseThrow(() -> new NoSuchElementException("존재하지 않는 질문")); + .map(answer -> { + Question question = questionRepository.findById(answer.getQuestionId()) + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 질문")); - return Answer.builder() - .question(question) - .score(answer.getScore()) - .build(); - }) - .toList(); + return Answer.builder() + .question(question) + .score(answer.getScore()) + .build(); + }) + .toList(); answers.forEach(answerRepository::save); } + + public BigDecimal getAverageScore(long instituteId, long questionId) { + OptionalDouble averageScore = reviewRepository.findReviews(instituteId, questionId) + .stream() + .flatMap(review -> review.getAnswers().stream()) + .filter(answer -> answer.getQuestion().getQuestionId() == questionId) + .mapToInt(Answer::getScore) + .average(); + + if (averageScore.isPresent()) { + return BigDecimal.valueOf(averageScore.getAsDouble()); + } else { + return BigDecimal.ZERO; + } + } } diff --git a/src/test/java/com/gamsa/history/service/HitstoryServiceTest.java b/src/test/java/com/gamsa/history/service/HitstoryServiceTest.java index 9344843..bd7ab2d 100644 --- a/src/test/java/com/gamsa/history/service/HitstoryServiceTest.java +++ b/src/test/java/com/gamsa/history/service/HitstoryServiceTest.java @@ -1,37 +1,51 @@ package com.gamsa.history.service; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - import com.gamsa.activity.stub.StubExistsActivityRepository; import com.gamsa.avatar.stub.StubExistsAvatarRepository; import com.gamsa.history.dto.HistorySaveRequest; import com.gamsa.history.stub.StubHistoryRepository; +import com.gamsa.review.service.QuestionService; +import com.gamsa.review.service.ReviewService; +import com.gamsa.review.stub.StubAnswerRepository; +import com.gamsa.review.stub.StubQuestionRepository; +import com.gamsa.review.stub.StubReviewRepository; +import com.gamsa.user.stub.StubExistsUserRepository; import org.junit.jupiter.api.Test; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + public class HitstoryServiceTest { HistorySaveRequest historySaveRequest = HistorySaveRequest.builder() .actId(1L) .build(); + ReviewService reviewService = new ReviewService( + new StubExistsUserRepository(), + new StubQuestionRepository(), + new StubReviewRepository(), + new StubAnswerRepository(), + new StubHistoryRepository() + ); + + QuestionService questionService = new QuestionService( + new StubQuestionRepository() + ); + + HistoryService historyService = new HistoryService(new StubHistoryRepository(), + new StubExistsAvatarRepository(), new StubExistsActivityRepository(), questionService, reviewService); + @Test void 새로운_기록_저장() { - //given - HistoryService historyService = new HistoryService(new StubHistoryRepository(), - new StubExistsAvatarRepository(), new StubExistsActivityRepository()); - //when & then assertDoesNotThrow(() -> historyService.save(historySaveRequest, 1L)); } @Test void 유저_기록_찾기() { - //given - HistoryService historyService = new HistoryService(new StubHistoryRepository(), - new StubExistsAvatarRepository(), new StubExistsActivityRepository()); //when & then Pageable pageable = PageRequest.of(0, 10); @@ -40,20 +54,12 @@ public class HitstoryServiceTest { @Test void 기록_삭제() { - //given - HistoryService historyService = new HistoryService(new StubHistoryRepository(), - new StubExistsAvatarRepository(), new StubExistsActivityRepository()); - //when & then assertDoesNotThrow(() -> historyService.delete(1L)); } @Test void 리뷰_상태_업데이트() { - //given - HistoryService historyService = new HistoryService(new StubHistoryRepository(), - new StubExistsAvatarRepository(), new StubExistsActivityRepository()); - //when & then assertDoesNotThrow(() -> historyService.updateReviewed(1L, true)); } diff --git a/src/test/java/com/gamsa/review/stub/StubAnswerRepository.java b/src/test/java/com/gamsa/review/stub/StubAnswerRepository.java new file mode 100644 index 0000000..242863d --- /dev/null +++ b/src/test/java/com/gamsa/review/stub/StubAnswerRepository.java @@ -0,0 +1,12 @@ +package com.gamsa.review.stub; + +import com.gamsa.review.domain.Answer; +import com.gamsa.review.repository.AnswerRepository; + +public class StubAnswerRepository implements AnswerRepository { + + @Override + public void save(Answer answer) { + // do nothing + } +} diff --git a/src/test/java/com/gamsa/review/stub/StubQuestionRepository.java b/src/test/java/com/gamsa/review/stub/StubQuestionRepository.java new file mode 100644 index 0000000..dc1489b --- /dev/null +++ b/src/test/java/com/gamsa/review/stub/StubQuestionRepository.java @@ -0,0 +1,34 @@ +package com.gamsa.review.stub; + +import com.gamsa.review.domain.Question; +import com.gamsa.review.repository.QuestionRepository; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class StubQuestionRepository implements QuestionRepository { + + Question question1 = Question.builder() + .questionId(1) + .content("질문 테스트1") + .build(); + + Question question2 = Question.builder() + .questionId(2) + .content("질문 테스트2") + .build(); + + List questions = Arrays.asList(question1, question2); + + @Override + public List findAllQuestion() { + return questions; + } + + @Override + public Optional findById(Integer id) { + return Optional.of(question1); + } + +} diff --git a/src/test/java/com/gamsa/review/stub/StubReviewRepository.java b/src/test/java/com/gamsa/review/stub/StubReviewRepository.java new file mode 100644 index 0000000..464a340 --- /dev/null +++ b/src/test/java/com/gamsa/review/stub/StubReviewRepository.java @@ -0,0 +1,108 @@ +package com.gamsa.review.stub; + +import com.gamsa.activity.domain.Activity; +import com.gamsa.activity.domain.District; +import com.gamsa.activity.domain.Institute; +import com.gamsa.avatar.constant.AgeRange; +import com.gamsa.avatar.constant.Experienced; +import com.gamsa.avatar.domain.Avatar; +import com.gamsa.history.constant.ActivityStatus; +import com.gamsa.history.domain.History; +import com.gamsa.review.domain.Answer; +import com.gamsa.review.domain.Question; +import com.gamsa.review.domain.Review; +import com.gamsa.review.repository.ReviewRepository; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.List; + +public class StubReviewRepository implements ReviewRepository { + + Question question = Question.builder() + .questionId(1) + .content("질문 테스트1") + .build(); + + Answer answer = Answer.builder() + .answerId(1L) + .question(question) + .score(0) + .build(); + + private final District district = District.builder() + .sidoCode(1234) + .sidoGunguCode(8888) + .sidoName("서울특별시") + .gunguName("강남구") + .sido(false) + .build(); + + private final Institute institute = Institute.builder() + .instituteId(1L) + .name("도서관") + .location("서울시") + .latitude(new BigDecimal("123456789.12341234")) + .longitude(new BigDecimal("987654321.43214321")) + .phone("010xxxxxxxx") + .build(); + + private final Activity activity = Activity.builder() + .actId(1L) + .actTitle("어린이놀이안전관리 및 놀잇감 청결유지 및 정리") + .actLocation("아이사랑꿈터 서구 5호점") + .description("봉사 내용") + .noticeStartDate(LocalDateTime.of(2024, 9, 10, 0, 0)) + .noticeEndDate(LocalDateTime.of(2024, 12, 7, 0, 0)) + .actStartDate(LocalDateTime.of(2024, 9, 10, 0, 0)) + .actEndDate(LocalDateTime.of(2024, 12, 7, 0, 0)) + .actStartTime(13) + .actEndTime(18) + .recruitTotalNum(1) + .adultPossible(true) + .teenPossible(false) + .groupPossible(false) + .actWeek(0111110) + .actManager("윤순영") + .actPhone("032-577-3026") + .url("https://...") + .institute(institute) + .sidoGungu(district) + .build(); + + Avatar avatar = Avatar.builder() + .avatarId(1L) + .avatarLevel(1L) + .avatarExp(1L) + .nickname("닉네임") + .ageRange(AgeRange.ADULT) + .experienced(Experienced.NOVICE) + .build(); + + History history = History.builder() + .historyId(1L) + .activity(activity) + .avatar(avatar) + .activityStatus(ActivityStatus.APPLIED) + .reviewed(false) + .build(); + + Review review1 = Review.builder() + .reviewId(1L) + .answers(List.of(answer)) + .activity(activity) + .institute(institute) + .history(history) + .build(); + + @Override + public void save(Review review) { + // do nothing + } + + @Override + public List findReviews(Long instituteID, Long questionId) { + return List.of(review1); + + } +}