Categorizes documents into year/month on overview.

This commit is contained in:
Stefan Schallerl 2025-02-06 17:17:53 +01:00
parent 319c58bcc2
commit 0d93394008
8 changed files with 128 additions and 8 deletions

View file

@ -28,7 +28,6 @@ class FilemureApp(repository: SqliteRepository) {
} }
} }
server.get("/", overviewController::overview, Role.USER)
server.get("/login", loginPageController::formLogin, Role.ANON, Role.USER) server.get("/login", loginPageController::formLogin, Role.ANON, Role.USER)
server.post("/login", loginPageController::doLogin, Role.ANON, Role.USER) server.post("/login", loginPageController::doLogin, Role.ANON, Role.USER)
server.post("/logout", loginPageController::doLogout, Role.USER) server.post("/logout", loginPageController::doLogout, Role.USER)
@ -36,6 +35,9 @@ class FilemureApp(repository: SqliteRepository) {
server.post("/upload", uploadController::upload, Role.USER) server.post("/upload", uploadController::upload, Role.USER)
server.get("/limbo", limboController::formLimbo, Role.USER) server.get("/limbo", limboController::formLimbo, Role.USER)
server.get("/", overviewController::overview, Role.USER)
server.get("/overview/{year}/{month}", overviewController::overviewCategory, Role.USER)
server.get("/document/new", documentController::createDocumentForm, Role.USER) server.get("/document/new", documentController::createDocumentForm, Role.USER)
server.post("/document/new", documentController::createDocument, Role.USER) server.post("/document/new", documentController::createDocument, Role.USER)
server.get("/document/{extId}", documentController::documentDetail, Role.USER) server.get("/document/{extId}", documentController::documentDetail, Role.USER)

View file

@ -6,7 +6,7 @@ import java.net.URLEncoder
class TemplateModifiers : Frame.Modifiers, Limbo.Modifiers, DocumentCreateForm.Modifiers, Overview.Modifiers, class TemplateModifiers : Frame.Modifiers, Limbo.Modifiers, DocumentCreateForm.Modifiers, Overview.Modifiers,
FilePreview.Modifiers, DocumentEditForm.Modifiers, FileList.Modifiers, FilePreview.Modifiers, DocumentEditForm.Modifiers, FileList.Modifiers,
net.h34t.filemure.tpl.Document.Modifiers { net.h34t.filemure.tpl.Document.Modifiers, OverviewDocuments.Modifiers {
fun hashPrefix(arg: String): String { fun hashPrefix(arg: String): String {
return URLEncoder.encode(arg, Charsets.UTF_8) return URLEncoder.encode(arg, Charsets.UTF_8)

View file

@ -6,6 +6,7 @@ import io.javalin.http.HttpStatus
import io.javalin.http.UnauthorizedResponse import io.javalin.http.UnauthorizedResponse
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
import java.io.OutputStreamWriter import java.io.OutputStreamWriter
import java.time.Month
/** /**
* Outputs an HTML page rendered via TempolinTemplate. * Outputs an HTML page rendered via TempolinTemplate.
@ -49,4 +50,11 @@ object TagAdapter {
} }
fun List<Tag>.serialize() = if (this.isEmpty()) "" else this.joinToString(",") { it.value } fun List<Tag>.serialize() = if (this.isEmpty()) "" else this.joinToString(",") { it.value }
} }
fun List<Document>.grouped(): Map<Int, Map<Month, List<Document>>> =
this.groupBy { it.referenceDate.year }
.mapValues { (_, values) ->
values.groupBy { it.referenceDate.month }
}

View file

@ -2,11 +2,13 @@ package net.h34t.filemure.controller
import io.javalin.http.Context import io.javalin.http.Context
import net.h34t.filemure.TemplateModifiers import net.h34t.filemure.TemplateModifiers
import net.h34t.filemure.grouped
import net.h34t.filemure.repository.SqliteRepository import net.h34t.filemure.repository.SqliteRepository
import net.h34t.filemure.requireSession import net.h34t.filemure.requireSession
import net.h34t.filemure.tempolin import net.h34t.filemure.tempolin
import net.h34t.filemure.tpl.Frame import net.h34t.filemure.tpl.Frame
import net.h34t.filemure.tpl.Overview import net.h34t.filemure.tpl.Overview
import net.h34t.filemure.tpl.OverviewDocuments
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle import java.time.format.FormatStyle
@ -21,6 +23,8 @@ class OverviewController(val modifiers: TemplateModifiers, val repository: Sqlit
val documents = repository.getDocuments(accountId = session.id) val documents = repository.getDocuments(accountId = session.id)
val grouped = documents.grouped()
ctx.tempolin( ctx.tempolin(
Frame( Frame(
modifiers = modifiers, modifiers = modifiers,
@ -29,6 +33,43 @@ class OverviewController(val modifiers: TemplateModifiers, val repository: Sqlit
content = Overview( content = Overview(
modifiers = modifiers, modifiers = modifiers,
limboFileCount = limboFileCount.toString(), limboFileCount = limboFileCount.toString(),
year = {
grouped.entries.sortedByDescending { it.key }.map { year ->
YearBlock(
year = year.key.toString(),
month = {
year.value.entries.sortedByDescending { it.key }.map { month ->
MonthBlock(
year = year.key.toString(),
month = (month.key.ordinal + 1).toString(),
count = month.value.size.toString()
)
}.asSequence()
}
)
}.asSequence()
}
)
)
)
}
fun overviewCategory(ctx: Context) {
val session = ctx.requireSession()
val year = ctx.pathParam("year").toInt()
val month = ctx.pathParam("month").toInt()
val documents = repository.getDocumentsByYearMonth(accountId = session.id, year = year, month = month)
ctx.tempolin(
Frame(
modifiers = modifiers,
title = "Overview",
isTarget = true,
content = OverviewDocuments(
modifiers = modifiers,
category = "$year-$month",
document = { document = {
documents.map { document -> documents.map { document ->
DocumentBlock( DocumentBlock(

View file

@ -153,6 +153,26 @@ class SqliteRepository(url: String) {
} }
} }
fun getDocumentsByYearMonth(accountId: Long, year: Int, month: Int, state: State = State.ACTIVE) = "%04d%02d".format(year, month).let { date ->
database.databaseQueries.getDocumentsByYearMonth(
account_id = accountId,
yearMonth = date,
state = state
).executeAsList().map {
Document(
id = it.id,
extId = it.ext_id,
title = it.title,
description = it.description,
tags = it.tags,
created = it.created,
referenceDate = it.reference_date,
state = it.state,
files = emptyList()
)
}
}
fun updateDocument( fun updateDocument(
accountId: Long, accountId: Long,
id: Long, id: Long,

View file

@ -112,6 +112,25 @@ WHERE
account_id=? AND account_id=? AND
state=?; state=?;
getDocumentsByYearMonth:
SELECT
d.id,
d.account_id,
d.ext_id,
d.title,
d.description,
d.tags,
d.created,
d.reference_date,
d.state,
strftime("%Y%m", reference_date) AS yearMonth
FROM
document d
WHERE
account_id=? AND
state=? AND
yearMonth = ?;
updateDocument: updateDocument:
UPDATE UPDATE
document document
@ -178,3 +197,12 @@ WHERE
getLastInsertRowId: getLastInsertRowId:
SELECT last_insert_rowid(); SELECT last_insert_rowid();
setDocumentState:
UPDATE document SET state=? WHERE ext_id=?;
setDocumentStates:
UPDATE document SET state=? WHERE ext_id IN ?;
setFilesStates:
UPDATE file SET state=? WHERE ext_id IN ?;

View file

@ -3,11 +3,15 @@
<p>Files in <a href="/limbo">limbo: {$limboFileCount}</a>.</p> <p>Files in <a href="/limbo">limbo: {$limboFileCount}</a>.</p>
<table> <table>
{for $document} {for $year}
<tr> <tr>
<td>{*$referenceDate}</td> <th colspan="2">{*$year}</th>
<td>{*$title}</td> </tr>
<td><a href="/document/{*$extId}">details</a></td> {for $month}
<tr>
<td>{*$month}</td>
<td><a href="/overview/{*$year}/{*$month}">{*$count} Files</a></td>
</tr> </tr>
{/for} {/for}
</table> {/for}
</table>

View file

@ -0,0 +1,17 @@
<h1>Documents for {*$category}</h1>
<table>
<tr>
<th>Date</th>
<th>Title</th>
<th>Details</th>
</tr>
{for $document}
<tr>
<td>{*$referenceDate}</td>
<td>{*$title}</td>
<td><a href="/document/{*$extId}">details</a></td>
</tr>
{/for}
</table>