diff --git a/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/Difficulty.kt b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/Difficulty.kt new file mode 100644 index 0000000..099e416 --- /dev/null +++ b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/Difficulty.kt @@ -0,0 +1,76 @@ +package kr.hs.dsm.inq.domain.question.persistence + +import javax.persistence.Column +import javax.persistence.Entity +import javax.persistence.FetchType +import javax.persistence.GeneratedValue +import javax.persistence.GenerationType +import javax.persistence.Id +import javax.persistence.JoinColumn +import javax.persistence.ManyToOne +import javax.persistence.Table + + +@Table(name = "tbl_difficulity") +@Entity +class Difficulty( + + @Id + @Column(columnDefinition = "BIGINT", nullable = false) + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long = 0L, + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "questions_id", columnDefinition = "BIGINT", nullable = false) + var questions: Questions, + + @Column(columnDefinition = "BIGINT", nullable = false) + var veryEasyCount: Int = 0, + + @Column(columnDefinition = "BIGINT", nullable = false) + var easyCount: Int = 0, + + @Column(columnDefinition = "BIGINT", nullable = false) + var normalCount: Int = 0, + + @Column(columnDefinition = "BIGINT", nullable = false) + var hardCount: Int = 0, + + @Column(columnDefinition = "BIGINT", nullable = false) + var veryHardCount: Int = 0, +) { + + var total = veryEasyCount + easyCount + normalCount + hardCount + veryHardCount + + fun getPercentage(difficultyLevel: DifficultyLevel): Int { + return getCount(difficultyLevel) * 100 / total + } + + private fun getCount(difficultyLevel: DifficultyLevel): Int { + return when (difficultyLevel) { + DifficultyLevel.VERY_EASY -> veryEasyCount + DifficultyLevel.EASY -> easyCount + DifficultyLevel.NORMAL -> normalCount + DifficultyLevel.HARD -> hardCount + DifficultyLevel.VERY_HARD -> veryHardCount + } + } + + fun addCount(difficultyLevel: DifficultyLevel) { + when (difficultyLevel) { + DifficultyLevel.VERY_EASY -> veryEasyCount++ + DifficultyLevel.EASY -> easyCount++ + DifficultyLevel.NORMAL -> normalCount++ + DifficultyLevel.HARD -> hardCount++ + DifficultyLevel.VERY_HARD -> veryHardCount + } + } +} + +enum class DifficultyLevel { + VERY_EASY, + EASY, + NORMAL, + HARD, + VERY_HARD +} \ No newline at end of file diff --git a/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/dto/QuestionSetDetailDto.kt b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/dto/QuestionSetDetailDto.kt index 01e6346..b5dfab6 100644 --- a/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/dto/QuestionSetDetailDto.kt +++ b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/dto/QuestionSetDetailDto.kt @@ -4,6 +4,7 @@ import com.querydsl.core.annotations.QueryProjection import kr.hs.dsm.inq.domain.question.persistence.Category import kr.hs.dsm.inq.domain.question.persistence.Tags import java.time.LocalDateTime +import kr.hs.dsm.inq.domain.question.persistence.Comments class QuestionSetDetailDto @QueryProjection constructor( val questionSetId: Long, @@ -19,4 +20,7 @@ class QuestionSetDetailDto @QueryProjection constructor( val isDisliked: Boolean, val isFavorite: Boolean, val tagList: List, -) + commentList: List? +) { + val commentList: List = commentList ?: listOf() +} diff --git a/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/repository/DifficultyRepository.kt b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/repository/DifficultyRepository.kt new file mode 100644 index 0000000..2a7f5ac --- /dev/null +++ b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/repository/DifficultyRepository.kt @@ -0,0 +1,11 @@ +package kr.hs.dsm.inq.domain.question.persistence.repository + +import kr.hs.dsm.inq.domain.question.persistence.Category +import kr.hs.dsm.inq.domain.question.persistence.Difficulty +import kr.hs.dsm.inq.domain.question.persistence.Tags +import org.springframework.data.domain.Pageable +import org.springframework.data.jpa.repository.JpaRepository + +interface DifficultyRepository : JpaRepository { + fun queryByQuestionsId(questionsId: Long): Difficulty? +} \ No newline at end of file diff --git a/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/repository/QuestionSetsRepository.kt b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/repository/QuestionSetsRepository.kt index f860afe..5b49150 100644 --- a/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/repository/QuestionSetsRepository.kt +++ b/src/main/kotlin/kr/hs/dsm/inq/domain/question/persistence/repository/QuestionSetsRepository.kt @@ -6,10 +6,13 @@ import com.querydsl.jpa.impl.JPAQueryFactory import kr.hs.dsm.inq.common.util.PageResponse import kr.hs.dsm.inq.common.util.PageUtil import kr.hs.dsm.inq.domain.question.persistence.* +import kr.hs.dsm.inq.domain.question.persistence.QAnswers.answers +import kr.hs.dsm.inq.domain.question.persistence.QComments.comments import kr.hs.dsm.inq.domain.question.persistence.QQuestionSets.questionSets import kr.hs.dsm.inq.domain.question.persistence.QQuestionSolvingHistory.questionSolvingHistory import kr.hs.dsm.inq.domain.question.persistence.QQuestionTags.questionTags import kr.hs.dsm.inq.domain.question.persistence.QTags.tags +import kr.hs.dsm.inq.domain.question.persistence.dto.QAnswersDto import kr.hs.dsm.inq.domain.question.persistence.dto.QQuestionSetDetailDto import kr.hs.dsm.inq.domain.question.persistence.dto.QQuestionSetDto import kr.hs.dsm.inq.domain.question.persistence.dto.QuestionSetDetailDto @@ -118,9 +121,10 @@ class CustomQuestionSetsRepositoryImpl( val favorite = QFavorite("favorite") return@run leftJoin(questionTags).on(questionTags.problems.eq(questionSets.problemId)) .leftJoin(tags).on(tags.id.eq(questionTags.id.tagId)) - .innerJoin(author).on(author.id.eq(questionSets.authorId.id)) - .leftJoin(liked).on(liked.id.userId.eq(user.id)).on(liked.post.eq(questionSets.postId)) - .leftJoin(favorite).on(favorite.id.userId.eq(user.id)).on(favorite.problemId.eq(questionSets.problemId)) + .innerJoin(author).on(author.id.eq(questionSets.author.id)) + .leftJoin(liked).on(liked.id.userId.eq(user.id)).on(liked.post.eq(questionSets.post)) + .leftJoin(favorite).on(favorite.id.userId.eq(user.id)).on(favorite.problemId.eq(questionSets.problem)) + .rightJoin(comments).on(comments.post.eq(questionSets.post)) .transform( GroupBy.groupBy(questionSets) .list( @@ -138,6 +142,7 @@ class CustomQuestionSetsRepositoryImpl( /* isDisliked = */ liked.isLiked.isFalse, /* isFavorite = */ favorite.isNotNull, /* tagList = */ GroupBy.list(tags), + /* comments = */ GroupBy.list(comments) ) ) ) diff --git a/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/QuestionController.kt b/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/QuestionController.kt index 0215a91..8528026 100644 --- a/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/QuestionController.kt +++ b/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/QuestionController.kt @@ -3,6 +3,7 @@ package kr.hs.dsm.inq.domain.question.presentation import javax.validation.Valid import javax.websocket.server.PathParam import kr.hs.dsm.inq.domain.question.persistence.Category +import kr.hs.dsm.inq.domain.question.persistence.DifficultyLevel import kr.hs.dsm.inq.domain.question.presentation.dto.* import kr.hs.dsm.inq.domain.question.service.QuestionService import org.springframework.http.HttpStatus @@ -106,4 +107,15 @@ class QuestionController( ) { return questionService.answerQuestionInQuestionSet(questionId, answerRequest) } + + @PostMapping("//{question-id}/difficulty") + fun assessDifficulty( + @PathVariable("question-id") questionId: Long, + @RequestParam level: DifficultyLevel + ): DifficultyResponse { + return questionService.assessDifficulty( + questionId = questionId, + difficultyLevel = level + ) + } } \ No newline at end of file diff --git a/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/dto/Responses.kt b/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/dto/Responses.kt index e3fabdd..7f73f2c 100644 --- a/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/dto/Responses.kt +++ b/src/main/kotlin/kr/hs/dsm/inq/domain/question/presentation/dto/Responses.kt @@ -251,6 +251,7 @@ data class GetQuestionSetDetailResponse( val isDisliked: Boolean, val isFavorite: Boolean, val tags: List, + val comments: List ) { companion object { fun of(questionSetDetail: QuestionSetDetailDto) = questionSetDetail.run { @@ -267,8 +268,17 @@ data class GetQuestionSetDetailResponse( isLiked = isLiked, isDisliked = isDisliked, isFavorite = isFavorite, - tags = tagList.map { it.tag } + tags = tagList.map { it.tag }, + comments = commentList.map { CommentResponse.of(it) } ) } } -} \ No newline at end of file +} + +data class DifficultyResponse( + val veryEasy: Int, + val easy: Int, + val normal: Int, + val hard: Int, + val veryHard: Int +) \ No newline at end of file diff --git a/src/main/kotlin/kr/hs/dsm/inq/domain/question/service/QuestionService.kt b/src/main/kotlin/kr/hs/dsm/inq/domain/question/service/QuestionService.kt index e21dd16..ae6f712 100644 --- a/src/main/kotlin/kr/hs/dsm/inq/domain/question/service/QuestionService.kt +++ b/src/main/kotlin/kr/hs/dsm/inq/domain/question/service/QuestionService.kt @@ -29,6 +29,7 @@ class QuestionService( private val questionSetsRepository: QuestionSetsRepository, private val setQuestionRepository: SetQuestionRepository, private val questionSolvingHistoryRepository: QuestionSolvingHistoryRepository, + private val difficultyRepository: DifficultyRepository ) { fun createQuestion(request: CreateQuestionRequest): CreateQuestionResponses { @@ -368,7 +369,6 @@ class QuestionService( fun getQuestionSetDetail(questionSetId: Long): GetQuestionSetDetailResponse { val user = SecurityUtil.getCurrentUser() - questionSetsRepository val questionSetDetail = questionSetId.run { questionSetsRepository.queryQuestionSetDtoById(user, questionSetId) @@ -415,4 +415,21 @@ class QuestionService( question.apply { answerCount += 1 } ) } + + fun assessDifficulty(questionId: Long, difficultyLevel: DifficultyLevel): DifficultyResponse { + + val questions = questionsRepository.findByIdOrNull(questionId) ?: throw QuestionNotFoundException + + val difficulty = difficultyRepository.queryByQuestionsId(questions.id) ?: difficultyRepository.save( + Difficulty(questions = questions) + ) + + return DifficultyResponse( + veryEasy = difficulty.getPercentage(DifficultyLevel.VERY_EASY), + easy = difficulty.getPercentage(DifficultyLevel.EASY), + normal = difficulty.getPercentage(DifficultyLevel.NORMAL), + hard = difficulty.getPercentage(DifficultyLevel.HARD), + veryHard = difficulty.getPercentage(DifficultyLevel.VERY_HARD), + ) + } } \ No newline at end of file