Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#55] feat: Review 기능 구현 #67

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/com/gamsa/activity/domain/Activity.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand Down
52 changes: 28 additions & 24 deletions src/main/java/com/gamsa/activity/dto/ActivityDetailResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<QuestionResponse, BigDecimal> 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();
}
}
35 changes: 27 additions & 8 deletions src/main/java/com/gamsa/activity/service/ActivityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,64 @@
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 {

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<ActivityFindSliceResponse> findSlice(ActivityFilterRequest request,
Pageable pageable) {
Pageable pageable) {
Slice<Activity> 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<QuestionResponse, BigDecimal> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<QuestionResponse, BigDecimal> 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();
}
}
33 changes: 26 additions & 7 deletions src/main/java/com/gamsa/history/service/HistoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<HistoryFindSliceResponse> findSliceByAvatarId(Long userId, Pageable pageable) {
Avatar avatar = avatarRepository.findByUserId(userId)
.orElseThrow(NoSuchElementException::new);
.orElseThrow(NoSuchElementException::new);

Slice<History> histories = historyRepository
.findSliceByAvatarId(avatar.getAvatarId(), pageable);
.findSliceByAvatarId(avatar.getAvatarId(), pageable);
histories.forEach(this::checkDate);

return histories.map(HistoryFindSliceResponse::from);
return histories.map(history -> {
Map<QuestionResponse, BigDecimal> 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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/gamsa/review/domain/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Getter
@Builder
public class Answer {

private Question question;
private Long answerId;
private int score;
private Question question;
}
12 changes: 11 additions & 1 deletion src/main/java/com/gamsa/review/domain/Review.java
Original file line number Diff line number Diff line change
@@ -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<Answer> answers;
private Avatar avatar;
private Activity activity;
private Institute institute;
private History history;
}
21 changes: 21 additions & 0 deletions src/main/java/com/gamsa/review/dto/ReviewResponse.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
37 changes: 30 additions & 7 deletions src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
}
}
Loading
Loading