Find notes by tags in notes list
This commit is contained in:
parent
8021814c31
commit
5295e32d86
@ -46,8 +46,9 @@ class NoteController(
|
|||||||
|
|
||||||
fun list(request: Request, jwtPayload: JwtPayload): Response {
|
fun list(request: Request, jwtPayload: JwtPayload): Response {
|
||||||
val currentPage = request.query("page")?.toIntOrNull()?.let(::abs) ?: 1
|
val currentPage = request.query("page")?.toIntOrNull()?.let(::abs) ?: 1
|
||||||
val (pages, notes) = noteService.paginatedNotes(jwtPayload.userId, currentPage)
|
val tag = request.query("tag")
|
||||||
return Response(OK).html(view.notes(jwtPayload, notes, currentPage, pages))
|
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 {
|
fun note(request: Request, jwtPayload: JwtPayload): Response {
|
||||||
|
|||||||
@ -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) {
|
renderPage(title = "Notes", jwtPayload = jwtPayload) {
|
||||||
div("container mx-auto p-4") {
|
div("container mx-auto p-4") {
|
||||||
div("flex justify-between mb-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") {
|
a(classes = "text-blue-200 text-xl hover:underline", href = "/notes/$uuid") {
|
||||||
+title
|
+title
|
||||||
}
|
}
|
||||||
span {
|
span("space-x-2") {
|
||||||
tags.forEach {
|
tags.forEach {
|
||||||
span("tag ml-2") { +"#$it" }
|
a(href = "?tag=$it", classes = "tag") {
|
||||||
|
span { +"#$it" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (numberOfPages > 1)
|
if (numberOfPages > 1)
|
||||||
pagination(currentPage, numberOfPages)
|
pagination(currentPage, numberOfPages, tag)
|
||||||
} else
|
} else
|
||||||
span { +"No notes yet" } // FIXME if too far in pagination, it it displayed
|
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>>()
|
val links = mutableListOf<Pair<String, String>>()
|
||||||
// if (currentPage > 1) links += "Previous" to "?page=${currentPage - 1}"
|
// 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}"
|
// if (currentPage < numberOfPages) links += "Next" to "?page=${currentPage + 1}"
|
||||||
|
|
||||||
nav("pages") {
|
nav("pages") {
|
||||||
|
|||||||
@ -29,11 +29,11 @@ class NoteService(
|
|||||||
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
||||||
.map { noteRepository.update(userId, uuid, it) }
|
.map { noteRepository.update(userId, uuid, it) }
|
||||||
|
|
||||||
fun paginatedNotes(userId: Int, page: Int, itemsPerPage: Int = 20): PaginatedNotes {
|
fun paginatedNotes(userId: Int, page: Int, itemsPerPage: Int = 20, tag: String? = null): PaginatedNotes {
|
||||||
val count = noteRepository.count(userId)
|
val count = noteRepository.count(userId, tag)
|
||||||
val offset = (page - 1) * itemsPerPage
|
val offset = (page - 1) * itemsPerPage
|
||||||
val numberOfPages = (count / itemsPerPage) + 1
|
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)
|
return PaginatedNotes(numberOfPages, notes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,5 +13,5 @@ interface NoteRepository {
|
|||||||
fun update(userId: Int, uuid: UUID, note: Note): PersistedNote?
|
fun update(userId: Int, uuid: UUID, note: Note): PersistedNote?
|
||||||
fun delete(userId: Int, uuid: UUID): Boolean
|
fun delete(userId: Int, uuid: UUID): Boolean
|
||||||
fun getTags(userId: Int): List<String>
|
fun getTags(userId: Int): List<String>
|
||||||
fun count(userId: Int): Int
|
fun count(userId: Int, tag: String? = null): Int
|
||||||
}
|
}
|
||||||
|
|||||||
@ -132,5 +132,9 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
|
|||||||
.mapColumns(isDistinct = true) { it.name } as List<String>
|
.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) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user