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

[Feat] 내가 푼 문제(review) 기능 추가 #29

Merged
merged 8 commits into from
Mar 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import com.beotkkot.qtudy.dto.request.quiz.GenerateQuizRequestDto;
import com.beotkkot.qtudy.dto.request.quiz.GradeQuizRequestDto;
import com.beotkkot.qtudy.dto.response.posts.PostsResponseDto;
import com.beotkkot.qtudy.dto.response.quiz.GetPostQuizResponseDto;
import com.beotkkot.qtudy.dto.response.quiz.GetReviewResponseDto;
import com.beotkkot.qtudy.dto.response.quiz.QuizGradeResponseDto;
import com.beotkkot.qtudy.dto.response.quiz.ReviewResponseDto;
import com.beotkkot.qtudy.service.auth.AuthService;
import com.beotkkot.qtudy.service.quiz.QuizService;
import com.beotkkot.qtudy.service.quiz.ReviewService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,6 +23,8 @@
public class QuizController {

private final QuizService quizService;
private final AuthService authService;
private final ReviewService reviewService;

// 퀴즈 생성 및 저장
@PostMapping("/quiz")
Expand All @@ -29,14 +36,14 @@ public ResponseEntity<?> generateQuiz(@RequestBody GenerateQuizRequestDto dto) {
}
}

// 태그별 퀴즈 출력
// 태그별 랜덤 10개 퀴즈 출력
@GetMapping("/quiz/tag-quiz")
public ResponseEntity<? super GetPostQuizResponseDto> getTagQuiz(@RequestParam String tagName) {
ResponseEntity<? super GetPostQuizResponseDto> response = quizService.getTagQuiz(tagName);
return response;
}

// 게시글 별 퀴즈 출력
// 게시글 별 생성된 퀴즈 출력
@GetMapping("/quiz/post-quiz")
public ResponseEntity<? super GetPostQuizResponseDto> getPostQuiz(@RequestParam Long postId) {
ResponseEntity<? super GetPostQuizResponseDto> response = quizService.getPostQuiz(postId);
Expand All @@ -47,8 +54,48 @@ public ResponseEntity<? super GetPostQuizResponseDto> getPostQuiz(@RequestParam
// 정답 채점
@PostMapping("/quiz/grade")
public ResponseEntity<? super QuizGradeResponseDto> gradeQuiz(@RequestHeader(value="Authorization") String token, @RequestBody GradeQuizRequestDto dto) {
// uuid 받아서 사용자 조회 필요 -> 오답노트 엔티티에 저장해야함
ResponseEntity<? super QuizGradeResponseDto> response = quizService.gradeQuiz(dto);
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super QuizGradeResponseDto> response = quizService.gradeQuiz(dto, kakao_uid);
return response;
}

// 내가 푼 퀴즈 전체 조회
@GetMapping("/my/quiz/all")
public ResponseEntity<? super ReviewResponseDto> getMyQuizList(@RequestHeader(value="Authorization") String token, @RequestParam int page) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super ReviewResponseDto> response = reviewService.getMyQuizList(kakao_uid, page);
return response;
}

// 내가 푼 퀴즈 상세 조회
@GetMapping("/my/quiz")
public ResponseEntity<? super GetReviewResponseDto> getMyQuiz(@RequestHeader(value="Authorization") String token, @RequestParam String reviewId) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super GetReviewResponseDto> response = reviewService.getMyQuiz(kakao_uid, reviewId);
return response;
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/beotkkot/qtudy/domain/quiz/Review.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.beotkkot.qtudy.domain.quiz;

import jakarta.persistence.*;
import lombok.*;

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Review {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private Long userId;

@Column(nullable = false)
private Long postId;

@Column(nullable = false)
private Long quizId;

@Column(nullable = false)
private int userAnswer;

@Column(nullable = false)
private String answer;

@Column(nullable = false)
private boolean correct;

private String explanation;

private Long categoryId;

private String type;

private String tags;

private int score;

private String createdAt;

@Column(nullable = false, columnDefinition = "TEXT")
private String reviewId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.beotkkot.qtudy.dto.object;

import com.beotkkot.qtudy.domain.quiz.Quiz;
import com.beotkkot.qtudy.domain.quiz.Review;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ReviewDetailListItem {
private Long quizId;
private List<String> tags;
private String question;
private String answer;
private int userAnswer;
private boolean correct;
private List<String> options;
private String explanation;
private String createdAt;

public static ReviewDetailListItem of(Quiz quiz, Review review) {
List<String> tags = Arrays.asList(quiz.getTags().split("\\s*,\\s*"));
List<String> options = Arrays.asList(quiz.getOptions().split("\\s*,\\s*"));

return ReviewDetailListItem.builder()
.quizId(quiz.getQuizId())
.tags(tags)
.question(quiz.getQuestion())
.answer(quiz.getAnswer())
.userAnswer(review.getUserAnswer())
.correct(review.isCorrect())
.options(options)
.explanation(quiz.getExplanation())
.createdAt(review.getCreatedAt())
.build();
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/beotkkot/qtudy/dto/object/ReviewListItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.beotkkot.qtudy.dto.object;

import com.beotkkot.qtudy.domain.quiz.Review;
import com.beotkkot.qtudy.domain.user.Users;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ReviewListItem {
private String userName;
private String userProfile;
private String reviewId;
private int totalScore;
private String type;
private String createdAt;
private Long categoryId;
private List<String> tags;

public static ReviewListItem of(Users user, Review review, int totalScore) {
List<String> tag = Arrays.asList(review.getTags().split("\\s*,\\s*"));
String userName;
String userProfile;
if (review.getType().equals("tag")) {
userName = null;
userProfile = null;
}
else {
userName = user.getName();
userProfile = user.getProfileImageUrl();
}

return ReviewListItem.builder()
.userName(userName)
.userProfile(userProfile)
.reviewId(review.getReviewId())
.totalScore(totalScore)
.type(review.getType())
.createdAt(review.getCreatedAt())
.tags(tag)
.categoryId(review.getCategoryId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class GradeQuizRequestDto {
private String type;
private List<Long> quizIdList;
private List<String> answerList;
private List<Integer> userAnswerList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

@Getter
public class PostsResponseDto extends ResponseDto {
private Long postId;

private PostsResponseDto() {
private PostsResponseDto(Long postId) {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
this.postId = postId;
}

public static ResponseEntity<PostsResponseDto> success() {
PostsResponseDto result = new PostsResponseDto();
public static ResponseEntity<PostsResponseDto> success(Long postId) {
PostsResponseDto result = new PostsResponseDto(postId);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@

@Getter
public class GetPostQuizResponseDto extends ResponseDto {
private String type;
private List<String> answerList;
private List<Long> quizIdList;
private List<QuizListItem> quizList;

public GetPostQuizResponseDto(List<QuizListItem> QuizListItem, List<String> AnswerListItem, List<Long> quizIdList) {
public GetPostQuizResponseDto(List<QuizListItem> QuizListItem, List<String> AnswerListItem, List<Long> quizIdList, String type) {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
this.type = type;
this.answerList = AnswerListItem;
this.quizIdList = quizIdList;
this.quizList = QuizListItem;
}

public static ResponseEntity<GetPostQuizResponseDto> success(List<QuizListItem> QuizListItem, List<String> AnswerListItem, List<Long> quizIdList) {
GetPostQuizResponseDto result = new GetPostQuizResponseDto(QuizListItem, AnswerListItem, quizIdList);
public static ResponseEntity<GetPostQuizResponseDto> success(List<QuizListItem> QuizListItem, List<String> AnswerListItem, List<Long> quizIdList, String type) {
GetPostQuizResponseDto result = new GetPostQuizResponseDto(QuizListItem, AnswerListItem, quizIdList, type);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.beotkkot.qtudy.dto.response.quiz;

import com.beotkkot.qtudy.common.ResponseCode;
import com.beotkkot.qtudy.common.ResponseMessage;
import com.beotkkot.qtudy.domain.posts.Posts;
import com.beotkkot.qtudy.domain.quiz.Quiz;
import com.beotkkot.qtudy.domain.quiz.Review;
import com.beotkkot.qtudy.domain.user.Users;
import com.beotkkot.qtudy.dto.object.QuizListItem;
import com.beotkkot.qtudy.dto.object.ReviewDetailListItem;
import com.beotkkot.qtudy.dto.response.ResponseDto;
import com.beotkkot.qtudy.dto.response.posts.GetPostsResponseDto;
import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Arrays;
import java.util.List;

@Getter
public class GetReviewResponseDto extends ResponseDto {
private int totalScore;
private List<ReviewDetailListItem> reviewList;

public GetReviewResponseDto(List<ReviewDetailListItem> reviewList, int totalScore) {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
this.totalScore = totalScore;
this.reviewList = reviewList;
}

public static ResponseEntity<GetReviewResponseDto> success(List<ReviewDetailListItem> reviewList, int totalScore) {
GetReviewResponseDto result = new GetReviewResponseDto(reviewList, totalScore);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

public static ResponseEntity<ResponseDto> notExistedPost() {
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.beotkkot.qtudy.dto.response.quiz;

import com.beotkkot.qtudy.common.ResponseCode;
import com.beotkkot.qtudy.common.ResponseMessage;
import com.beotkkot.qtudy.dto.object.QuizGradeListItem;
import com.beotkkot.qtudy.dto.object.ReviewListItem;
import com.beotkkot.qtudy.dto.response.ResponseDto;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

@Getter
public class ReviewResponseDto extends ResponseDto{
private int page;
private int totalPages;
private List<ReviewListItem> reviewListItems;

public ReviewResponseDto(List<ReviewListItem> reviewListItems, int page, int totalPages) {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
this.page = page;
this.totalPages = totalPages;
this.reviewListItems = reviewListItems;
}

public static ResponseEntity<ReviewResponseDto> success(List<ReviewListItem> reviewListItems, int page, int totalPages) {
ReviewResponseDto result = new ReviewResponseDto(reviewListItems, page, totalPages);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

public static ResponseEntity<ResponseDto> notExistedPost() {
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface QuizRepository extends JpaRepository<Quiz, Long> {

Quiz findByQuizId(Long quizId);

@Query("SELECT q FROM Quiz q WHERE q.tags LIKE %:tagName% ORDER BY RAND() LIMIT 10")
@Query("SELECT q FROM Quiz q WHERE q.tags LIKE %:tagName% ORDER BY q.quizId ASC, RAND() LIMIT 10")
List<Quiz> findByTagName(@Param("tagName") String tagName);

}
Loading