Find notes by tags
This commit is contained in:
parent
b5beca8661
commit
8021814c31
@ -6,7 +6,7 @@ import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
import java.util.*
|
||||
|
||||
interface NoteRepository {
|
||||
fun findAll(userId: Int, limit: Int = 20, offset: Int = 0): List<PersistedNoteMetadata>
|
||||
fun findAll(userId: Int, limit: Int = 20, offset: Int = 0, tag: String? = null): List<PersistedNoteMetadata>
|
||||
fun exists(userId: Int, uuid: UUID): Boolean
|
||||
fun create(userId: Int, note: Note): PersistedNote
|
||||
fun find(userId: Int, uuid: UUID): PersistedNote?
|
||||
|
||||
@ -4,6 +4,7 @@ import be.simplenotes.domain.model.Note
|
||||
import be.simplenotes.domain.model.PersistedNote
|
||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
import be.simplenotes.domain.usecases.repositories.NoteRepository
|
||||
import be.simplenotes.persistance.extensions.uuidBinary
|
||||
import me.liuwj.ktorm.database.*
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
@ -14,13 +15,25 @@ import kotlin.collections.HashMap
|
||||
internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
|
||||
|
||||
@Throws(IllegalArgumentException::class)
|
||||
override fun findAll(userId: Int, limit: Int, offset: Int): List<PersistedNoteMetadata> {
|
||||
override fun findAll(userId: Int, limit: Int, offset: Int, tag: String?): List<PersistedNoteMetadata> {
|
||||
require(limit > 0) { "limit should be positive" }
|
||||
require(offset >= 0) { "offset should not be negative" }
|
||||
|
||||
val notes = db.notes
|
||||
val uuids1: List<UUID>? = if (tag != null) {
|
||||
db.from(Tags)
|
||||
.leftJoin(Notes, on = Notes.uuid eq Tags.noteUuid)
|
||||
.select(Notes.uuid)
|
||||
.where { (Notes.userId eq userId) and (Tags.name eq tag) }
|
||||
.map { it[Notes.uuid]!! }
|
||||
} else null
|
||||
|
||||
var query = db.notes
|
||||
.filterColumns { listOf(it.uuid, it.title, it.updatedAt) }
|
||||
.filter { it.userId eq userId }
|
||||
|
||||
if (uuids1 != null) query = query.filter { it.uuid inList uuids1 }
|
||||
|
||||
val notes = query
|
||||
.sortedByDescending { it.updatedAt }
|
||||
.take(limit)
|
||||
.drop(offset)
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
<logger name="org.eclipse.jetty" level="INFO"/>
|
||||
<logger name="me.liuwj.ktorm.database" level="INFO"/>
|
||||
<logger name="me.liuwj.ktorm.database" level="DEBUG"/>
|
||||
<logger name="com.zaxxer.hikari" level="INFO"/>
|
||||
<logger name="org.flywaydb.core" level="INFO"/>
|
||||
</configuration>
|
||||
|
||||
@ -155,6 +155,27 @@ internal class NoteRepositoryImplTest {
|
||||
.hasSize(10)
|
||||
.allMatch { it.title.toInt() in 41..50 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `find all notes with tag`() {
|
||||
createNote(user1.id, "1", listOf("a", "b"))
|
||||
createNote(user1.id, "2")
|
||||
createNote(user1.id, "3", listOf("c"))
|
||||
createNote(user1.id, "4", listOf("c"))
|
||||
createNote(user2.id, "5", listOf("c"))
|
||||
|
||||
assertThat(noteRepo.findAll(user1.id, tag = "a"))
|
||||
.hasSize(1)
|
||||
.first()
|
||||
.hasFieldOrPropertyWithValue("title", "1")
|
||||
|
||||
assertThat(noteRepo.findAll(user1.id, tag = "c"))
|
||||
.hasSize(2)
|
||||
|
||||
assertThat(noteRepo.findAll(user2.id, tag = "c"))
|
||||
.hasSize(1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user