Skip to content

Commit

Permalink
Merge pull request #69 from kakao-tech-campus-2nd-step3/develop
Browse files Browse the repository at this point in the history
deploy: 10주차 내용 배포
  • Loading branch information
5win authored Nov 8, 2024
2 parents 4006ec3 + 99e19f5 commit fe8fb13
Show file tree
Hide file tree
Showing 31 changed files with 802 additions and 61 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/gamsa/activity/domain/Activity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.gamsa.activity.domain;

import com.gamsa.activity.constant.Category;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Getter
@Builder
public class Activity {
Expand All @@ -24,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
23 changes: 23 additions & 0 deletions src/main/java/com/gamsa/review/controller/QuestionController.java
Original file line number Diff line number Diff line change
@@ -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());
}


}
31 changes: 31 additions & 0 deletions src/main/java/com/gamsa/review/controller/ReviewController.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/gamsa/review/domain/Answer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gamsa.review.domain;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class Answer {
private Long answerId;
private int score;
private Question question;
}
12 changes: 12 additions & 0 deletions src/main/java/com/gamsa/review/domain/Question.java
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions src/main/java/com/gamsa/review/domain/Review.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gamsa.review.domain;

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;
}
12 changes: 12 additions & 0 deletions src/main/java/com/gamsa/review/dto/AnswerSaveRequest.java
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit fe8fb13

Please sign in to comment.