Skip to content

Latest commit

ย 

History

History
87 lines (71 loc) ยท 3.06 KB

data-layer.md

File metadata and controls

87 lines (71 loc) ยท 3.06 KB

Data Layer

Files

Path under core/data/.../<domain>/ Example
model/ LoginResult
exception/ AuthException
remote/service/ AuthService
remote/dto/request|response/ LoginRequest, TokenResponse
local/ TokenLocalDataSource, WorkoutPhotoLocalDataSource, WorkoutEntityMapper
repository/ AuthRepository, AuthRepositoryImpl
Path under core/database/.../ Example
(root) RouteeDatabase (one Room DB for the app, internal)
dao/ WorkoutDao, WorkoutPhotoDao
entity/ WorkoutEntity, TrackPointEntity, WorkoutPhotoEntity

Hilt modules live in core/data/.../di/ and core/database/.../di/.

Room

Item Rule
Entity โ†” model core/data/.../<domain>/local/
Schema Commit core/database/schemas/. New version โ†’ @AutoMigration, no destructive migration
Write Insert only where a row is created, update with @Update (not @Upsert or REPLACE)

Service and DTO

internal interface AuthService {
    @POST("api/v1/auth/login")
    suspend fun login(@Body request: LoginRequest): BaseResponse<TokenResponse>
}

@Serializable
internal data class TokenResponse( // DTO๋Š” internal, core:data ๋ฐ–์œผ๋กœ ์•ˆ ๋‚˜๊ฐ
    @SerialName("accessToken") val accessToken: String,
    @SerialName("refreshToken") val refreshToken: String,
)

Repository

// ํ•จ์ˆ˜ ์ด๋ฆ„
interface RecapRepository {
    suspend fun getRecap(id: Long): Result<Recap>           // get: 1ํšŒ ์กฐํšŒ
    fun observeRecaps(): Flow<List<Recap>>                    // observe: Flow๋กœ ๊ณ„์† ๊ตฌ๋…
    suspend fun saveRecap(recap: Recap): Result<Unit>         // save, update: ์ €์žฅ, ์ˆ˜์ •
    suspend fun deleteRecap(id: Long): Result<Unit>           // delete: ํ•˜๋‚˜, clear: ์ „๋ถ€
    suspend fun shareRecap(id: Long): Result<Unit>            // ๊ทธ ์™ธ ๋™์ž‘์€ ๋„๋ฉ”์ธ ๋™์‚ฌ (login, register)
}                                                             // post, put, patch๋Š” Service์—๋งŒ

// ๊ตฌํ˜„ (core/data/.../auth)
internal class AuthRepositoryImpl @Inject constructor( // ๊ตฌํ˜„์ฒด๋Š” internal
    private val authService: AuthService,
    private val apiResponseHandler: ApiResponseHandler,
) : AuthRepository {
    override suspend fun login(idToken: String, provider: SocialProvider): Result<LoginResult> =
        apiResponseHandler.safeApiCall { authService.login(LoginRequest(idToken, provider.name)) } // runCatching ๋Œ€์‹ 
            .mapApiException(::mapAuthException)
}

Retrofit

Qualifier Access token TimeZone header For
@Auth Yes Yes Most APIs (default)
@NoAuth No Yes Login, sign-up, token reissue
@Module
@InstallIn(SingletonComponent::class)
internal abstract class RecapModule {
    @Binds @Singleton
    abstract fun bindRecapRepository(impl: RecapRepositoryImpl): RecapRepository

    companion object {
        @Provides @Singleton
        fun provideRecapService(@Auth retrofit: Retrofit): RecapService = retrofit.create()
    }
}