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

change getting current logged in user poems to paged data and flow #18

Open
github-actions bot opened this issue Oct 17, 2022 · 0 comments
Open
Labels
todo This is something to do

Comments

@github-actions
Copy link

// TODO: change getting current logged in user poems to paged data and flow


package com.mambo.poetree.data.repositories

import com.mambo.poetree.data.domain.Poem
import com.mambo.poetree.data.local.PoetreeDatabase
import com.mambo.poetree.data.local.entity.Bookmarked
import com.mambo.poetree.data.local.entity.Drafted
import com.mambo.poetree.data.local.preferences.UserPreferences
import com.mambo.poetree.data.remote.CreatePoemRequest
import com.mambo.poetree.data.remote.EditPoemRequest
import com.mambo.poetree.data.remote.PoemRequest
import com.mambo.poetree.data.remote.PoemsApi
import io.realm.kotlin.ext.asFlow
import io.realm.kotlin.notifications.ObjectChange
import kotlinx.coroutines.flow.Flow

class PoemRepository {

    private val poemsApi: PoemsApi = PoemsApi()

    private val realm = PoetreeDatabase.realm()

    /**
     * DRAFTS
     */

    suspend fun getDrafts() = realm.write { query(clazz = Drafted::class).find() }.asFlow()

    suspend fun saveDraft(poem: Drafted) {
        realm.write {
            copyToRealm(poem)
        }
    }

    suspend fun updateDraft(poem: Drafted) {
        realm.write {
            val item = query<Drafted>(clazz = Drafted::class, "id == ${poem.id}").first().find()
            item?.apply {
                title = poem.title
                content = poem.content
                html = poem.html
                topic = poem.topic
            }
        }
    }

    suspend fun getDraft(poem: Poem): Flow<ObjectChange<Drafted>> {
        return realm.write {
            query<Drafted>(clazz = Drafted::class, "id == ${poem.id}").find().first()
        }.asFlow()
    }

    suspend fun deleteDraft(poem: Drafted) {
        realm.write {
            val item = query<Drafted>(clazz = Drafted::class, "id == ${poem.id}").find().first()
            delete(item)
        }
    }

    suspend fun deleteDrafts() {
        realm.write {
            val drafts = query(clazz = Drafted::class).find()
            delete(drafts)
        }
    }

    /**
     * BOOKMARKS
     */

    suspend fun getBookmarks() = realm.write { query(clazz = Bookmarked::class).find() }.asFlow()

    suspend fun saveBookmark(poem: Bookmarked) {
        realm.write {
            copyToRealm(poem)
        }
    }

    suspend fun updateBookmark(poem: Bookmarked) {
        realm.write {
            val item =
                query<Bookmarked>(clazz = Bookmarked::class, "id == ${poem.id}").first().find()
            item?.apply {
                title = poem.title
                content = poem.content
                html = poem.html
                topic = poem.topic
                updatedAt = poem.updatedAt
                topic = poem.topic
                reads = poem.reads
                read = poem.read
                bookmarks = poem.bookmarks
                bookmarked = poem.bookmarked
                likes = poem.likes
                liked = poem.liked
                comments = poem.comments
                commented = poem.commented
            }
        }
    }

    suspend fun getBookmark(poem: Bookmarked): Flow<ObjectChange<Bookmarked>> {
        return realm.write {
            query<Bookmarked>(clazz = Bookmarked::class, "id == ${poem.id}").find().first()
        }.asFlow()
    }

    suspend fun deleteBookmark(poem: Bookmarked) {
        realm.write {
            val item =
                query<Bookmarked>(clazz = Bookmarked::class, "id == ${poem.id}").find().first()
            delete(item)
        }
    }

    suspend fun deleteBookmarks() {
        realm.write {
            val drafts = query(clazz = Bookmarked::class).find()
            delete(drafts)
        }
    }

    /**
     * REMOTE
     */

    suspend fun createPublished(request: CreatePoemRequest) = poemsApi.createPoem(request)

    suspend fun updatePublished(request: EditPoemRequest) = poemsApi.updatePoem(request)

    suspend fun deletePublished(poemId: String) = poemsApi.deletePoem(poemId = poemId)

    suspend fun getPublished(poemId: String) = poemsApi.getPoem(PoemRequest(poemId))

    suspend fun markAsRead(poemId: String) = poemsApi.markPoemAsRead(poemId)

    suspend fun bookmark(poemId: String) = poemsApi.bookmarkPoem(poemId = poemId)

    suspend fun unBookmark(poemId: String) = poemsApi.unBookmarkPoem(poemId = poemId)

    // TODO: change getting remote poems to paged data and flow
    suspend fun getPoems(page: Int) = poemsApi.getPoems(page = page)

    // TODO: change getting searched poems to paged data and flow
    suspend fun searchPoems(topicId: Int?, query: String = "", page: Int = 1) =
        poemsApi.searchPoems(topicId = topicId, query = query, page = page)

    // TODO: change getting current logged in user poems to paged data and flow
    suspend fun getMyPoems() {
        val id = UserPreferences().getUserData()?.id ?: ""
        poemsApi.getUser(userId = id)
    }

    // TODO: update getting user poems to paged data and flow
    suspend fun getUserPoems(userId: String, page: Int = 1) =
        poemsApi.getUserPoems(userId = userId, page = page)

}

@github-actions github-actions bot added the todo This is something to do label Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
todo This is something to do
Projects
None yet
Development

No branches or pull requests

0 participants