Skip to content

Commit

Permalink
kuring-217 남은 질문 횟수를 서버에서 받아오도록 수정 (#339)
Browse files Browse the repository at this point in the history
* feature: 쿠링봇 프리뷰 데이터를 실제 로직에 맞게 수정

* feature: 쿠링봇 질문 횟수 response 클래스 추가

* feature: 쿠링봇 질문 횟수 API 추가

* feature: 쿠링봇 질문 횟수 API를 UI에 노출

* feature: 쿠링봇 질문 횟수를 서버에서 받아오게 수정

* remove: 로컬 횟수 계산 코드 제거

* refactor: try-catch 대신 runCatching 사용
  • Loading branch information
mwy3055 authored Sep 23, 2024
1 parent fb0b080 commit c7d02e5
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ku_stacks.ku_ring.ai.repository

import com.ku_stacks.ku_ring.domain.KuringBotMessage
import java.time.LocalDate

interface KuringBotRepository {
/**
Expand Down Expand Up @@ -41,11 +40,9 @@ interface KuringBotRepository {
suspend fun insertMessage(message: KuringBotMessage)

/**
* 지정된 기간 동안에 전송한 질문의 수를 반환한다.
* 남은 질문 횟수를 반환한다.
*
* @param from 기간의 시작
* @param to 기간의 끝
* @return [from]부터 [to]까지 전송한 질문의 수 (inclusive)
* @param token 사용자의 FCM 토큰
*/
suspend fun getQueryCount(from: LocalDate, to: LocalDate): Int
suspend fun getQueryCount(token: String): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import com.ku_stacks.ku_ring.ai.mapper.toEntity
import com.ku_stacks.ku_ring.domain.KuringBotMessage
import com.ku_stacks.ku_ring.local.room.KuringBotMessageDao
import com.ku_stacks.ku_ring.remote.kuringbot.KuringBotClient
import com.ku_stacks.ku_ring.util.toEpochSecond
import java.time.LocalDate
import com.ku_stacks.ku_ring.remote.user.UserClient
import javax.inject.Inject

class KuringBotRepositoryImpl @Inject constructor(
private val kuringBotClient: KuringBotClient,
private val kuringBotMessageDao: KuringBotMessageDao,
private val userClient: UserClient,
) : KuringBotRepository {

override suspend fun openKuringBotSession(
Expand All @@ -38,8 +38,8 @@ class KuringBotRepositoryImpl @Inject constructor(
kuringBotMessageDao.insertMessage(message.toEntity())
}

override suspend fun getQueryCount(from: LocalDate, to: LocalDate): Int {
return kuringBotMessageDao.getQueryCount(from.toEpochSecond(), to.toEpochSecond())
override suspend fun getQueryCount(token: String): Int {
return userClient.getKuringBotQueryCount(token)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.ku_stacks.ku_ring.ai.repository.KuringBotRepository
import com.ku_stacks.ku_ring.ai.repository.KuringBotRepositoryImpl
import com.ku_stacks.ku_ring.local.room.KuringBotMessageDao
import com.ku_stacks.ku_ring.remote.kuringbot.KuringBotClient
import com.ku_stacks.ku_ring.remote.user.UserClient
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
Expand All @@ -13,13 +14,14 @@ import org.mockito.exceptions.base.MockitoException
class KuringBotRepositoryImplTest {
private val kuringBotClient = Mockito.mock(KuringBotClient::class.java)
private val dao = Mockito.mock(KuringBotMessageDao::class.java)
private val userClient = Mockito.mock(UserClient::class.java)
private lateinit var repository: KuringBotRepository

private val query = "교내,외 장학금 및 학자금 대출 관련 전화번호들을 안내를 해줘"

@Before
fun setup() {
repository = KuringBotRepositoryImpl(kuringBotClient, dao)
repository = KuringBotRepositoryImpl(kuringBotClient, dao, userClient)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ class UserClient @Inject constructor(

suspend fun registerUser(token: String): DefaultResponse =
userService.registerUser(RegisterUserRequest(token))

suspend fun getKuringBotQueryCount(token: String): Int {
return runCatching {
userService.getKuringBotQueryCount(token).data.leftAskCount
}.getOrElse { 0 }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.ku_stacks.ku_ring.remote.user

import com.ku_stacks.ku_ring.remote.user.request.FeedbackRequest
import com.ku_stacks.ku_ring.remote.user.request.RegisterUserRequest
import com.ku_stacks.ku_ring.remote.user.response.KuringBotQueryCountResponse
import com.ku_stacks.ku_ring.remote.util.DefaultResponse
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST

Expand All @@ -16,4 +18,7 @@ interface UserService {

@POST("v2/users")
suspend fun registerUser(@Body registerUserRequest: RegisterUserRequest): DefaultResponse

@GET("v2/users/ask-counts")
suspend fun getKuringBotQueryCount(@Header("User-Token") token: String): KuringBotQueryCountResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ku_stacks.ku_ring.remote.user.response

data class KuringBotQueryCountResponse(
val code: Int,
val message: String,
val data: KuringBotQueryCountData,
)

data class KuringBotQueryCountData(
val leftAskCount: Int,
val maxAskCount: Int,
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ku_stacks.ku_ring.ai.repository.KuringBotRepository
import com.ku_stacks.ku_ring.domain.KuringBotMessage
import com.ku_stacks.ku_ring.kuringbot.mapper.toUIMessages
import com.ku_stacks.ku_ring.preferences.PreferenceUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
Expand All @@ -14,12 +15,12 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDate
import javax.inject.Inject

@HiltViewModel
class KuringBotViewModel @Inject constructor(
private val kuringBotRepository: KuringBotRepository,
private val messageCounter: KuringBotMessageCounter,
preferences: PreferenceUtil,
) : ViewModel() {
private val token = preferences.fcmToken
Expand All @@ -40,8 +41,7 @@ class KuringBotViewModel @Inject constructor(
}

private fun addInitialMessages(messages: List<KuringBotMessage>) {
val uiMessages = messageCounter.convertInitialUIMessages(messages)
addUiMessages(uiMessages)
addUiMessages(messages.toUIMessages())
}

private fun addUiMessages(messages: List<KuringBotUIMessage>) {
Expand Down Expand Up @@ -92,16 +92,14 @@ class KuringBotViewModel @Inject constructor(
response = response.copy(message = response.message + it)
updateLastMessage(response)
}
addUiMessage(messageCounter.calculateQuestionsRemaining(response.postedTime))

addQueryCountMessage()

stopKuringBotJob(response)
}

private fun addUiMessage(message: KuringBotUIMessage) {
addUiMessages(listOf(message))
if (message is KuringBotUIMessage.Question) {
messageCounter.increaseMessageCount(message.toDomain())
}
}

private suspend fun saveMessageToLocal(message: KuringBotUIMessage.Savable) =
Expand All @@ -118,6 +116,15 @@ class KuringBotViewModel @Inject constructor(
updateMessages(newMessages)
}

private suspend fun addQueryCountMessage() {
addUiMessage(
KuringBotUIMessage.QuestionsRemaining(
kuringBotRepository.getQueryCount(token),
LocalDate.now(),
)
)
}

private suspend fun stopKuringBotJob(response: KuringBotUIMessage.Response) {
saveMessageToLocal(response)
stopKuringBotJob()
Expand All @@ -138,8 +145,8 @@ class KuringBotViewModel @Inject constructor(
if (savable is KuringBotUIMessage.Response) {
saveMessageToLocal(savable)
}
addUiMessage(messageCounter.calculateQuestionsRemaining(savable.postedTime))
}
addQueryCountMessage()

stopKuringBotJob()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.ku_stacks.ku_ring.kuringbot.R
import com.ku_stacks.ku_ring.kuringbot.compose.components.*
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Month

@Composable
internal fun KuringBotScreen(
Expand Down Expand Up @@ -357,13 +358,21 @@ private fun KuringBotScreenPreview() {
}
}

private val previewMessages = listOf(
private val previewMessages: List<KuringBotUIMessage> = listOf(
KuringBotUIMessage.Question(
message = "예시 질문".repeat(10),
postedTime = LocalDateTime.now(),
postedTime = LocalDateTime.of(2024, Month.JANUARY, 1, 12, 0),
),
KuringBotUIMessage.Response(
message = "예시 대답".repeat(10),
postedTime = LocalDateTime.of(2024, Month.JANUARY, 1, 12, 0),
),
KuringBotUIMessage.Question(
message = "예시 질문",
postedTime = LocalDateTime.now(),
),
KuringBotUIMessage.Response(
message = "예시 대답",
postedTime = LocalDateTime.now(),
),
KuringBotUIMessage.QuestionsRemaining(
Expand All @@ -381,7 +390,7 @@ private val previewMessages = listOf(
KuringBotUIMessage.QuestionsRemaining(
questionsRemaining = 0,
postedTime = LocalDate.now(),
)
),
)

@LightAndDarkPreview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.ku_stacks.ku_ring.kuringbot.mapper
import com.ku_stacks.ku_ring.domain.KuringBotMessage
import com.ku_stacks.ku_ring.kuringbot.KuringBotUIMessage

internal fun List<KuringBotMessage>.toUIMessages(): List<KuringBotUIMessage> =
map { it.toUIMessage() }

internal fun KuringBotMessage.toUIMessage(): KuringBotUIMessage = when (isQuery) {
true -> KuringBotUIMessage.Question(
id = id,
Expand Down

0 comments on commit c7d02e5

Please sign in to comment.