-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add UserTagRepository (api, impl, test)
- Loading branch information
Showing
2 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
...livefast/eattrash/raccoonforlemmy/core/persistence/repository/DefaultUserTagRepository.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,123 @@ | ||
package com.livefast.eattrash.raccoonforlemmy.core.persistence.repository | ||
|
||
import com.livefast.eattrash.raccoonforlemmy.core.persistence.UserTagEntity | ||
import com.livefast.eattrash.raccoonforlemmy.core.persistence.UserTagMemberEntity | ||
import com.livefast.eattrash.raccoonforlemmy.core.persistence.data.UserTagMemberModel | ||
import com.livefast.eattrash.raccoonforlemmy.core.persistence.data.UserTagModel | ||
import com.livefast.eattrash.raccoonforlemmy.core.persistence.provider.DatabaseProvider | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.withContext | ||
|
||
internal class DefaultUserTagRepository( | ||
provider: DatabaseProvider, | ||
) : UserTagRepository { | ||
private val db = provider.getDatabase() | ||
|
||
override suspend fun getAll(accountId: Long): List<UserTagModel> = | ||
withContext(Dispatchers.IO) { | ||
db.usertagsQueries | ||
.getAllBy(accountId) | ||
.executeAsList() | ||
.map { it.toModel() } | ||
} | ||
|
||
override suspend fun getMembers(tagId: Long): List<UserTagMemberModel> = | ||
withContext(Dispatchers.IO) { | ||
db.usertagmembersQueries | ||
.getMembers(tagId) | ||
.executeAsList() | ||
.map { it.toModel() } | ||
} | ||
|
||
override suspend fun getTags( | ||
username: String, | ||
accountId: Long, | ||
): List<UserTagModel> = | ||
withContext(Dispatchers.IO) { | ||
db.usertagmembersQueries | ||
.getAllBy(username, accountId) | ||
.executeAsList() | ||
.map { e -> | ||
UserTagModel( | ||
name = e.name, | ||
id = e.user_tag_id ?: 0, | ||
) | ||
} | ||
} | ||
|
||
override suspend fun create( | ||
model: UserTagModel, | ||
accountId: Long, | ||
) = withContext(Dispatchers.IO) { | ||
db.usertagsQueries.create( | ||
name = model.name, | ||
account_id = accountId, | ||
) | ||
} | ||
|
||
override suspend fun update( | ||
id: Long, | ||
name: String, | ||
) = withContext(Dispatchers.IO) { | ||
db.usertagsQueries.update( | ||
id = id, | ||
name = name, | ||
) | ||
} | ||
|
||
override suspend fun delete(id: Long) = | ||
withContext(Dispatchers.IO) { | ||
db.usertagsQueries.delete(id) | ||
} | ||
|
||
override suspend fun addMember( | ||
username: String, | ||
userTagId: Long, | ||
) = withContext(Dispatchers.IO) { | ||
db.usertagmembersQueries.create( | ||
username = username, | ||
user_tag_id = userTagId, | ||
) | ||
} | ||
|
||
override suspend fun removeMember( | ||
username: String, | ||
userTagId: Long, | ||
) = withContext(Dispatchers.IO) { | ||
db.usertagmembersQueries.delete( | ||
username = username, | ||
user_tag_id = userTagId, | ||
) | ||
} | ||
|
||
override suspend fun getBelonging( | ||
accountId: Long, | ||
username: String, | ||
): List<UserTagModel> = | ||
withContext(Dispatchers.IO) { | ||
db.usertagmembersQueries | ||
.getAllBy( | ||
username = username, | ||
account_id = accountId, | ||
).executeAsList() | ||
.map { e -> | ||
UserTagModel( | ||
name = e.name, | ||
id = e.user_tag_id ?: 0, | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun UserTagEntity.toModel() = | ||
UserTagModel( | ||
id = id, | ||
name = name, | ||
) | ||
|
||
private fun UserTagMemberEntity.toModel() = | ||
UserTagMemberModel( | ||
username = username, | ||
userTagId = user_tag_id ?: 0, | ||
) |
42 changes: 42 additions & 0 deletions
42
...in/com/livefast/eattrash/raccoonforlemmy/core/persistence/repository/UserTagRepository.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,42 @@ | ||
package com.livefast.eattrash.raccoonforlemmy.core.persistence.repository | ||
|
||
import com.livefast.eattrash.raccoonforlemmy.core.persistence.data.UserTagMemberModel | ||
import com.livefast.eattrash.raccoonforlemmy.core.persistence.data.UserTagModel | ||
|
||
interface UserTagRepository { | ||
suspend fun getAll(accountId: Long): List<UserTagModel> | ||
|
||
suspend fun getMembers(tagId: Long): List<UserTagMemberModel> | ||
|
||
suspend fun getTags( | ||
username: String, | ||
accountId: Long, | ||
): List<UserTagModel> | ||
|
||
suspend fun create( | ||
model: UserTagModel, | ||
accountId: Long, | ||
) | ||
|
||
suspend fun update( | ||
id: Long, | ||
name: String, | ||
) | ||
|
||
suspend fun delete(id: Long) | ||
|
||
suspend fun addMember( | ||
username: String, | ||
userTagId: Long, | ||
) | ||
|
||
suspend fun removeMember( | ||
username: String, | ||
userTagId: Long, | ||
) | ||
|
||
suspend fun getBelonging( | ||
accountId: Long, | ||
username: String, | ||
): List<UserTagModel> | ||
} |