Skip to content

Commit

Permalink
[#55] feat: 리뷰 기능 구현 중간 커밋
Browse files Browse the repository at this point in the history
  • Loading branch information
5win committed Nov 2, 2024
1 parent 66bf974 commit bd3ef56
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 0 deletions.
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 Question question;
private int score;
}
12 changes: 12 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,12 @@
package com.gamsa.review.domain;

import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class Review {

private List<Answer> answers;
}
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;
}
13 changes: 13 additions & 0 deletions src/main/java/com/gamsa/review/dto/ReviewSaveRequest.java
Original file line number Diff line number Diff line change
@@ -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<AnswerSaveRequest> answers;
}
19 changes: 19 additions & 0 deletions src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java
Original file line number Diff line number Diff line change
@@ -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 {

}
33 changes: 33 additions & 0 deletions src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java
Original file line number Diff line number Diff line change
@@ -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<AnswerJpaEntity> answers;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gamsa.review.repository;

import com.gamsa.review.domain.Answer;

public interface AnswerRepository {

void save(Answer answer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.gamsa.review.domain.Question;
import java.util.List;
import java.util.Optional;

public interface QuestionRepository {

List<Question> findAllQuestion();

Optional<Question> findById(Integer questionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;

Expand All @@ -18,4 +19,10 @@ public List<Question> findAllQuestion() {
.map(QuestionJpaEntity::toModel)
.toList();
}

@Override
public Optional<Question> findById(Integer questionId) {
return questionJpaRepository.findById(questionId)
.map(QuestionJpaEntity::toModel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gamsa.review.repository;

import com.gamsa.review.domain.Review;

public interface ReviewRepository {

void save(Review review);
}
71 changes: 71 additions & 0 deletions src/main/java/com/gamsa/review/service/ReviewService.java
Original file line number Diff line number Diff line change
@@ -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<Answer> 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);
}
}

0 comments on commit bd3ef56

Please sign in to comment.