Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/search data #40

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.seven.colink.data.firebase.repository

import android.content.ContentValues.TAG
import android.util.Log
import com.algolia.search.saas.Index
import com.algolia.search.saas.Query
Expand All @@ -20,6 +21,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import org.json.JSONArray
import org.json.JSONObject
import javax.inject.Inject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
Expand All @@ -32,6 +34,7 @@ class PostRepositoryImpl @Inject constructor(
override suspend fun registerPost(post: PostEntity) = suspendCoroutine { continuation ->
firebaseFirestore.collection(DataBaseType.POST.title).document(post.key).set(post)
.addOnSuccessListener {
addPostToAlgolia(post)
continuation.resume(DataResultStatus.SUCCESS)
}
.addOnFailureListener { e ->
Expand Down Expand Up @@ -80,10 +83,10 @@ class PostRepositoryImpl @Inject constructor(
val algoliaQuery = Query(query)
val filters = mutableListOf<String>()
groupType?.let {
filters.add("groupType:${it}")
filters.add("groupType:\"${it}\"")
}
projectStatus?.let {
filters.add("projectStatus:${it}")
filters.add("status:\"${it}\"")
}
if (filters.isNotEmpty()) {
algoliaQuery.filters = filters.joinToString(" AND ")
Expand Down Expand Up @@ -118,20 +121,26 @@ class PostRepositoryImpl @Inject constructor(

private suspend fun fetchPostEntity(key: String): PostEntity? = getPost(key).getOrNull()

override suspend fun getRecentPost(count: Int) =
firebaseFirestore.collection(DataBaseType.POST.title)
override suspend fun getRecentPost(count: Int, groupType: GroupType?): List<PostEntity> {
var query = firebaseFirestore.collection(DataBaseType.POST.title)
.orderBy("registeredDate", com.google.firebase.firestore.Query.Direction.DESCENDING)
.limit(count.toLong())
.get().await()
.documents.mapNotNull {
it.toObject(PostEntity::class.java)
}

if (groupType != null) {
query = query.whereEqualTo("groupType", groupType.name)
}

return query.get().await().documents.mapNotNull { snapshot ->
snapshot.toObject(PostEntity::class.java)
}
}

override suspend fun updatePost(key: String, updatedPost: PostEntity) =
suspendCoroutine { continuation ->
firebaseFirestore.collection(DataBaseType.POST.title).document(key)
.set(updatedPost, com.google.firebase.firestore.SetOptions.merge())
.addOnSuccessListener {
addPostToAlgolia(updatedPost)
continuation.resume(DataResultStatus.SUCCESS)
}
.addOnFailureListener { e ->
Expand All @@ -153,5 +162,24 @@ class PostRepositoryImpl @Inject constructor(
}
}
}

private fun addPostToAlgolia(post: PostEntity) = CoroutineScope(Dispatchers.IO).launch {
try {
val postJson = JSONObject().apply {
put("objectID", post.key)
put("description", post.description)
put("title", post.title)
put("tags", JSONArray(post.tags))
put("groupType", post.groupType)
put("status", post.status)
}
algolia.addObjectAsync(postJson) { _, exception ->
if (exception != null) {
}
}
} catch (e: Exception){
Log.e(TAG, "$e")
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ interface PostRepository {
projectStatus: ProjectStatus? = null
): List<PostEntity>

suspend fun getRecentPost(count: Int): List<PostEntity>
suspend fun updatePost(key: String, updatedPost: PostEntity): DataResultStatus
suspend fun incrementPostViews(key: String): DataResultStatus

suspend fun getRecentPost(count: Int, groupType: GroupType?= null): List<PostEntity>
}
Loading