diff --git a/app/src/main/kotlin/net/h34t/filemure/Util.kt b/app/src/main/kotlin/net/h34t/filemure/Util.kt index b2279d2..021956e 100644 --- a/app/src/main/kotlin/net/h34t/filemure/Util.kt +++ b/app/src/main/kotlin/net/h34t/filemure/Util.kt @@ -39,6 +39,13 @@ fun Context.requireSession(): Session = this.getSession() ?: throw UnauthorizedR private val chars = ('a'..'z') + ('A'..'Z') + ('0'..'9') -fun generateExtId(): String { - return (0..8).map { chars.random() }.joinToString("") -} +@JvmInline +value class ExtId(val value: String) { + override fun toString() = value + + companion object { + fun generate(): ExtId { + return ExtId((0..8).map { chars.random() }.joinToString("")) + } + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/net/h34t/filemure/controller/DocumentController.kt b/app/src/main/kotlin/net/h34t/filemure/controller/DocumentController.kt index 2e0b522..842b274 100644 --- a/app/src/main/kotlin/net/h34t/filemure/controller/DocumentController.kt +++ b/app/src/main/kotlin/net/h34t/filemure/controller/DocumentController.kt @@ -17,6 +17,13 @@ class DocumentController(val modifiers: TemplateModifiers, val repository: Sqlit private val dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT) 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) { val session = ctx.requireSession() @@ -73,8 +80,8 @@ class DocumentController(val modifiers: TemplateModifiers, val repository: Sqlit ?: throw BadRequestResponse("") 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") } } \ No newline at end of file diff --git a/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt b/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt index 1277e11..645fe59 100644 --- a/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt +++ b/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt @@ -1,7 +1,9 @@ package net.h34t.filemure.repository +import net.h34t.filemure.ExtId 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.sql.Connection 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 (?,?,?,?,?,?)") .use { stmt -> stmt.setLong(1, accountId) - stmt.setString(2, generateExtId()) + stmt.setString(2, ExtId.generate().toString()) stmt.setString(3, filename) stmt.setString(4, contentType) stmt.setLong(5, size) @@ -70,9 +72,12 @@ class SqliteRepository(url: String) { tags: List, description: String, fileIds: List - ): Long { + ): ExtId { + val savePoint = connection.setSavepoint() try { + val extId = ExtId.generate() + val documentId = connection.prepareStatement( """INSERT INTO document |(account_id, ext_id, title, description, tags, created, reference_date) @@ -80,7 +85,7 @@ class SqliteRepository(url: String) { |(?, ?, ?, ?, ?, datetime(), ?)""".trimMargin() ).use { stmt -> stmt.setLong(1, accountId) - stmt.setString(2, generateExtId()) + stmt.setString(2, extId.value) stmt.setString(3, title) stmt.setString(4, description) stmt.setString(5, tags.joinToString(",")) @@ -102,10 +107,78 @@ class SqliteRepository(url: String) { } connection.commit() - return documentId + return extId } catch (exception: Exception) { connection.rollback(savePoint) throw exception } } + + fun getDocuments(accountId: Long): List { + 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() + + 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() + ) + } + } } \ No newline at end of file diff --git a/app/src/main/tpl/net.h34t.filemure.tpl/Document.tpl.html b/app/src/main/tpl/net.h34t.filemure.tpl/Document.tpl.html new file mode 100644 index 0000000..ac9306c --- /dev/null +++ b/app/src/main/tpl/net.h34t.filemure.tpl/Document.tpl.html @@ -0,0 +1,15 @@ +

{*$title}

+ +

Date: {*$referenceDate}">

+ +

Tags: {for $tags}{*$tag}{/for}

+ +

Description:
+ {*$description} +

+ + diff --git a/core/src/main/kotlin/net/h34t/filemure/core/entity/Types.kt b/core/src/main/kotlin/net/h34t/filemure/core/entity/Types.kt index 42b4087..5f7f1d0 100644 --- a/core/src/main/kotlin/net/h34t/filemure/core/entity/Types.kt +++ b/core/src/main/kotlin/net/h34t/filemure/core/entity/Types.kt @@ -1,16 +1,16 @@ package net.h34t.filemure.core.entity -import kotlinx.datetime.Instant -import kotlinx.datetime.LocalDateTime +import java.time.LocalDateTime import java.util.* class Document( - val id: UUID, + val id: Long, + val extId: String, val title: String, val description: String, val tags: List, - val created: Instant, - val date: LocalDateTime, + val created: LocalDateTime, + val referenceDate: LocalDateTime, val files: List, ) @@ -29,6 +29,6 @@ data class DocFile( val id: UUID, val filename: String, val mimeType: MimeType, - val created: Instant, + val created: LocalDateTime, )