Adds document display (WIP)

This commit is contained in:
Stefan Schallerl 2025-02-03 00:55:14 +01:00
parent 3ee198faa9
commit aa940cc42a
5 changed files with 118 additions and 16 deletions

View file

@ -39,6 +39,13 @@ fun Context.requireSession(): Session = this.getSession() ?: throw UnauthorizedR
private val chars = ('a'..'z') + ('A'..'Z') + ('0'..'9') private val chars = ('a'..'z') + ('A'..'Z') + ('0'..'9')
fun generateExtId(): String { @JvmInline
return (0..8).map { chars.random() }.joinToString("") value class ExtId(val value: String) {
} override fun toString() = value
companion object {
fun generate(): ExtId {
return ExtId((0..8).map { chars.random() }.joinToString(""))
}
}
}

View file

@ -17,6 +17,13 @@ class DocumentController(val modifiers: TemplateModifiers, val repository: Sqlit
private val dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT) private val dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT)
private val isoDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss") private val isoDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
fun documentDetail(ctx: Context) {
val session = ctx.requireSession()
val extId = ctx.pathParam("extId")
}
fun createDocumentForm(ctx: Context) { fun createDocumentForm(ctx: Context) {
val session = ctx.requireSession() val session = ctx.requireSession()
@ -73,8 +80,8 @@ class DocumentController(val modifiers: TemplateModifiers, val repository: Sqlit
?: throw BadRequestResponse("") ?: throw BadRequestResponse("")
val fileIds = ctx.formParams("file_id").map { it.toLong() } val fileIds = ctx.formParams("file_id").map { it.toLong() }
val id = repository.addDocument(session.id, title, referenceDate, tags, description, fileIds) val extId = repository.addDocument(session.id, title, referenceDate, tags, description, fileIds)
ctx.redirectPRG("/document/$id") ctx.redirectPRG("/document/$extId")
} }
} }

View file

@ -1,7 +1,9 @@
package net.h34t.filemure.repository package net.h34t.filemure.repository
import net.h34t.filemure.ExtId
import net.h34t.filemure.LimboFile import net.h34t.filemure.LimboFile
import net.h34t.filemure.generateExtId import net.h34t.filemure.core.entity.Document
import net.h34t.filemure.core.entity.Tag
import java.io.InputStream import java.io.InputStream
import java.sql.Connection import java.sql.Connection
import java.sql.DriverManager import java.sql.DriverManager
@ -18,7 +20,7 @@ class SqliteRepository(url: String) {
connection.prepareStatement("INSERT INTO file (account_id, ext_id, filename, content_type, file_size, content) VALUES (?,?,?,?,?,?)") connection.prepareStatement("INSERT INTO file (account_id, ext_id, filename, content_type, file_size, content) VALUES (?,?,?,?,?,?)")
.use { stmt -> .use { stmt ->
stmt.setLong(1, accountId) stmt.setLong(1, accountId)
stmt.setString(2, generateExtId()) stmt.setString(2, ExtId.generate().toString())
stmt.setString(3, filename) stmt.setString(3, filename)
stmt.setString(4, contentType) stmt.setString(4, contentType)
stmt.setLong(5, size) stmt.setLong(5, size)
@ -70,9 +72,12 @@ class SqliteRepository(url: String) {
tags: List<String>, tags: List<String>,
description: String, description: String,
fileIds: List<Long> fileIds: List<Long>
): Long { ): ExtId {
val savePoint = connection.setSavepoint() val savePoint = connection.setSavepoint()
try { try {
val extId = ExtId.generate()
val documentId = connection.prepareStatement( val documentId = connection.prepareStatement(
"""INSERT INTO document """INSERT INTO document
|(account_id, ext_id, title, description, tags, created, reference_date) |(account_id, ext_id, title, description, tags, created, reference_date)
@ -80,7 +85,7 @@ class SqliteRepository(url: String) {
|(?, ?, ?, ?, ?, datetime(), ?)""".trimMargin() |(?, ?, ?, ?, ?, datetime(), ?)""".trimMargin()
).use { stmt -> ).use { stmt ->
stmt.setLong(1, accountId) stmt.setLong(1, accountId)
stmt.setString(2, generateExtId()) stmt.setString(2, extId.value)
stmt.setString(3, title) stmt.setString(3, title)
stmt.setString(4, description) stmt.setString(4, description)
stmt.setString(5, tags.joinToString(",")) stmt.setString(5, tags.joinToString(","))
@ -102,10 +107,78 @@ class SqliteRepository(url: String) {
} }
connection.commit() connection.commit()
return documentId return extId
} catch (exception: Exception) { } catch (exception: Exception) {
connection.rollback(savePoint) connection.rollback(savePoint)
throw exception throw exception
} }
} }
fun getDocuments(accountId: Long): List<Document> {
connection.prepareStatement("""SELECT d.id, d.account_id, d.ext_id, d.title, d.description, d.tags, d.created, d.reference_date FROM document d WHERE account_id=?""")
.use { stmt ->
stmt.setLong(1, accountId)
val res = stmt.executeQuery()
val documentList = mutableListOf<Document>()
while (res.next()) {
documentList.add(
Document(
id = res.getLong(1),
extId = res.getString(3),
title = res.getString(4),
description = res.getString(5),
tags = res.getString(6).split(",").map { Tag(it) },
created = LocalDateTime.parse(res.getString(7), dtf),
referenceDate = LocalDateTime.parse(res.getString(8), dtf),
files = emptyList()
)
)
}
return documentList.toList()
}
}
fun getDocumentById(accountId: Long, id: Long): Document {
return connection.prepareStatement("""SELECT d.id, d.account_id, d.ext_id, d.title, d.description, d.tags, d.created, d.reference_date FROM document d WHERE id=? AND account_id=?""")
.use { stmt ->
stmt.setLong(1, id)
stmt.setLong(2, accountId)
val res = stmt.executeQuery()
Document(
id = res.getLong(1),
extId = res.getString(3),
title = res.getString(4),
description = res.getString(5),
tags = res.getString(6).split(",").map { Tag(it) },
created = LocalDateTime.parse(res.getString(7), dtf),
referenceDate = LocalDateTime.parse(res.getString(8), dtf),
// TODO load files
files = emptyList()
)
}
}
fun getDocumentByExtId(accountId: Long, extId: String): Document {
return connection.prepareStatement("""SELECT d.id, d.account_id, d.ext_id, d.title, d.description, d.tags, d.created, d.reference_date FROM document d WHERE ext_id=? AND account_id=?""")
.use { stmt ->
stmt.setString(1, extId)
stmt.setLong(2, accountId)
val res = stmt.executeQuery()
Document(
id = res.getLong(1),
extId = res.getString(3),
title = res.getString(4),
description = res.getString(5),
tags = res.getString(6).split(",").map { Tag(it) },
created = LocalDateTime.parse(res.getString(7), dtf),
referenceDate = LocalDateTime.parse(res.getString(8), dtf),
// TODO load files
files = emptyList()
)
}
}
} }

View file

@ -0,0 +1,15 @@
<h1>{*$title}</h1>
<p>Date: {*$referenceDate}"></p>
<p>Tags: <span id="tags">{for $tags}<span>{*$tag}</span>{/for}</span></p>
<p>Description:<br>
{*$description}
</p>
<ul>
{for $files}
<li><a href="/file/{*$extId}">{*$filename}</a> ({*$contentType})</li>
{/for}
</ul>

View file

@ -1,16 +1,16 @@
package net.h34t.filemure.core.entity package net.h34t.filemure.core.entity
import kotlinx.datetime.Instant import java.time.LocalDateTime
import kotlinx.datetime.LocalDateTime
import java.util.* import java.util.*
class Document( class Document(
val id: UUID, val id: Long,
val extId: String,
val title: String, val title: String,
val description: String, val description: String,
val tags: List<Tag>, val tags: List<Tag>,
val created: Instant, val created: LocalDateTime,
val date: LocalDateTime, val referenceDate: LocalDateTime,
val files: List<DocFile>, val files: List<DocFile>,
) )
@ -29,6 +29,6 @@ data class DocFile(
val id: UUID, val id: UUID,
val filename: String, val filename: String,
val mimeType: MimeType, val mimeType: MimeType,
val created: Instant, val created: LocalDateTime,
) )