Skip to content

Commit

Permalink
implement Question Domain (#23)
Browse files Browse the repository at this point in the history
* implement answer question (#26)
  • Loading branch information
daehwan2da authored Apr 2, 2024
1 parent c3ac765 commit f3ccb8a
Show file tree
Hide file tree
Showing 30 changed files with 592 additions and 75 deletions.
8 changes: 7 additions & 1 deletion plu-api/src/main/kotlin/com/th/plu/api/config/WebConfig.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.th.plu.api.config

import com.th.plu.api.config.converter.YearMonthConverter
import com.th.plu.api.config.interceptor.AuthInterceptor
import com.th.plu.api.config.resolver.MemberIdResolver
import org.springframework.context.annotation.Configuration
import org.springframework.format.FormatterRegistry
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
class WebConfig(
private val authInterceptor: AuthInterceptor,
private val memberIdResolver: MemberIdResolver
private val memberIdResolver: MemberIdResolver,
private val yearMonthConverter: YearMonthConverter,
) : WebMvcConfigurer {

override fun addInterceptors(registry: InterceptorRegistry) {
Expand All @@ -21,4 +24,7 @@ class WebConfig(
resolvers.add(memberIdResolver)
}

override fun addFormatters(registry: FormatterRegistry) {
registry.addConverter(yearMonthConverter)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.th.plu.api.config.converter

import org.springframework.core.convert.converter.Converter
import org.springframework.stereotype.Component
import java.time.YearMonth
import java.time.format.DateTimeFormatter

@Component
class YearMonthConverter : Converter<String, YearMonth> {
override fun convert(source: String): YearMonth =
YearMonth.parse(source, DateTimeFormatter.ofPattern("yyyyMM"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@ package com.th.plu.api.controller.answer

import com.th.plu.api.config.interceptor.Auth
import com.th.plu.api.config.resolver.MemberId
import com.th.plu.api.controller.answer.dto.WritingAnswerRequestDto
import com.th.plu.api.controller.answer.dto.WritingAnswerResponseDto
import com.th.plu.api.controller.answer.dto.response.AnswerInfoResponse
import com.th.plu.api.controller.answer.dto.response.EveryAnswerInfoResponse
import com.th.plu.api.service.answer.AnswerService
import com.th.plu.api.controller.answer.dto.toAnswerWriting
import com.th.plu.api.controller.answer.dto.toWritingAnswerResponse
import com.th.plu.common.dto.response.ApiResponse
import com.th.plu.api.service.answer.AnswerService
import com.th.plu.domain.domain.answer.dto.EveryAnswerRetrieveResponses
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.*

@Tag(name = "Answer")

@Tag(name = "Answers")
@RestController
@RequestMapping("/api")
class AnswerController(
private val answerService: AnswerService
private val answerService: AnswerService,
) {
@Auth
@Operation(summary = "오늘의 질문 답변하기")
@PostMapping("/v1/answers/{questionId}")
fun writeAnswer(
@MemberId memberId: Long,
@PathVariable("questionId") questionId: Long,
@RequestBody writingAnswerRequestDto: WritingAnswerRequestDto,
): ApiResponse<WritingAnswerResponseDto> =
answerService.writeAnswer(memberId, questionId, toAnswerWriting(writingAnswerRequestDto)).let {
ApiResponse.success(toWritingAnswerResponse(it))
}

@Auth
@Operation(summary = "[인증] 질문 답변 조회")
@GetMapping("/v1/answer/{answerId}")
Expand Down Expand Up @@ -52,8 +69,8 @@ class AnswerController(
@Operation(summary = "[인증] 모두의 답변 조회(무한 스크롤)")
@GetMapping("/v1/answers")
fun paginateAnswersByCursor(
@RequestParam(defaultValue = Long.MAX_VALUE.toString()) lastAnswerId: Long,
@RequestParam(defaultValue = "10") pageSize: Long,
@RequestParam(defaultValue = Long.MAX_VALUE.toString()) lastAnswerId: Long,
@RequestParam(defaultValue = "10") pageSize: Long,
): ApiResponse<EveryAnswerRetrieveResponses> {
return ApiResponse.success(answerService.findEveryAnswersWithCursor(lastAnswerId, pageSize))
}
Expand All @@ -62,8 +79,8 @@ class AnswerController(
@Operation(summary = "[인증] 모두의 답변 조회(좋아요 TopN)")
@GetMapping("/v1/answers/popular")
fun getAnswersAboutLikeTopN(
@RequestParam(defaultValue = "10") getCount: Long,
@RequestParam(defaultValue = "10") getCount: Long,
): ApiResponse<EveryAnswerRetrieveResponses> {
return ApiResponse.success(answerService.findEveryAnswersLikeTopN(getCount))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.th.plu.api.controller.answer.dto

data class AnswerResponseDto(
val id: Long,
val body: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.th.plu.api.controller.answer.dto

data class ReactionResponseDto(
val likeCount: Long,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.th.plu.api.controller.answer.dto

import com.th.plu.domain.domain.answer.AnswerWriting

data class WritingAnswerRequestDto(
val body: String,
val settings: AnswerSettingRequestDto,
)

data class AnswerSettingRequestDto(
val open: Boolean,
)

fun toAnswerWriting(writingAnswerRequestDto: WritingAnswerRequestDto) = AnswerWriting(
body = writingAnswerRequestDto.body,
open = writingAnswerRequestDto.settings.open,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.th.plu.api.controller.answer.dto

import com.th.plu.api.controller.question.dto.QuestionResponseDto
import com.th.plu.domain.domain.answer.WritingAnswerResult

data class WritingAnswerResponseDto(
val question: QuestionResponseDto,
val answer: AnswerResponseDto,
val reaction: ReactionResponseDto,
)

internal fun toWritingAnswerResponse(writingAnswerResult: WritingAnswerResult) = WritingAnswerResponseDto(
question = QuestionResponseDto(
id = writingAnswerResult.questionId,
title = writingAnswerResult.questionTitle,
content = writingAnswerResult.questionContent,
exposedAt = writingAnswerResult.questionExposedAt,
elementType = writingAnswerResult.questionElementType,
characterImageUrl = writingAnswerResult.characterImageUrl,
answered = writingAnswerResult.questionAnswered,
),
answer = AnswerResponseDto(
id = writingAnswerResult.answerId,
body = writingAnswerResult.answerBody,
),
reaction = ReactionResponseDto(
likeCount = writingAnswerResult.reactionLikeCount,
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.th.plu.api.controller.question

import com.th.plu.api.config.interceptor.Auth
import com.th.plu.api.config.resolver.MemberId
import com.th.plu.api.controller.question.dto.*
import com.th.plu.common.dto.response.ApiResponse
import com.th.plu.api.service.question.QuestionService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.format.annotation.DateTimeFormat
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.time.YearMonth

@Tag(name = "Questions")
@RestController
class QuestionController(
private val questionService: QuestionService,
) {
@Auth
@Operation(summary = "오늘의 질문")
@GetMapping("/api/v1/questions/today")
fun getQuestionToday(
@MemberId memberId: Long,
): ApiResponse<QuestionTodayResponseDto> = questionService.getQuestionToday(memberId).let {
ApiResponse.success(toQuestionTodayResponseDto(it))
}

@Auth
@Operation(summary = "내가 답변한 질문 월별 조회")
@GetMapping("/api/v1/questions/my")
fun getQuestionsWhatIAnsweredMonthly(
@MemberId memberId: Long,
@RequestParam("yearMonth") @DateTimeFormat(pattern = "yyyyMM") selectedYearMonth: YearMonth,
): ApiResponse<QuestionListResponseDto> =
questionService.getQuestionsAnsweredMonthly(memberId, selectedYearMonth).let {
ApiResponse.success(toQuestionListResponseDto(it))
}

@Auth
@Operation(summary = "답변 기록이 있는 년월 얻기")
@GetMapping("/api/v1/questions/answeredDate")
fun getYearMonthWhenIAnswered(
@MemberId memberId: Long,
): ApiResponse<QuestionAnsweredResponseDto> =
questionService.getYearMonthAnswered(memberId).let { ApiResponse.success(toQuestionAnsweredResponse(it)) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.th.plu.api.controller.question.dto

import java.time.YearMonth

data class QuestionAnsweredResponseDto(
val year: List<Int>,
val yearMonth: List<YearMonthResponseDto>
)

data class YearMonthResponseDto(
val year: Int,
val month: Int,
)

internal fun toQuestionAnsweredResponse(yearMonths: Set<YearMonth>) = QuestionAnsweredResponseDto(
year = yearMonths.map { it.year }.sortedDescending(),
yearMonth = yearMonths.map { toYearMonthResponse(it) }
)

internal fun toYearMonthResponse(yearMonth: YearMonth) = YearMonthResponseDto(year = yearMonth.year, month = yearMonth.monthValue)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.th.plu.api.controller.question.dto

import com.th.plu.domain.domain.question.QuestionResultDto

data class QuestionListResponseDto(
val answerCount: Int,
val questions: List<QuestionResponseDto>,
)

internal fun toQuestionListResponseDto(resultList: List<QuestionResultDto>): QuestionListResponseDto = QuestionListResponseDto(
answerCount = resultList.size,
questions = resultList.map { toQuestionResponseDto(it) }
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.th.plu.api.controller.question.dto

import com.th.plu.domain.domain.question.ElementType
import com.th.plu.domain.domain.question.QuestionResultDto
import java.time.LocalDateTime

data class QuestionResponseDto(
val id: Long,
val title: String,
val content: String,
val exposedAt: LocalDateTime,
val characterImageUrl: String,
val elementType: ElementType,
val answered: Boolean,
)

internal fun toQuestionResponseDto(result: QuestionResultDto) = QuestionResponseDto(
id = result.questionId,
title = result.title,
content = result.content,
exposedAt = result.exposedAt,
characterImageUrl = result.elementType.characterImageUrl,
elementType = result.elementType,
answered = result.answered,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.th.plu.api.controller.question.dto

import com.th.plu.domain.domain.question.QuestionResultDto

data class QuestionTodayResponseDto(
val question: QuestionResponseDto,
)

internal fun toQuestionTodayResponseDto(result: QuestionResultDto): QuestionTodayResponseDto = QuestionTodayResponseDto(
question = toQuestionResponseDto(result)
)
Loading

0 comments on commit f3ccb8a

Please sign in to comment.