Skip to content

Commit 7cb070e

Browse files
committed
UserProfileRepository interface
KeyValueRepository is generic interface, not specific to UserProfileRepository
1 parent 8e82b68 commit 7cb070e

File tree

7 files changed

+108
-108
lines changed

7 files changed

+108
-108
lines changed

src/app/src/androidTest/java/com/couchbase/learningpath/DatabaseIntegrationTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.couchbase.learningpath.data.DatabaseManager
88
import com.couchbase.learningpath.data.audits.AuditRepositoryDb
99
import com.couchbase.learningpath.data.project.ProjectRepositoryDb
1010
import com.couchbase.learningpath.data.stockItem.StockItemRepositoryDb
11-
import com.couchbase.learningpath.data.userprofile.UserProfileRepository
11+
import com.couchbase.learningpath.data.userprofile.UserProfileRepositoryDb
1212
import com.couchbase.learningpath.data.warehouse.WarehouseRepositoryDb
1313
import com.couchbase.learningpath.models.*
1414
import com.couchbase.learningpath.services.MockAuthenticationService
@@ -34,7 +34,7 @@ class DatabaseIntegrationTests {
3434
private lateinit var warehouseRepository: WarehouseRepositoryDb
3535
private lateinit var auditRepository: AuditRepositoryDb
3636
private lateinit var stockItemRepository: StockItemRepositoryDb
37-
private lateinit var userProfileRepository: UserProfileRepository
37+
private lateinit var userProfileRepository: UserProfileRepositoryDb
3838

3939
//test users
4040
private lateinit var user1: User
@@ -87,7 +87,7 @@ class DatabaseIntegrationTests {
8787
auditRepository = AuditRepositoryDb(authenticationService, databaseManager)
8888
stockItemRepository = StockItemRepositoryDb(databaseManager)
8989
warehouseRepository = WarehouseRepositoryDb(databaseManager)
90-
userProfileRepository = UserProfileRepository(databaseManager)
90+
userProfileRepository = UserProfileRepositoryDb(databaseManager)
9191
projectRepository = ProjectRepositoryDb(
9292
authenticationService = authenticationService,
9393
warehouseRepository = warehouseRepository,

src/app/src/main/java/com/couchbase/learningpath/InventoryApplication.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import org.koin.core.logger.Level
1010
import org.koin.core.module.Module
1111
import org.koin.dsl.module
1212

13-
import com.couchbase.learningpath.data.KeyValueRepository
1413
import com.couchbase.learningpath.data.audits.AuditRepository
1514
import com.couchbase.learningpath.data.audits.AuditRepositoryDb
1615
import com.couchbase.learningpath.data.project.ProjectRepository
1716
import com.couchbase.learningpath.data.project.ProjectRepositoryDb
1817
import com.couchbase.learningpath.data.stockItem.StockItemRepository
1918
import com.couchbase.learningpath.data.stockItem.StockItemRepositoryDb
2019
import com.couchbase.learningpath.data.userprofile.UserProfileRepository
20+
import com.couchbase.learningpath.data.userprofile.UserProfileRepositoryDb
2121
import com.couchbase.learningpath.data.warehouse.WarehouseRepository
2222
import com.couchbase.learningpath.data.warehouse.WarehouseRepositoryDb
2323
import com.couchbase.learningpath.services.AuthenticationService
@@ -74,7 +74,7 @@ class InventoryApplication
7474
singleOf(::DatabaseManager)
7575
singleOf(::MockAuthenticationService) bind AuthenticationService::class
7676
singleOf(::ReplicatorServiceDb) bind ReplicatorService::class
77-
singleOf(::UserProfileRepository) bind KeyValueRepository::class
77+
singleOf(::UserProfileRepositoryDb) bind UserProfileRepository::class
7878
singleOf(::WarehouseRepositoryDb) bind WarehouseRepository::class
7979
singleOf(::ProjectRepositoryDb) bind ProjectRepository::class
8080
singleOf(::StockItemRepositoryDb) bind StockItemRepository::class

src/app/src/main/java/com/couchbase/learningpath/data/KeyValueRepository.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package com.couchbase.learningpath.data
22

33
interface KeyValueRepository {
44

5-
fun inventoryDatabaseName(): String
6-
fun inventoryDatabaseLocation(): String?
7-
85
suspend fun count(): Int
9-
suspend fun get(currentUser: String): Map<String, Any>
6+
suspend fun get(key: String): Map<String, Any?>
107
suspend fun save(data: Map<String, Any>) : Boolean
118
}
Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,10 @@
11
package com.couchbase.learningpath.data.userprofile
22

3-
import com.couchbase.lite.CouchbaseLiteException
4-
import com.couchbase.lite.MutableDocument
5-
6-
import com.couchbase.learningpath.data.DatabaseManager
73
import com.couchbase.learningpath.data.KeyValueRepository
8-
import kotlinx.coroutines.Dispatchers
9-
import kotlinx.coroutines.withContext
10-
11-
class UserProfileRepository(
12-
private val databaseManager: DatabaseManager
13-
) : KeyValueRepository {
14-
private val userProfileType = "user"
15-
16-
override fun inventoryDatabaseName(): String {
17-
return databaseManager.currentInventoryDatabaseName
18-
}
19-
20-
override fun inventoryDatabaseLocation(): String? {
21-
return databaseManager.inventoryDatabase?.path
22-
}
23-
24-
override suspend fun get(currentUser: String): Map<String, Any> {
25-
return withContext(Dispatchers.IO) {
26-
val results = HashMap<String, Any>() // <1>
27-
results["email"] = currentUser as Any // <2>
28-
29-
val database = databaseManager.inventoryDatabase
30-
database?.let { db ->
31-
val documentId = getCurrentUserDocumentId(currentUser)
32-
val doc = db.getDocument(documentId) // <3>
33-
if (doc != null) {
34-
if (doc.contains("givenName")) { // <4>
35-
results["givenName"] = doc.getString("givenName") as Any // <4>
36-
}
37-
if (doc.contains("surname")) { // <4>
38-
results["surname"] = doc.getString("surname") as Any // <4>
39-
}
40-
if (doc.contains("jobTitle")) { // <4>
41-
results["jobTitle"] = doc.getString("jobTitle") as Any // <4>
42-
}
43-
if (doc.contains("team")) { // <4>
44-
results["team"] = doc.getString("team") as Any // <4>
45-
}
46-
if (doc.contains("imageData")) { // <4>
47-
results["imageData"] = doc.getBlob("imageData") as Any // <4>
48-
}
49-
}
50-
}
51-
return@withContext results // <5>
52-
}
53-
}
54-
55-
override suspend fun save(data: Map<String, Any>): Boolean {
56-
return withContext(Dispatchers.IO) {
57-
val email = data["email"] as String
58-
val documentId = getCurrentUserDocumentId(email)
59-
val mutableDocument = MutableDocument(documentId, data)
60-
try {
61-
val database = databaseManager.inventoryDatabase
62-
database?.save(mutableDocument)
63-
} catch (e: CouchbaseLiteException) {
64-
android.util.Log.e(e.message, e.stackTraceToString())
65-
return@withContext false
66-
}
67-
return@withContext true
68-
}
69-
}
70-
71-
override suspend fun count(): Int {
72-
return withContext(Dispatchers.IO) {
73-
val database = databaseManager.inventoryDatabase
74-
database?.let { db ->
75-
val query = "SELECT COUNT(*) AS count FROM _ WHERE documentType='$userProfileType'"
76-
val results = db.createQuery(query).execute().allResults()
77-
return@withContext results[0].getInt("count")
78-
}
79-
return@withContext 0
80-
}
81-
}
824

83-
suspend fun delete(documentId: String): Boolean {
84-
return withContext(Dispatchers.IO) {
85-
var result = false
86-
val database = databaseManager.inventoryDatabase
87-
database?.let { db ->
88-
val document = db.getDocument(documentId)
89-
document?.let {
90-
db.delete(it)
91-
result = true
92-
}
93-
}
94-
return@withContext result
95-
}
96-
}
5+
interface UserProfileRepository : KeyValueRepository {
976

98-
private fun getCurrentUserDocumentId(currentUser: String): String {
99-
return "user::${currentUser}"
100-
}
7+
fun inventoryDatabaseName(): String
8+
fun inventoryDatabaseLocation(): String?
9+
suspend fun delete(documentId: String): Boolean
10110
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.couchbase.learningpath.data.userprofile
2+
3+
import com.couchbase.lite.CouchbaseLiteException
4+
import com.couchbase.lite.MutableDocument
5+
6+
import com.couchbase.learningpath.data.DatabaseManager
7+
import com.couchbase.lite.documentChangeFlow
8+
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.flow.Flow
10+
import kotlinx.coroutines.flow.emptyFlow
11+
import kotlinx.coroutines.flow.map
12+
import kotlinx.coroutines.withContext
13+
14+
class UserProfileRepositoryDb(
15+
private val databaseManager: DatabaseManager
16+
) : UserProfileRepository {
17+
private val userProfileType = "user"
18+
19+
override fun inventoryDatabaseName(): String {
20+
return databaseManager.currentInventoryDatabaseName
21+
}
22+
23+
override fun inventoryDatabaseLocation(): String? {
24+
return databaseManager.inventoryDatabase?.path
25+
}
26+
27+
override suspend fun get(key: String): Map<String, Any?> {
28+
return withContext(Dispatchers.IO) {
29+
val results = HashMap<String, Any?>() // <1>
30+
results["email"] = key // <2>
31+
32+
val database = databaseManager.inventoryDatabase
33+
database?.let { db ->
34+
val documentId = getCurrentUserDocumentId(key)
35+
val doc = db.getDocument(documentId) // <3>
36+
if (doc != null) {
37+
results["givenName"] = doc.getString("givenName") // <4>
38+
results["surname"] = doc.getString("surname") // <4>
39+
results["jobTitle"] = doc.getString("jobTitle") // <4>
40+
results["team"] = doc.getString("team") // <4>
41+
results["imageData"] = doc.getBlob("imageData") // <4>
42+
}
43+
}
44+
results // <5>
45+
}
46+
}
47+
48+
override suspend fun save(data: Map<String, Any>): Boolean {
49+
return withContext(Dispatchers.IO) {
50+
val email = data["email"] as String
51+
val documentId = getCurrentUserDocumentId(email)
52+
val mutableDocument = MutableDocument(documentId, data)
53+
try {
54+
val database = databaseManager.inventoryDatabase
55+
database?.save(mutableDocument)
56+
} catch (e: CouchbaseLiteException) {
57+
android.util.Log.e(e.message, e.stackTraceToString())
58+
return@withContext false
59+
}
60+
return@withContext true
61+
}
62+
}
63+
64+
override suspend fun count(): Int {
65+
return withContext(Dispatchers.IO) {
66+
val database = databaseManager.inventoryDatabase
67+
database?.let { db ->
68+
val query = "SELECT COUNT(*) AS count FROM _ WHERE documentType='$userProfileType'"
69+
val results = db.createQuery(query).execute().allResults()
70+
return@withContext results[0].getInt("count")
71+
}
72+
return@withContext 0
73+
}
74+
}
75+
76+
override suspend fun delete(documentId: String): Boolean {
77+
return withContext(Dispatchers.IO) {
78+
var result = false
79+
val database = databaseManager.inventoryDatabase
80+
database?.let { db ->
81+
val document = db.getDocument(documentId)
82+
document?.let {
83+
db.delete(it)
84+
result = true
85+
}
86+
}
87+
return@withContext result
88+
}
89+
}
90+
91+
private fun getCurrentUserDocumentId(currentUser: String): String {
92+
return "user::${currentUser}"
93+
}
94+
}

src/app/src/main/java/com/couchbase/learningpath/ui/developer/DevDatabaseInfoViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import kotlinx.coroutines.Dispatchers
99
import kotlinx.coroutines.launch
1010
import kotlinx.coroutines.withContext
1111

12-
import com.couchbase.learningpath.data.KeyValueRepository
1312
import com.couchbase.learningpath.data.audits.AuditRepository
1413
import com.couchbase.learningpath.data.project.ProjectRepository
1514
import com.couchbase.learningpath.data.stockItem.StockItemRepository
15+
import com.couchbase.learningpath.data.userprofile.UserProfileRepository
1616
import com.couchbase.learningpath.data.warehouse.WarehouseRepository
1717
import com.couchbase.learningpath.services.AuthenticationService
1818
import kotlinx.coroutines.ExperimentalCoroutinesApi
1919

2020
@kotlinx.serialization.ExperimentalSerializationApi
2121
class DevDatabaseInfoViewModel(
22-
private val userProfileRepository: KeyValueRepository,
22+
private val userProfileRepository: UserProfileRepository,
2323
private val warehouseRepository: WarehouseRepository,
2424
private val projectRepository: ProjectRepository,
2525
private val auditRepository: AuditRepository,

src/app/src/main/java/com/couchbase/learningpath/ui/profile/UserProfileViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import kotlinx.coroutines.launch
1616
import kotlinx.coroutines.withContext
1717
import java.io.ByteArrayOutputStream
1818

19-
import com.couchbase.learningpath.data.KeyValueRepository
2019
import com.couchbase.learningpath.services.AuthenticationService
2120
import com.couchbase.learningpath.R
21+
import com.couchbase.learningpath.data.userprofile.UserProfileRepository
2222
import com.couchbase.learningpath.models.User
2323

2424
class UserProfileViewModel(
2525
application: Application,
26-
private val repository: KeyValueRepository,
26+
private val repository: UserProfileRepository,
2727
private val authService: AuthenticationService
2828
) : AndroidViewModel(application) {
2929

0 commit comments

Comments
 (0)