Skip to content

Commit

Permalink
feat: replaceすると順番が入れ替わってしまうことがあるので
Browse files Browse the repository at this point in the history
存在している時はupdateするようにした
  • Loading branch information
pantasystem committed May 15, 2024
1 parent 8ff14cc commit 176db83
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ interface NoteDAO {
)
suspend fun deleteReactionCountsByNoteId(noteId: String)

// delete reaction counts
@Transaction
@Update
suspend fun updateReactionCount(reactionCount: ReactionCountEntity)



@Query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,20 @@ class SQLiteNoteDataSource @Inject constructor(
val deleteList = existsNoteReactionCountMap.filter {
!reactionCountsMap.containsKey(it.key)
}.values.toList()
val insertList = reactionCountsMap.filter { (_, value) ->
value.id !in existsNoteReactionCountMap
}.values.toList()

val updateOrInsertList = reactionCountsMap.mapNotNull { (key, value) ->
val exists = existsNoteReactionCountMap[key]
if (exists == null || exists != value) {
value
} else {
null
}
}
val updateList = reactionCountsMap.filter { (_, value) ->
value.id in existsNoteReactionCountMap && value != existsNoteReactionCountMap[value.id]
}.values.toList()

withContext(ioDispatcher) {
noteDAO.insertReactionCounts(updateOrInsertList)
noteDAO.insertReactionCounts(insertList)
noteDAO.deleteReactionCounts(deleteList.map { it.id })
updateList.forEach {
noteDAO.updateReactionCount(it)
}
}

}
Expand Down

0 comments on commit 176db83

Please sign in to comment.