Skip to content

Commit

Permalink
feat: 내가 등록한 질문 api
Browse files Browse the repository at this point in the history
  • Loading branch information
12xii committed Nov 5, 2023
1 parent d9eea0d commit 2c13995
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ 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.QQuestionDetailDto
import kr.hs.dsm.inq.domain.question.persistence.dto.QQuestionDto
import kr.hs.dsm.inq.domain.question.persistence.dto.QuestionDetailDto
import kr.hs.dsm.inq.domain.question.persistence.dto.QuestionDto
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 org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository

Expand All @@ -35,6 +34,8 @@ 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 queryGetQuestionDtoById(user: User): List<UserQuestionDto>
}

@Repository
Expand Down Expand Up @@ -109,16 +110,6 @@ class CustomQuestionRepositoryImpl(
)
)
)

// val questionId: Long,
// val question: String,
// val category: Category,
// val username: String,
// val job: String,
// val jobDuration: Int,
// tagList: List<Tags>?,
// val isAnswered: Boolean,
// val isFavorite: Boolean
}

override fun queryQuestionDetailDtoById(
Expand Down Expand Up @@ -159,4 +150,29 @@ class CustomQuestionRepositoryImpl(
)
)
}

override fun queryGetQuestionDtoById(user: User): List<UserQuestionDto> {
return queryFactory
.selectFrom(questions)
.where(questions.author.eq(user))
.getUserQuestionListDto(user)
}

fun <T> JPAQuery<T>.getUserQuestionListDto(user: User): List<UserQuestionDto> = run {
return@run innerJoin(questionTags).on(questionTags.problems.eq(questions.problem))
.innerJoin(tags).on(tags.id.eq(questionTags.id.tagId))
.transform(
GroupBy.groupBy(questions)
.list(
QUserQuestionDto(
/* questionId = */ questions.id,
/* question = */ questions.question,
/* createdAt = */ questions.createdAt,
/* category = */ questions.category,
/* tagList = */ GroupBy.list(tags),
/* isAnswered = */ questions.isNull
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kr.hs.dsm.inq.common.util.PageUtil
import kr.hs.dsm.inq.domain.question.persistence.*
import kr.hs.dsm.inq.domain.question.persistence.dto.*
import java.time.LocalDateTime
import java.util.Date

data class CreateQuestionResponses(
val questionId: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ 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.user.presentation.dto.*
import kr.hs.dsm.inq.domain.user.service.UserService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@RequestMapping("/user")
@RestController
Expand Down Expand Up @@ -42,4 +37,8 @@ class UserController(
fun queryUserAttendance(): UserAttendanceResponse =
userService.queryUserAttendance()

@GetMapping("/question/{page}")
fun getMyQuestion(@PathVariable("page") page: Int): GetMyQuestionResponse {
return userService.getMyQuestion(page)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ 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 }
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kr.hs.dsm.inq.domain.user.presentation.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 question: String,
val createdAt: LocalDateTime,
val category: Category,
tagList: List<Tags>?,
val isAnswered: Boolean
) {
val tagList = tagList ?: listOf()
}
14 changes: 12 additions & 2 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,6 +2,8 @@ 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.repository.QuestionsRepository
import kr.hs.dsm.inq.domain.user.presentation.dto.GetMyQuestionResponse
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 All @@ -12,7 +14,6 @@ import kr.hs.dsm.inq.domain.user.presentation.dto.*
import kr.hs.dsm.inq.global.security.token.JwtGenerator
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import java.time.LocalDate
import java.time.LocalDateTime
import javax.transaction.Transactional
import kr.hs.dsm.inq.domain.user.persistence.repository.UserRepository
Expand All @@ -22,7 +23,8 @@ class UserService(
private val userRepository: UserRepository,
private val passwordEncoder: PasswordEncoder,
private val jwtGenerator: JwtGenerator,
private val attendanceRepository: AttendanceRepository
private val attendanceRepository: AttendanceRepository,
private val questionsRepository: QuestionsRepository
) {

fun signIn(request: UserSignInRequest): TokenResponse {
Expand Down Expand Up @@ -90,4 +92,12 @@ class UserService(
sunday = attendance.sunday
)
}

fun getMyQuestion(page: Int): GetMyQuestionResponse {
val user = SecurityUtil.getCurrentUser()

val usersQuestions = questionsRepository.queryGetQuestionDtoById(user)

return GetMyQuestionResponse.of(usersQuestions)
}
}

0 comments on commit 2c13995

Please sign in to comment.