-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/com/gamsa/review/controller/ReviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/com/gamsa/review/entity/AnswerJpaEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/main/java/com/gamsa/review/entity/ReviewJpaEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/gamsa/review/repository/AnswerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/gamsa/review/repository/ReviewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |