diff --git a/app/src/main/kotlin/controllers/NoteController.kt b/app/src/main/kotlin/controllers/NoteController.kt index e9ad5dc..5bc2f1c 100644 --- a/app/src/main/kotlin/controllers/NoteController.kt +++ b/app/src/main/kotlin/controllers/NoteController.kt @@ -46,8 +46,9 @@ class NoteController( fun list(request: Request, jwtPayload: JwtPayload): Response { val currentPage = request.query("page")?.toIntOrNull()?.let(::abs) ?: 1 - val (pages, notes) = noteService.paginatedNotes(jwtPayload.userId, currentPage) - return Response(OK).html(view.notes(jwtPayload, notes, currentPage, pages)) + val tag = request.query("tag") + val (pages, notes) = noteService.paginatedNotes(jwtPayload.userId, currentPage, tag = tag) + return Response(OK).html(view.notes(jwtPayload, notes, currentPage, pages, tag = tag)) } fun note(request: Request, jwtPayload: JwtPayload): Response { diff --git a/app/src/main/kotlin/views/NoteView.kt b/app/src/main/kotlin/views/NoteView.kt index b82a9a7..5cfb8c7 100644 --- a/app/src/main/kotlin/views/NoteView.kt +++ b/app/src/main/kotlin/views/NoteView.kt @@ -48,7 +48,13 @@ class NoteView(staticFileResolver: StaticFileResolver) : View(staticFileResolver } } - fun notes(jwtPayload: JwtPayload, notes: List, currentPage: Int, numberOfPages: Int) = + fun notes( + jwtPayload: JwtPayload, + notes: List, + currentPage: Int, + numberOfPages: Int, + tag: String? + ) = renderPage(title = "Notes", jwtPayload = jwtPayload) { div("container mx-auto p-4") { div("flex justify-between mb-4") { @@ -66,25 +72,29 @@ class NoteView(staticFileResolver: StaticFileResolver) : View(staticFileResolver a(classes = "text-blue-200 text-xl hover:underline", href = "/notes/$uuid") { +title } - span { + span("space-x-2") { tags.forEach { - span("tag ml-2") { +"#$it" } + a(href = "?tag=$it", classes = "tag") { + span { +"#$it" } + } } } } } } if (numberOfPages > 1) - pagination(currentPage, numberOfPages) + pagination(currentPage, numberOfPages, tag) } else span { +"No notes yet" } // FIXME if too far in pagination, it it displayed } } - private fun DIV.pagination(currentPage: Int, numberOfPages: Int) { + private fun DIV.pagination(currentPage: Int, numberOfPages: Int, tag: String?) { val links = mutableListOf>() // if (currentPage > 1) links += "Previous" to "?page=${currentPage - 1}" - links += (1..numberOfPages).map { "$it" to "?page=$it" } + links += (1..numberOfPages).map { page -> + "$page" to (tag?.let { "?page=$page&tag=$it" } ?: "?page=$page") + } // if (currentPage < numberOfPages) links += "Next" to "?page=${currentPage + 1}" nav("pages") { diff --git a/domain/src/main/kotlin/usecases/NoteService.kt b/domain/src/main/kotlin/usecases/NoteService.kt index 58fbd0a..3799c68 100644 --- a/domain/src/main/kotlin/usecases/NoteService.kt +++ b/domain/src/main/kotlin/usecases/NoteService.kt @@ -29,11 +29,11 @@ class NoteService( .map { Note(it.metadata, markdown = markdownText, html = it.html) } .map { noteRepository.update(userId, uuid, it) } - fun paginatedNotes(userId: Int, page: Int, itemsPerPage: Int = 20): PaginatedNotes { - val count = noteRepository.count(userId) + fun paginatedNotes(userId: Int, page: Int, itemsPerPage: Int = 20, tag: String? = null): PaginatedNotes { + val count = noteRepository.count(userId, tag) val offset = (page - 1) * itemsPerPage val numberOfPages = (count / itemsPerPage) + 1 - val notes = if (count == 0) emptyList() else noteRepository.findAll(userId, itemsPerPage, offset) + val notes = if (count == 0) emptyList() else noteRepository.findAll(userId, itemsPerPage, offset, tag) return PaginatedNotes(numberOfPages, notes) } diff --git a/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt b/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt index 5054802..6ddefc4 100644 --- a/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt +++ b/domain/src/main/kotlin/usecases/repositories/NoteRepository.kt @@ -13,5 +13,5 @@ interface NoteRepository { fun update(userId: Int, uuid: UUID, note: Note): PersistedNote? fun delete(userId: Int, uuid: UUID): Boolean fun getTags(userId: Int): List - fun count(userId: Int): Int + fun count(userId: Int, tag: String? = null): Int } diff --git a/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt b/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt index e376aad..d830947 100644 --- a/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt +++ b/persistance/src/main/kotlin/notes/NoteRepositoryImpl.kt @@ -132,5 +132,9 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository { .mapColumns(isDistinct = true) { it.name } as List } - override fun count(userId: Int) = db.notes.count { it.userId eq userId } + override fun count(userId: Int, tag: String?): Int { + if (tag == null) return db.notes.count { it.userId eq userId } + return db.sequenceOf(Tags) + .count { it.name eq tag and (it.note.userId eq userId) } + } }