From ab384776853b045f326b4bb4521ded83a0366379 Mon Sep 17 00:00:00 2001 From: Ryo Takeuchi Date: Mon, 4 Dec 2023 23:52:19 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20ParticipateExecutor=20=E3=82=92?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/club/nito/core/data/AuthRepository.kt | 8 ++++++++ .../club/nito/core/data/DefaultAuthRepository.kt | 4 ++++ .../club/nito/core/domain/ParticipateUseCase.kt | 14 ++++++++++++-- .../nito/core/network/auth/AuthRemoteDataSource.kt | 5 +++++ .../network/auth/SupabaseAuthRemoteDataSource.kt | 4 ++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/core/data/src/commonMain/kotlin/club/nito/core/data/AuthRepository.kt b/core/data/src/commonMain/kotlin/club/nito/core/data/AuthRepository.kt index 613398a3..5b31e701 100644 --- a/core/data/src/commonMain/kotlin/club/nito/core/data/AuthRepository.kt +++ b/core/data/src/commonMain/kotlin/club/nito/core/data/AuthRepository.kt @@ -1,8 +1,10 @@ package club.nito.core.data +import club.nito.core.model.ApiException import club.nito.core.model.AuthStatus import club.nito.core.model.UserInfo import kotlinx.coroutines.flow.Flow +import kotlin.coroutines.cancellation.CancellationException /** * 認証に関するリポジトリ @@ -27,4 +29,10 @@ public sealed interface AuthRepository { * 認証ユーザー情報を更新する */ public suspend fun modifyAuthUser(email: String?, password: String?): UserInfo + + /** + * 現在ログイン中のユーザー情報を取得する + */ + @Throws(ApiException.SessionNotFoundException::class, CancellationException::class) + public suspend fun currentUser(): UserInfo } diff --git a/core/data/src/commonMain/kotlin/club/nito/core/data/DefaultAuthRepository.kt b/core/data/src/commonMain/kotlin/club/nito/core/data/DefaultAuthRepository.kt index 3c14ba1b..1706f353 100644 --- a/core/data/src/commonMain/kotlin/club/nito/core/data/DefaultAuthRepository.kt +++ b/core/data/src/commonMain/kotlin/club/nito/core/data/DefaultAuthRepository.kt @@ -1,6 +1,7 @@ package club.nito.core.data import club.nito.core.datastore.DataStore +import club.nito.core.model.ApiException import club.nito.core.model.AuthStatus import club.nito.core.model.UserInfo import club.nito.core.network.auth.AuthRemoteDataSource @@ -31,4 +32,7 @@ public class DefaultAuthRepository( email = email, password = password, ) + + override suspend fun currentUser(): UserInfo = remoteDataSource.currentUserOrNull() + ?: throw ApiException.SessionNotFoundException(cause = null) } diff --git a/core/domain/src/commonMain/kotlin/club/nito/core/domain/ParticipateUseCase.kt b/core/domain/src/commonMain/kotlin/club/nito/core/domain/ParticipateUseCase.kt index 345c71a3..c0be2bf7 100644 --- a/core/domain/src/commonMain/kotlin/club/nito/core/domain/ParticipateUseCase.kt +++ b/core/domain/src/commonMain/kotlin/club/nito/core/domain/ParticipateUseCase.kt @@ -1,7 +1,10 @@ package club.nito.core.domain +import club.nito.core.data.AuthRepository import club.nito.core.data.ParticipantRepository import club.nito.core.model.ExecuteResult +import club.nito.core.model.participant.ParticipantDeclaration +import club.nito.core.model.runExecuting /** * 参加表明するユースケース @@ -11,9 +14,16 @@ public sealed interface ParticipateUseCase { } public class ParticipateExecutor( + private val authRepository: AuthRepository, private val participantRepository: ParticipantRepository, ) : ParticipateUseCase { - override suspend fun invoke(scheduleId: String, comment: String): ExecuteResult { - TODO("Not yet implemented") + override suspend fun invoke(scheduleId: String, comment: String): ExecuteResult = runExecuting { + participantRepository.participate( + declaration = ParticipantDeclaration( + scheduleId = scheduleId, + memberId = authRepository.currentUser().id, + comment = comment, + ), + ) } } diff --git a/core/network/src/commonMain/kotlin/club/nito/core/network/auth/AuthRemoteDataSource.kt b/core/network/src/commonMain/kotlin/club/nito/core/network/auth/AuthRemoteDataSource.kt index e60af75e..be3c0566 100644 --- a/core/network/src/commonMain/kotlin/club/nito/core/network/auth/AuthRemoteDataSource.kt +++ b/core/network/src/commonMain/kotlin/club/nito/core/network/auth/AuthRemoteDataSource.kt @@ -12,4 +12,9 @@ public sealed interface AuthRemoteDataSource { public suspend fun modifyAuthUser(email: String?, password: String?): UserInfo public suspend fun authIfNeeded() public suspend fun refreshCurrentSession() + + /** + * 現在ログイン中のユーザー情報を取得する + */ + public suspend fun currentUserOrNull(): UserInfo? } diff --git a/core/network/src/commonMain/kotlin/club/nito/core/network/auth/SupabaseAuthRemoteDataSource.kt b/core/network/src/commonMain/kotlin/club/nito/core/network/auth/SupabaseAuthRemoteDataSource.kt index c93a5175..627958a2 100644 --- a/core/network/src/commonMain/kotlin/club/nito/core/network/auth/SupabaseAuthRemoteDataSource.kt +++ b/core/network/src/commonMain/kotlin/club/nito/core/network/auth/SupabaseAuthRemoteDataSource.kt @@ -57,4 +57,8 @@ public class SupabaseAuthRemoteDataSource( } override suspend fun refreshCurrentSession(): Unit = goTrue.refreshCurrentSession() + + override suspend fun currentUserOrNull(): UserInfo? { + return goTrue.currentUserOrNull()?.let(SupabaseAuthRemoteDataSourceMapper::transformToUserInfo) + } }