-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from groupeminaste/feature/cache
Setting up cache
- Loading branch information
Showing
23 changed files
with
269 additions
and
25 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
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
File renamed without changes.
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
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
14 changes: 14 additions & 0 deletions
14
shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/posts/IPostsRepository.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,14 @@ | ||
package me.nathanfallet.extopy.repositories.posts | ||
|
||
import kotlinx.datetime.Instant | ||
import me.nathanfallet.extopy.models.posts.Post | ||
|
||
interface IPostsRepository { | ||
|
||
fun save(post: Post, expiresFromCacheAt: Instant) | ||
fun get(id: String): Post? | ||
fun delete(id: String) | ||
fun deleteAll() | ||
fun deleteExpired() | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/posts/PostsRepository.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,44 @@ | ||
package me.nathanfallet.extopy.repositories.posts | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import me.nathanfallet.extopy.database.Database | ||
import me.nathanfallet.extopy.database.Posts | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.repositories.users.IUsersRepository | ||
|
||
class PostsRepository( | ||
private val database: Database, | ||
private val usersRepository: IUsersRepository, | ||
) : IPostsRepository { | ||
|
||
override fun save(post: Post, expiresFromCacheAt: Instant) = | ||
database.postsQueries.save( | ||
Posts( | ||
id = post.id, | ||
userId = post.userId, | ||
body = post.body, | ||
expiresFromCacheAt = expiresFromCacheAt.toString() | ||
) | ||
) | ||
|
||
override fun get(id: String): Post? = | ||
database.postsQueries.get(id, Clock.System.now().toString()).executeAsOneOrNull()?.let { | ||
Post( | ||
id = it.id, | ||
userId = it.userId, | ||
body = it.body, | ||
user = it.userId?.let { userId -> usersRepository.get(userId) }, | ||
) | ||
} | ||
|
||
override fun delete(id: String) = | ||
database.postsQueries.delete(id) | ||
|
||
override fun deleteAll() = | ||
database.postsQueries.deleteAll() | ||
|
||
override fun deleteExpired() = | ||
database.postsQueries.deleteExpired(Clock.System.now().toString()) | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/IUsersRepository.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,14 @@ | ||
package me.nathanfallet.extopy.repositories.users | ||
|
||
import kotlinx.datetime.Instant | ||
import me.nathanfallet.extopy.models.users.User | ||
|
||
interface IUsersRepository { | ||
|
||
fun save(user: User, expiresFromCacheAt: Instant) | ||
fun get(id: String): User? | ||
fun delete(id: String) | ||
fun deleteAll() | ||
fun deleteExpired() | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.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,47 @@ | ||
package me.nathanfallet.extopy.repositories.users | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import me.nathanfallet.extopy.database.Database | ||
import me.nathanfallet.extopy.database.Users | ||
import me.nathanfallet.extopy.models.users.User | ||
|
||
class UsersRepository( | ||
private val database: Database, | ||
) : IUsersRepository { | ||
|
||
override fun save(user: User, expiresFromCacheAt: Instant) = | ||
database.usersQueries.save( | ||
Users( | ||
id = user.id, | ||
displayName = user.displayName, | ||
username = user.username, | ||
email = user.email, | ||
biography = user.biography, | ||
avatar = user.avatar, | ||
expiresFromCacheAt = expiresFromCacheAt.toString() | ||
) | ||
) | ||
|
||
override fun get(id: String): User? = | ||
database.usersQueries.get(id, Clock.System.now().toString()).executeAsOneOrNull()?.let { | ||
User( | ||
id = it.id, | ||
displayName = it.displayName, | ||
username = it.username, | ||
email = it.email, | ||
biography = it.biography, | ||
avatar = it.avatar | ||
) | ||
} | ||
|
||
override fun delete(id: String) = | ||
database.usersQueries.delete(id) | ||
|
||
override fun deleteAll() = | ||
database.usersQueries.deleteAll() | ||
|
||
override fun deleteExpired() = | ||
database.usersQueries.deleteExpired(Clock.System.now().toString()) | ||
|
||
} |
13 changes: 12 additions & 1 deletion
13
shared/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/CreatePostUseCase.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 |
---|---|---|
@@ -1,15 +1,26 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.plus | ||
import me.nathanfallet.extopy.client.IExtopyClient | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.models.posts.PostPayload | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
|
||
class CreatePostUseCase( | ||
private val client: IExtopyClient, | ||
private val postsRepository: IPostsRepository, | ||
) : ICreatePostUseCase { | ||
|
||
override suspend fun invoke(input: PostPayload): Post? { | ||
return client.posts.create(input) | ||
return client.posts.create(input)?.also { | ||
postsRepository.save( | ||
it, | ||
Clock.System.now().plus(60, DateTimeUnit.SECOND, TimeZone.currentSystemDefault()) | ||
) | ||
} | ||
} | ||
|
||
} |
13 changes: 12 additions & 1 deletion
13
...ed/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/FetchPostRepliesUseCase.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 |
---|---|---|
@@ -1,14 +1,25 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.plus | ||
import me.nathanfallet.extopy.client.IExtopyClient | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
|
||
class FetchPostRepliesUseCase( | ||
private val client: IExtopyClient, | ||
private val postsRepository: IPostsRepository, | ||
) : IFetchPostRepliesUseCase { | ||
|
||
override suspend fun invoke(input1: String, input2: Long, input3: Long): List<Post> { | ||
return client.posts.getReplies(input1, input2, input3) | ||
return client.posts.getReplies(input1, input2, input3).onEach { | ||
postsRepository.save( | ||
it, | ||
Clock.System.now().plus(60, DateTimeUnit.SECOND, TimeZone.currentSystemDefault()) | ||
) | ||
} | ||
} | ||
|
||
} |
13 changes: 12 additions & 1 deletion
13
shared/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/FetchPostUseCase.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 |
---|---|---|
@@ -1,14 +1,25 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.plus | ||
import me.nathanfallet.extopy.client.IExtopyClient | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
|
||
class FetchPostUseCase( | ||
private val client: IExtopyClient, | ||
private val postsRepository: IPostsRepository, | ||
) : IFetchPostUseCase { | ||
|
||
override suspend fun invoke(input: String): Post? { | ||
return client.posts.get(input) | ||
return postsRepository.get(input) ?: client.posts.get(input)?.also { | ||
postsRepository.save( | ||
it, | ||
Clock.System.now().plus(60, DateTimeUnit.SECOND, TimeZone.currentSystemDefault()) | ||
) | ||
} | ||
} | ||
|
||
} |
13 changes: 12 additions & 1 deletion
13
shared/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/users/FetchUserPostsUseCase.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 |
---|---|---|
@@ -1,14 +1,25 @@ | ||
package me.nathanfallet.extopy.usecases.users | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.plus | ||
import me.nathanfallet.extopy.client.IExtopyClient | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
|
||
class FetchUserPostsUseCase( | ||
private val client: IExtopyClient, | ||
private val postsRepository: IPostsRepository, | ||
) : IFetchUserPostsUseCase { | ||
|
||
override suspend fun invoke(input1: String, input2: Long, input3: Long): List<Post> { | ||
return client.users.getPosts(input1, input2, input3) | ||
return client.users.getPosts(input1, input2, input3).onEach { | ||
postsRepository.save( | ||
it, | ||
Clock.System.now().plus(60, DateTimeUnit.SECOND, TimeZone.currentSystemDefault()) | ||
) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.