Skip to content

Commit

Permalink
Merge branch 'main' into get-user-question
Browse files Browse the repository at this point in the history
  • Loading branch information
12xii authored Nov 6, 2023
2 parents 9368f7b + d26ac16 commit 7a21b87
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class QuestionSolvingHistory(

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", columnDefinition = "BIGINT", nullable = false)
var userId: User,
var user: User,

@Column(columnDefinition = "DATETIME(6)", nullable = false)
var solvedAt: LocalDateTime = LocalDateTime.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CustomQuestionSetsRepositoryImpl(
return leftJoin(questionTags).on(questionTags.problems.eq(questionSets.problem))
.leftJoin(tags).on(tags.id.eq(questionTags.id.tagId))
.leftJoin(questionSolvingHistory)
.on(questionSolvingHistory.userId.id.eq(user.id)).on(questionSolvingHistory.problem.eq(questionSets.problem))
.on(questionSolvingHistory.user.id.eq(user.id)).on(questionSolvingHistory.problem.eq(questionSets.problem))
.innerJoin(author).on(author.id.eq(questionSets.author.id))
.innerJoin(post).on(post.id.eq(questionSets.post.id))
// .rightJoin(favorite).on(favorite.questions.id.eq(questions.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ interface CustomQuestionRepository {
fun queryQuestionDtoOrderByAnswerCount(user: User, page: Long): PageResponse<QuestionDto>
fun queryQuestionDtoById(id: Long, user: User): QuestionDto?
fun queryQuestionDetailDtoById(user: User, questionId: Long): QuestionDetailDto?

fun queryQuestionDtoByWriterId(page: Long, user: User): PageResponse<UserQuestionDto>
}

Expand Down Expand Up @@ -67,6 +66,15 @@ class CustomQuestionRepositoryImpl(
)
}

override fun queryQuestionDto(
user: User,
): List<QuestionDto> {
return queryFactory
.selectFrom(questions)
.orderBy(questions.likeCount.asc())
.getQuestionDto(user)
}

override fun queryQuestionDtoOrderByAnswerCount(user: User, page: Long): PageResponse<QuestionDto> {
val questionList = queryFactory
.selectFrom(questions)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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
import org.springframework.lang.Nullable
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
Expand Down Expand Up @@ -44,11 +42,23 @@ class QuestionController(
return questionService.getTodayQuestion()
}

@GetMapping("/random")
fun getRandomQuestion(@Valid @ModelAttribute request: GetRandomQuestionRequest): QuestionResponse {
return questionService.getRandomQuestion(
category = request.category
)
}

@GetMapping("/popular")
fun getPopularQuestion(): QuestionListResponse {
return questionService.getPopularQuestion()
}

@GetMapping("/popular/set")
fun getPopularQuestionSet(): QuestionSetResponse {
return questionService.getPopularQuestionSet()
}

@GetMapping("/{question-id}")
fun getQuestionDetail(@PathVariable("question-id") questionId: Long): QuestionDetailResponse {
return questionService.getQuestionDetail(questionId)
Expand Down Expand Up @@ -94,7 +104,7 @@ class QuestionController(
}

@GetMapping("/set")
fun getQuestionSets(@Valid @ModelAttribute request: GetQuestionSetsRequest): GetQuestionSetResponse{
fun getQuestionSets(@Valid @ModelAttribute request: GetQuestionSetsRequest): QuestionSetResponse{
return questionService.getQuestionSet(request)
}

Expand Down Expand Up @@ -130,7 +140,7 @@ class QuestionController(
}

@GetMapping("/set/rank")
fun getQuestionSetRank(@Valid @ModelAttribute request: GetQuestionSetRankRequest): GetQuestionSetResponse {
fun getQuestionSetRank(@Valid @ModelAttribute request: GetQuestionSetRankRequest): QuestionSetResponse {
return questionService.getQuestionSetRank(request)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ data class GetQuestionRankRequest(
val page: Long
)

data class GetPopularQuestionRequest(
val page: Long
data class GetRandomQuestionRequest(
val category: Category?
)

data class QuestionSetsRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,37 +223,37 @@ data class RegisterQuestionSetsResponse(
val isFavorite: Boolean
)

data class GetQuestionSetResponse(
data class QuestionSetResponse(
val hasNext: Boolean,
val questionSetsList: List<QuestionSet>,
val questionSetsList: List<QuestionSetsResponse>,
) {
companion object {
fun of(pageResponse: PageResponse<QuestionSetDto>) = pageResponse.run {
GetQuestionSetResponse(
QuestionSetResponse(
hasNext = hasNext,
questionSetsList = list.map { QuestionSet.of(it) }
questionSetsList = list.map { QuestionSetsResponse.of(it) }
)
}
}
}

data class QuestionSetRankResponse(
val hasNext: Boolean,
val questionSetsList: List<QuestionSet>
val questionSetsList: List<QuestionSetsResponse>
) {
companion object {
fun of(page: Long, pageResponse: PageResponse<QuestionSetDto>) = pageResponse.run {
GetQuestionSetResponse(
QuestionSetResponse(
hasNext = hasNext,
questionSetsList = list.mapIndexed { idx, it ->
QuestionSet.of(it, PageUtil.getOffset(page) + idx + 1)
QuestionSetsResponse.of(it, PageUtil.getOffset(page) + idx + 1)
}
)
}
}
}

data class QuestionSet(
data class QuestionSetsResponse(
val questionSetId: Long?,
val rank: Long? = null,
val questionSetName: String?,
Expand All @@ -269,7 +269,7 @@ data class QuestionSet(
) {
companion object {
fun of(dto: QuestionSetDto) = dto.run {
QuestionSet(
QuestionSetsResponse(
questionSetId = questionSetId,
questionSetName = questionSetName,
createdAt = createdAt,
Expand All @@ -285,7 +285,7 @@ data class QuestionSet(
}

fun of(dto: QuestionSetDto, rank: Long) = dto.run {
QuestionSet(
QuestionSetsResponse(
questionSetId = questionSetId,
rank = rank,
questionSetName = questionSetName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,20 @@ class QuestionService(

fun getTodayQuestion(): QuestionResponse {
val user = SecurityUtil.getCurrentUser()
val todayQuestion = questionsRepository.queryQuestionDtoById(1L, user) ?: throw QuestionNotFoundException
val todayQuestion = questionsRepository.queryQuestionDto(user = user).get(0)
return QuestionResponse.of(todayQuestion)
}

fun getRandomQuestion(category: Category?): QuestionResponse {
val user = SecurityUtil.getCurrentUser()
val random = questionsRepository.queryQuestionDtoOrderByLike(
user = user,
page = 0,
category = category
).list.random()
return QuestionResponse.of(random)
}

fun getPopularQuestion(): QuestionListResponse {

val user = SecurityUtil.getCurrentUser()
Expand All @@ -153,6 +163,15 @@ class QuestionService(
return QuestionListResponse.of(questionList)
}

fun getPopularQuestionSet(): QuestionSetResponse {
val user = SecurityUtil.getCurrentUser()
val questionSetList = questionSetsRepository.queryQuestionSetDtoOrderByLike(
user = user,
page = 1L
)
return QuestionSetResponse.of(questionSetList)
}

fun getQuestionDetail(questionId: Long): QuestionDetailResponse {

val user = SecurityUtil.getCurrentUser()
Expand Down Expand Up @@ -198,9 +217,7 @@ class QuestionService(
fun answerQuestion(questionId: Long, request: AnswerRequest) {

val user = SecurityUtil.getCurrentUser()

val questions = questionsRepository.findByIdOrNull(questionId) ?: throw QuestionNotFoundException

val post = postRepository.save(Post())

answersRepository.save(
Expand All @@ -219,7 +236,7 @@ class QuestionService(

questionSolvingHistoryRepository.save(
QuestionSolvingHistory(
userId = user,
user = user,
problem = questions.problem
)
)
Expand Down Expand Up @@ -378,7 +395,7 @@ class QuestionService(
)
}

fun getQuestionSet(request: GetQuestionSetsRequest): GetQuestionSetResponse {
fun getQuestionSet(request: GetQuestionSetsRequest): QuestionSetResponse {
val user = SecurityUtil.getCurrentUser()

val questionSetList = request.run{
Expand All @@ -391,7 +408,7 @@ class QuestionService(
)
}

return GetQuestionSetResponse.of(questionSetList)
return QuestionSetResponse.of(questionSetList)
}

fun getQuestionSetDetail(questionSetId: Long): GetQuestionSetDetailResponse {
Expand All @@ -416,7 +433,7 @@ class QuestionService(

questionSolvingHistoryRepository.save(
QuestionSolvingHistory(
userId = user,
user = user,
problem = questionSet.problem
)
)
Expand Down Expand Up @@ -464,7 +481,7 @@ class QuestionService(
)
}

fun getQuestionSetRank(request: GetQuestionSetRankRequest): GetQuestionSetResponse {
fun getQuestionSetRank(request: GetQuestionSetRankRequest): QuestionSetResponse {
val user = SecurityUtil.getCurrentUser()

val questionSetList = questionSetsRepository.queryQuestionSetDtoOrderByLike(
Expand Down

0 comments on commit 7a21b87

Please sign in to comment.