-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package me.nathanfallet.extopy.repositories.users | ||
|
||
import me.nathanfallet.extopy.models.users.User | ||
|
||
interface IUsersRepository { | ||
|
||
fun upsert(user: User) | ||
fun get(id: String): User? | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package me.nathanfallet.extopy.repositories.users | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.plus | ||
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, | ||
Check warning on line 12 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt
|
||
) : IUsersRepository { | ||
|
||
override fun upsert(user: User) = database.usersQueries.upsert( | ||
id = user.id, | ||
displayName = user.displayName, | ||
username = user.username, | ||
email = user.email, | ||
biography = user.biography, | ||
avatar = user.avatar, | ||
expiresFromCacheAt = Clock.System.now() | ||
.plus(60, DateTimeUnit.SECOND, TimeZone.currentSystemDefault()) | ||
.toString() | ||
Check warning on line 24 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt
|
||
) | ||
|
||
override fun get(id: String): User? = database.usersQueries.get(id) | ||
Check warning on line 27 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt
|
||
.executeAsOneOrNull()?.toUser() | ||
|
||
private fun Users.toUser() = User( | ||
id = id, | ||
displayName = displayName, | ||
username = username, | ||
email = email, | ||
biography = biography, | ||
avatar = avatar | ||
Check warning on line 36 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt
|
||
) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TABLE posts ( | ||
id TEXT PRIMARY KEY, | ||
userId TEXT, | ||
body TEXT | ||
); | ||
|
||
insert: | ||
INSERT INTO posts (id, userId, body) | ||
VALUES (?, ? , ?); | ||
|
||
get: | ||
SELECT * FROM posts WHERE id = ?; | ||
|
||
delete: | ||
DELETE FROM posts WHERE id = ?; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
CREATE TABLE users ( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
displayName TEXT NOT NULL, | ||
username TEXT NOT NULL, | ||
email TEXT, | ||
biography TEXT, | ||
avatar TEXT, | ||
expiresFromCacheAt TEXT NOT NULL | ||
); | ||
|
||
upsert { | ||
UPDATE users | ||
SET displayName = :displayName, | ||
username = :username, | ||
email = :email, | ||
biography = :biography, | ||
avatar = :avatar, | ||
expiresFromCacheAt = :expiresFromCacheAt | ||
WHERE id = :id; | ||
|
||
INSERT OR IGNORE INTO users (id, displayName, username, email, biography, avatar, expiresFromCacheAt) | ||
VALUES ( | ||
:id, | ||
:displayName, | ||
:username, | ||
:email, | ||
:biography, | ||
:avatar, | ||
:expiresFromCacheAt | ||
); | ||
} | ||
|
||
get: | ||
SELECT * FROM users WHERE id = ?; | ||
|
||
delete: | ||
DELETE FROM users WHERE id = ?; |