From 8021814c3190597f83fef14dedf630e7214c8ce7 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Mon, 17 Aug 2020 16:44:16 +0200 Subject: [PATCH] Find notes by tags --- .../usecases/repositories/NoteRepository.kt | 2 +- .../main/kotlin/notes/NoteRepositoryImpl.kt | 17 +++++++++++++-- persistance/src/main/resources/logback.xml | 2 +- .../kotlin/notes/NoteRepositoryImplTest.kt | 21 +++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt b/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt index 734b045..5054802 100644 --- a/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt +++ b/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt @@ -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 + fun findAll(userId: Int, limit: Int = 20, offset: Int = 0, tag: String? = null): List fun exists(userId: Int, uuid: UUID): Boolean fun create(userId: Int, note: Note): PersistedNote fun find(userId: Int, uuid: UUID): PersistedNote? diff --git a/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt b/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt index 7cd6be7..e376aad 100644 --- a/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt +++ b/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt @@ -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 { + override fun findAll(userId: Int, limit: Int, offset: Int, tag: String?): List { require(limit > 0) { "limit should be positive" } require(offset >= 0) { "offset should not be negative" } - val notes = db.notes + val uuids1: List? = 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) diff --git a/persistance/src/main/resources/logback.xml b/persistance/src/main/resources/logback.xml index c0633b0..5024926 100644 --- a/persistance/src/main/resources/logback.xml +++ b/persistance/src/main/resources/logback.xml @@ -10,7 +10,7 @@ - + diff --git a/persistance/src/test/kotlin/notes/NoteRepositoryImplTest.kt b/persistance/src/test/kotlin/notes/NoteRepositoryImplTest.kt index ee3dd0d..9ba0e03 100644 --- a/persistance/src/test/kotlin/notes/NoteRepositoryImplTest.kt +++ b/persistance/src/test/kotlin/notes/NoteRepositoryImplTest.kt @@ -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