-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.susu.core.model | ||
|
||
data class Vote( | ||
val id: Long, | ||
val category: String, | ||
val content: String, | ||
val isModified: Boolean, | ||
val optionList: List<String> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
data/src/main/java/com/susu/data/data/repository/VoteRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.susu.data.data.repository | ||
|
||
import com.susu.core.android.Dispatcher | ||
import com.susu.core.android.SusuDispatchers | ||
import com.susu.core.model.Category | ||
import com.susu.core.model.Vote | ||
import com.susu.data.local.dao.CategoryConfigDao | ||
import com.susu.data.local.model.toEntity | ||
import com.susu.data.local.model.toModel | ||
import com.susu.data.remote.api.CategoryService | ||
import com.susu.data.remote.api.VoteService | ||
import com.susu.data.remote.model.request.CreateVoteRequest | ||
import com.susu.data.remote.model.request.VoteOption | ||
import com.susu.data.remote.model.response.toModel | ||
import com.susu.domain.repository.CategoryConfigRepository | ||
import com.susu.domain.repository.VoteRepository | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class VoteRepositoryImpl @Inject constructor( | ||
private val api: VoteService, | ||
) : VoteRepository { | ||
override suspend fun createVote( | ||
content: String, | ||
optionList: List<String>, | ||
categoryId: Int, | ||
): Vote = api.createVote( | ||
createVoteRequest = CreateVoteRequest( | ||
content = content, | ||
optionList = optionList.mapIndexed { index, voteContent -> | ||
VoteOption( | ||
content = voteContent, | ||
seq = index + 1, | ||
) | ||
}, | ||
categoryId = categoryId, | ||
), | ||
).getOrThrow().toModel() | ||
} |
15 changes: 15 additions & 0 deletions
15
data/src/main/java/com/susu/data/remote/api/VoteService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.susu.data.remote.api | ||
|
||
import com.susu.data.remote.model.request.CreateVoteRequest | ||
import com.susu.data.remote.model.response.VoteResponse | ||
import com.susu.data.remote.retrofit.ApiResult | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface VoteService { | ||
|
||
@POST("/api/v1/votes") | ||
suspend fun createVote( | ||
@Body createVoteRequest: CreateVoteRequest | ||
): ApiResult<VoteResponse> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
data/src/main/java/com/susu/data/remote/model/request/CreateVoteRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.susu.data.remote.model.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CreateVoteRequest( | ||
val content: String, | ||
@SerialName("options") | ||
val optionList: List<VoteOption>, | ||
@SerialName("postCategoryId") | ||
val categoryId: Int | ||
) | ||
|
||
@Serializable | ||
data class VoteOption( | ||
val content: String, | ||
val seq: Int | ||
) |
24 changes: 24 additions & 0 deletions
24
data/src/main/java/com/susu/data/remote/model/response/VoteResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.susu.data.remote.model.response | ||
|
||
import com.susu.core.model.Vote | ||
import com.susu.data.remote.model.request.VoteOption | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class VoteResponse( | ||
val id: Long, | ||
val category: String, | ||
val content: String, | ||
val isModified: Boolean, | ||
@SerialName("options") | ||
val optionList: List<VoteOption> | ||
) | ||
|
||
internal fun VoteResponse.toModel() = Vote( | ||
id = id, | ||
category = category, | ||
content = content, | ||
isModified = isModified, | ||
optionList = optionList.sortedBy { it.seq }.map { it.content }, | ||
) |
11 changes: 11 additions & 0 deletions
11
domain/src/main/java/com/susu/domain/repository/VoteRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.susu.domain.repository | ||
|
||
import com.susu.core.model.Vote | ||
|
||
interface VoteRepository { | ||
suspend fun createVote( | ||
content: String, | ||
optionList: List<String>, | ||
categoryId: Int | ||
): Vote | ||
} |
26 changes: 26 additions & 0 deletions
26
domain/src/main/java/com/susu/domain/usecase/vote/CreateVoteUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.susu.domain.usecase.vote | ||
|
||
import com.susu.core.common.runCatchingIgnoreCancelled | ||
import com.susu.domain.repository.CategoryConfigRepository | ||
import com.susu.domain.repository.VoteRepository | ||
import javax.inject.Inject | ||
|
||
class CreateVoteUseCase @Inject constructor( | ||
private val voteRepository: VoteRepository, | ||
) { | ||
suspend operator fun invoke(param: Param) = runCatchingIgnoreCancelled { | ||
with(param) { | ||
voteRepository.createVote( | ||
content = content, | ||
optionList = optionList, | ||
categoryId = categoryId, | ||
) | ||
} | ||
} | ||
|
||
data class Param( | ||
val content: String, | ||
val optionList: List<String>, | ||
val categoryId: Int, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters