Find notes by tags in notes list

This commit is contained in:
Hubert Van De Walle 2020-08-17 17:09:48 +02:00
parent 8021814c31
commit 5295e32d86
5 changed files with 28 additions and 13 deletions

View File

@ -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 {

View File

@ -48,7 +48,13 @@ class NoteView(staticFileResolver: StaticFileResolver) : View(staticFileResolver
}
}
fun notes(jwtPayload: JwtPayload, notes: List<PersistedNoteMetadata>, currentPage: Int, numberOfPages: Int) =
fun notes(
jwtPayload: JwtPayload,
notes: List<PersistedNoteMetadata>,
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<Pair<String, String>>()
// 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") {

View File

@ -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)
}

View File

@ -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<String>
fun count(userId: Int): Int
fun count(userId: Int, tag: String? = null): Int
}

View File

@ -132,5 +132,9 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
.mapColumns(isDistinct = true) { it.name } as List<String>
}
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) }
}
}