Skip to content

Commit

Permalink
feat: 질문세트 response 댓글 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed Oct 31, 2023
1 parent 74a1538 commit 7dd768b
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,4 +20,7 @@ class QuestionSetDetailDto @QueryProjection constructor(
val isDisliked: Boolean,
val isFavorite: Boolean,
val tagList: List<Tags>,
)
commentList: List<Comments>?
) {
val commentList: List<Comments> = commentList ?: listOf()
}
Original file line number Diff line number Diff line change
@@ -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<Difficulty, Long> {
fun queryByQuestionsId(questionsId: Long): Difficulty?
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -138,6 +142,7 @@ class CustomQuestionSetsRepositoryImpl(
/* isDisliked = */ liked.isLiked.isFalse,
/* isFavorite = */ favorite.isNotNull,
/* tagList = */ GroupBy.list(tags),
/* comments = */ GroupBy.list(comments)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ data class GetQuestionSetDetailResponse(
val isDisliked: Boolean,
val isFavorite: Boolean,
val tags: List<String>,
val comments: List<CommentResponse>
) {
companion object {
fun of(questionSetDetail: QuestionSetDetailDto) = questionSetDetail.run {
Expand All @@ -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) }
)
}
}
}
}

data class DifficultyResponse(
val veryEasy: Int,
val easy: Int,
val normal: Int,
val hard: Int,
val veryHard: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -368,7 +369,6 @@ class QuestionService(
fun getQuestionSetDetail(questionSetId: Long): GetQuestionSetDetailResponse {

val user = SecurityUtil.getCurrentUser()
questionSetsRepository

val questionSetDetail = questionSetId.run {
questionSetsRepository.queryQuestionSetDtoById(user, questionSetId)
Expand Down Expand Up @@ -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),
)
}
}

0 comments on commit 7dd768b

Please sign in to comment.