Skip to content

Commit

Permalink
fix: response 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
12xii committed Nov 6, 2023
1 parent 2c13995 commit 9368f7b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ 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.Category
import kr.hs.dsm.inq.domain.question.persistence.QAnswers.answers
import kr.hs.dsm.inq.domain.question.persistence.QQuestionTags.questionTags
import kr.hs.dsm.inq.domain.question.persistence.QQuestions.questions
import kr.hs.dsm.inq.domain.question.persistence.QTags.tags
import kr.hs.dsm.inq.domain.question.persistence.Questions
import kr.hs.dsm.inq.domain.question.persistence.dto.*
import kr.hs.dsm.inq.domain.user.persistence.QUser
import kr.hs.dsm.inq.domain.user.persistence.User
import kr.hs.dsm.inq.domain.user.presentation.dto.QUserQuestionDto
import kr.hs.dsm.inq.domain.user.presentation.dto.UserQuestionDto
import kr.hs.dsm.inq.domain.user.persistence.dto.QUserQuestionDto
import kr.hs.dsm.inq.domain.user.persistence.dto.UserQuestionDto
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository

Expand All @@ -35,7 +36,7 @@ interface CustomQuestionRepository {
fun queryQuestionDtoById(id: Long, user: User): QuestionDto?
fun queryQuestionDetailDtoById(user: User, questionId: Long): QuestionDetailDto?

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

@Repository
Expand Down Expand Up @@ -151,26 +152,40 @@ class CustomQuestionRepositoryImpl(
)
}

override fun queryGetQuestionDtoById(user: User): List<UserQuestionDto> {
return queryFactory
override fun queryQuestionDtoByWriterId(page: Long, user: User): PageResponse<UserQuestionDto> {
val questions = queryFactory
.selectFrom(questions)
.where(questions.author.eq(user))
.getUserQuestionListDto(user)

return PageUtil.toPageResponse(
page = page,
list = questions
)
}

fun <T> JPAQuery<T>.getUserQuestionListDto(user: User): List<UserQuestionDto> = run {
val writer = QUser("writer")

return@run innerJoin(questionTags).on(questionTags.problems.eq(questions.problem))
.innerJoin(tags).on(tags.id.eq(questionTags.id.tagId))
.innerJoin(answers).on(answers.questions.id.eq(questions.id))
.rightJoin(writer).on(writer.id.eq(user.id))
.transform(
GroupBy.groupBy(questions)
.list(
QUserQuestionDto(
/* questionId = */ questions.id,
/* authorId = */ writer.id,
/* username = */ writer.username,
/* job = */ writer.job,
/* jobDuration = */ writer.jobDuration,
/* question = */ questions.question,
/* createdAt = */ questions.createdAt,
/* category = */ questions.category,
/* tagList = */ GroupBy.list(tags),
/* isAnswered = */ questions.isNull
/* isFavorite = */ questions.isNull, // favorite.isNotNull
/* exemplaryAnswer = */ answers.answer,
/* createdAt = */ questions.createdAt,
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,40 @@ data class QuestionDetailResponse(
createdAt = createdAt
)
}


}
}

data class UserQuestionResponse(
val questionId: Long,
val authorId: Long,
val username: String,
val job: String,
val jobDuration: Int,
val question: String,
val category: Category,
val tags: List<String>,
val isFavorite: Boolean,
val exemplaryAnswer: String,
val createdAt: LocalDateTime
) {
companion object {
fun of(questionDetail: QuestionDetailDto, exemplaryAnswer: String) = questionDetail.run {
UserQuestionResponse(
questionId = questionId,
authorId = authorId,
username = username,
job = job,
jobDuration = jobDuration,
question = question,
category = category,
tags = tagList.map { it.tag },
isFavorite = isFavorite,
exemplaryAnswer = exemplaryAnswer,
createdAt = createdAt
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.hs.dsm.inq.domain.user.persistence.dto

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

class UserQuestionDto @QueryProjection constructor(
val questionId: Long,
val authorId: Long,
val username: String,
val job: String,
val jobDuration: Int,
val question: String,
val category: Category,
tagList: List<Tags>?,
val isFavorite: Boolean,
val exemplaryAnswer : String,
val createdAt: LocalDateTime
) {
val tagList: List<Tags> = tagList ?: listOf()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package kr.hs.dsm.inq.domain.user.presentation

import javax.validation.Valid
import kr.hs.dsm.inq.common.dto.TokenResponse
import kr.hs.dsm.inq.domain.user.presentation.dto.GetMyQuestionResponse
import kr.hs.dsm.inq.domain.question.presentation.dto.UserQuestionResponse
import kr.hs.dsm.inq.domain.user.presentation.dto.*
import kr.hs.dsm.inq.domain.user.service.UserService
import org.springframework.http.HttpStatus
Expand Down Expand Up @@ -38,7 +38,7 @@ class UserController(
userService.queryUserAttendance()

@GetMapping("/question/{page}")
fun getMyQuestion(@PathVariable("page") page: Int): GetMyQuestionResponse {
fun getMyQuestion(@PathVariable("page") page: Long): List<UserQuestionResponse> {
return userService.getMyQuestion(page)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.hs.dsm.inq.domain.user.presentation.dto

import kr.hs.dsm.inq.domain.user.persistence.dto.UserQuestionDto
import java.time.LocalDate

data class UserInfoResponse(
Expand All @@ -24,16 +25,4 @@ data class UserAttendanceResponse(
val saturday: Boolean,

val sunday: Boolean,
)

data class GetMyQuestionResponse(
val questionList: List<UserQuestionDto>
) {
companion object {
fun of(questionList: List<UserQuestionDto>) = questionList.run{
GetMyQuestionResponse(
questionList = questionList.map { it }
)
}
}
}
)

This file was deleted.

26 changes: 22 additions & 4 deletions src/main/kotlin/kr/hs/dsm/inq/domain/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package kr.hs.dsm.inq.domain.user.service

import kr.hs.dsm.inq.common.dto.TokenResponse
import kr.hs.dsm.inq.common.util.SecurityUtil
import kr.hs.dsm.inq.domain.question.persistence.dto.QuestionDetailDto
import kr.hs.dsm.inq.domain.question.persistence.repository.QuestionsRepository
import kr.hs.dsm.inq.domain.user.presentation.dto.GetMyQuestionResponse
import kr.hs.dsm.inq.domain.question.presentation.dto.UserQuestionResponse
import kr.hs.dsm.inq.domain.user.exception.AttendanceNotFound
import kr.hs.dsm.inq.domain.user.exception.PasswordMismatchException
import kr.hs.dsm.inq.domain.user.exception.UserAlreadyExist
Expand Down Expand Up @@ -93,11 +94,28 @@ class UserService(
)
}

fun getMyQuestion(page: Int): GetMyQuestionResponse {
fun getMyQuestion(page: Long): List<UserQuestionResponse> {
val user = SecurityUtil.getCurrentUser()

val usersQuestions = questionsRepository.queryGetQuestionDtoById(user)
val usersQuestions = questionsRepository.queryQuestionDtoByWriterId(page, user)

return usersQuestions.list.map {
UserQuestionResponse.of(
questionDetail = QuestionDetailDto(
questionId = it.questionId,
authorId = it.authorId,
username = it.username,
job = it.job,
jobDuration = it.jobDuration,
question = it.question,
category = it.category,
tagList = it.tagList,
isFavorite = it.isFavorite,
createdAt = it.createdAt
),
exemplaryAnswer = it.exemplaryAnswer
)
}

return GetMyQuestionResponse.of(usersQuestions)
}
}

0 comments on commit 9368f7b

Please sign in to comment.