Skip to content

Commit

Permalink
Merge pull request #171 from 2rabs/rt/local-datasource
Browse files Browse the repository at this point in the history
✨ Repository に Dao を追加
  • Loading branch information
tatsutakein authored Jan 2, 2024
2 parents cd9f971 + cc52412 commit 7d0357e
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
package club.nito.core.data

import club.nito.core.database.participant.ParticipantDao
import club.nito.core.model.participant.Participant
import club.nito.core.model.participant.ParticipantDeclaration
import club.nito.core.model.participant.ParticipantStatus
import club.nito.core.model.participant.ParticipantUser
import club.nito.core.model.schedule.ScheduleId
import club.nito.core.network.participation.ParticipantRemoteDataSource
import kotlinx.coroutines.flow.Flow

public class DefaultParticipantRepository(
private val remoteDataSource: ParticipantRemoteDataSource,
private val dao: ParticipantDao,
) : ParticipantRepository {
override fun participantUsersStream(scheduleId: String): Flow<List<ParticipantUser>> =
dao.participantUsersStream(scheduleId = scheduleId)

override suspend fun getParticipants(scheduleId: String): List<Participant> =
remoteDataSource.getParticipants(scheduleId = scheduleId)
remoteDataSource.getParticipants(scheduleId = scheduleId).also {
dao.upsert(entities = it)
}

override suspend fun getParticipants(scheduleIds: List<String>): List<Participant> =
remoteDataSource.getParticipants(scheduleIds = scheduleIds)
remoteDataSource.getParticipants(scheduleIds = scheduleIds).also {
dao.upsert(entities = it)
}

override suspend fun existParticipantByUserId(scheduleId: ScheduleId, userId: String): Boolean =
remoteDataSource.existParticipantByUserId(scheduleId = scheduleId, userId = userId)

override suspend fun fetchParticipantStatus(scheduleId: ScheduleId, userId: String): ParticipantStatus =
remoteDataSource.fetchParticipantStatus(scheduleId = scheduleId, userId = userId)

override suspend fun insertParticipate(declaration: ParticipantDeclaration): Participant =
remoteDataSource.insertParticipate(declaration = declaration)
override suspend fun insertParticipate(declaration: ParticipantDeclaration): Participant {
val participant = remoteDataSource.insertParticipate(declaration = declaration)
dao.upsert(participant)
return participant
}

override suspend fun updateParticipate(declaration: ParticipantDeclaration): Participant =
remoteDataSource.updateParticipate(declaration = declaration)
override suspend fun updateParticipate(declaration: ParticipantDeclaration): Participant {
val participant = remoteDataSource.updateParticipate(declaration = declaration)
dao.upsert(participant)
return participant
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package club.nito.core.data

import club.nito.core.database.place.PlaceDao
import club.nito.core.model.place.Place
import club.nito.core.model.place.PlaceId
import club.nito.core.network.place.PlaceRemoteDataSource
import kotlinx.coroutines.flow.Flow

public class DefaultPlaceRepository(
private val remoteDataSource: PlaceRemoteDataSource,
private val placeDao: PlaceDao,
) : PlaceRepository {
override fun placeStream(placeId: PlaceId): Flow<Place?> = placeDao.placeStream(placeId)

override suspend fun fetchPlaceList(vararg ids: PlaceId): List<Place> {
return remoteDataSource.fetchPlaceList(ids.toList())
return remoteDataSource.fetchPlaceList(ids.toList()).also {
placeDao.upsert(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package club.nito.core.data

import club.nito.core.database.profile.ProfileDao
import club.nito.core.model.FetchSingleContentResult
import club.nito.core.model.UserProfile
import club.nito.core.network.user.UserRemoteDataSource
import kotlinx.coroutines.flow.Flow

public class DefaultUserRepository(
private val remoteDataSource: UserRemoteDataSource,
private val profileDao: ProfileDao,
) : UserRepository {
override fun profilesStream(userIds: List<String>): Flow<List<UserProfile>> =
profileDao.profilesStream(userIds = userIds)

override suspend fun getProfile(userId: String): FetchSingleContentResult<UserProfile> {
val profile = remoteDataSource.getProfile(userId = userId)
return profile?.let { FetchSingleContentResult.Success(it) }
?: FetchSingleContentResult.NoContent
}

override suspend fun getProfiles(userIds: List<String>): List<UserProfile> {
return remoteDataSource.getProfiles(userIds = userIds)
}
override suspend fun getProfiles(userIds: List<String>): List<UserProfile> = remoteDataSource
.getProfiles(userIds = userIds)
.also(profileDao::upsert)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package club.nito.core.data

import club.nito.core.database.schedule.ScheduleDao
import club.nito.core.model.Order
import club.nito.core.model.schedule.Schedule
import club.nito.core.model.schedule.ScheduleId
import club.nito.core.model.schedule.ScheduleWithPlace
import club.nito.core.network.schedule.ScheduleRemoteDataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.datetime.Instant

public class OfflineFirstScheduleRepository(
private val remoteDataSource: ScheduleRemoteDataSource,
private val scheduleDao: ScheduleDao,
) : ScheduleRepository {
override val scheduleListFlow: Flow<List<Schedule>> = flow {
// TODO: LocalDataSource
emit(remoteDataSource.getScheduleList(limit = 10))
}
override val scheduleListFlow: Flow<List<Schedule>>
get() = scheduleDao.schedulesStream

override suspend fun getScheduleList(
limit: Int,
Expand All @@ -24,7 +24,19 @@ public class OfflineFirstScheduleRepository(
limit = limit,
order = order,
after = after,
)
).also {
scheduleDao.upsert(it)
}

override fun scheduleStream(id: ScheduleId): Flow<Schedule?> = scheduleDao.scheduleStream(scheduleId = id)

override fun scheduleWithPlaceStream(id: ScheduleId): Flow<ScheduleWithPlace?> {
return scheduleDao.scheduleWithPlaceStream(scheduleId = id)
}

override suspend fun fetchSchedule(id: ScheduleId): Schedule = remoteDataSource.fetchSchedule(id = id)
override suspend fun fetchSchedule(id: ScheduleId): Schedule = remoteDataSource
.fetchSchedule(id = id)
.also {
scheduleDao.upsert(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ package club.nito.core.data
import club.nito.core.model.participant.Participant
import club.nito.core.model.participant.ParticipantDeclaration
import club.nito.core.model.participant.ParticipantStatus
import club.nito.core.model.participant.ParticipantUser
import club.nito.core.model.schedule.ScheduleId
import kotlinx.coroutines.flow.Flow

/**
* 参加情報を扱うリポジトリ
*/
public sealed interface ParticipantRepository {
/**
* 該当の予定の参加者情報のストリームを取得する
*
* @param scheduleId 参加者情報を取得するスケジュールID
*/
public fun participantUsersStream(scheduleId: String): Flow<List<ParticipantUser>>

/**
* 該当の予定の参加情報を取得する
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package club.nito.core.data

import club.nito.core.model.place.Place
import club.nito.core.model.place.PlaceId
import kotlinx.coroutines.flow.Flow

/**
* 場所に関するリポジトリ
*/
public sealed interface PlaceRepository {
/**
* 場所のストリームを取得する
* @param placeId 取得する場所のID
*/
public fun placeStream(placeId: PlaceId): Flow<Place?>

/**
* 場所を取得する
* @param ids 取得する場所のID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@ package club.nito.core.data
import club.nito.core.model.Order
import club.nito.core.model.schedule.Schedule
import club.nito.core.model.schedule.ScheduleId
import club.nito.core.model.schedule.ScheduleWithPlace
import kotlinx.coroutines.flow.Flow
import kotlinx.datetime.Instant

/**
* スケジュールに関するリポジトリ
*/
public sealed interface ScheduleRepository {
/**
* スケジュール一覧のストリームを取得する
*/
public val scheduleListFlow: Flow<List<Schedule>>

/**
* スケジュール一覧を取得する
*/
public suspend fun getScheduleList(
limit: Int,
order: Order = Order.DESCENDING,
after: Instant? = null,
): List<Schedule>

/**
* 該当スケジュールのストリームを取得する
*/
public fun scheduleStream(id: ScheduleId): Flow<Schedule?>

/**
* 該当スケジュールのストリームを取得する
*/
public fun scheduleWithPlaceStream(id: ScheduleId): Flow<ScheduleWithPlace?>

/**
* スケジュールを取得する
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package club.nito.core.data

import club.nito.core.model.FetchSingleContentResult
import club.nito.core.model.UserProfile
import kotlinx.coroutines.flow.Flow

/**
* ユーザープロフィールに関するリポジトリ
*/
public sealed interface UserRepository {
/**
* ユーザープロフィールのストリームを取得する
*/
public fun profilesStream(userIds: List<String>): Flow<List<UserProfile>>

/**
* ユーザープロフィールを取得する
*/
Expand Down

0 comments on commit 7d0357e

Please sign in to comment.