Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/api/quiz/check_solve_today_quiz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Check solve today quiz

오늘의 푼 퀴즈 정보를 조회합니다.

## Request
### Http method: `GET`
### URL: `https://api.gitanimals.org/quizs/context/today`
### Request header
- Authorization: `{token}`

## Response

오늘 풀었다면 아래와 같이 응답됩니다.

Response Status 200

```json
{
"isSolved": true,
"contextId": "12345",
"prize": 12345, // 오늘 획득한 prize가 응답된다. 획득한 돈이 없다면 (ex. FAIL) 0
"result": "FAIL" // FAIL, SOLVING, SUCCESS, FAIL, DONE
}
```

오늘의 퀴즈를 아직 풀지 않았다면 아래와 같이 응답됩니다.

Response Status 200

```json
{
"isSolved": false
}
```
8 changes: 8 additions & 0 deletions src/main/kotlin/org/gitanimals/quiz/app/SolveQuizFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.gitanimals.quiz.app

import org.gitanimals.quiz.app.request.CreateSolveQuizRequest
import org.gitanimals.quiz.app.response.QuizContextResponse
import org.gitanimals.quiz.app.response.TodaySolvedContextResponse
import org.gitanimals.quiz.domain.approved.QuizService
import org.gitanimals.quiz.domain.context.QuizSolveContext
import org.gitanimals.quiz.domain.context.QuizSolveContextService
Expand Down Expand Up @@ -64,6 +65,13 @@ class SolveQuizFacade(
quizSolveContextService.stopQuizByIdAndUserId(id = id, userId = userId)
}

fun getTodaySolvedContextResult(token: String): TodaySolvedContextResponse {
val userId = identityApi.getUserByToken(token).id.toLong()
val quizSolveContext = quizSolveContextService.findTodaySolvedContext(userId)

return TodaySolvedContextResponse.from(quizSolveContext)
}

private companion object {
private val quizLevels =
listOf(Level.EASY, Level.EASY, Level.MEDIUM, Level.DIFFICULT, Level.DIFFICULT)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.gitanimals.quiz.app.response

import org.gitanimals.quiz.domain.context.QuizSolveContext
import org.gitanimals.quiz.domain.context.QuizSolveContextStatus

data class TodaySolvedContextResponse(
val isSolved: Boolean,
val contextId: String?,
val prize: Int?,
val result: QuizSolveContextStatus?,
) {

companion object {

fun from(quizSolveContext: QuizSolveContext?): TodaySolvedContextResponse {
if (quizSolveContext == null) {
return TodaySolvedContextResponse(
isSolved = false,
contextId = null,
prize = null,
result = null,
)
}

return TodaySolvedContextResponse(
isSolved = true,
contextId = quizSolveContext.id.toString(),
prize = quizSolveContext.getPrize(),
result = quizSolveContext.getStatus(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ class QuizContextController(
@PathVariable("contextId") contextId: Long,
) = solveQuizFacade.stopQuiz(token = token, id = contextId)


@ResponseStatus(HttpStatus.OK)
@GetMapping("/quizs/context/today")
fun getTodaySolvedContextResult(
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
) = solveQuizFacade.getTodaySolvedContextResult(token)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class QuizSolveContextService(
private val quizSolveContextRepository: QuizSolveContextRepository,
) {

// @Transactional
// @Transactional
fun createQuizSolveContext(
userId: Long,
category: Category,
quizs: List<Quiz>,
): QuizSolveContext {
val now = LocalDate.ofInstant(instant(), ZoneId.of("Asia/Seoul"))
val now = LocalDate.ofInstant(instant(), ZoneId.of("UTC"))
quizSolveContextRepository.findQuizSolveContextByUserIdAndSolvedAt(userId, now)?.let {
quizSolveContextRepository.deleteAll() // 전체삭제 QA끝나고 지운다.
// throw IllegalArgumentException("Already solve daily quiz.")
Expand Down Expand Up @@ -57,6 +57,13 @@ class QuizSolveContextService(
quizSolveContext.stopSolve()
}

@Transactional(readOnly = true)
fun findTodaySolvedContext(userId: Long): QuizSolveContext?{
val now = LocalDate.ofInstant(instant(), ZoneId.of("UTC"))

return quizSolveContextRepository.findQuizSolveContextByUserIdAndSolvedAt(userId, now)
}

private fun getQuizSolveContextByIdAndUserIdWithLock(
id: Long,
userId: Long
Expand Down