diff --git a/app/src/main/kotlin/net/h34t/filemure/FilemureApp.kt b/app/src/main/kotlin/net/h34t/filemure/FilemureApp.kt index 4a65a8a..ac55b23 100644 --- a/app/src/main/kotlin/net/h34t/filemure/FilemureApp.kt +++ b/app/src/main/kotlin/net/h34t/filemure/FilemureApp.kt @@ -44,8 +44,10 @@ class FilemureApp(repository: SqliteRepository) { server.get("/document/{extId}/download", documentController::downloadDocument, Role.USER) server.get("/document/{extId}/edit", documentController::editDocumentForm, Role.USER) server.post("/document/{extId}/edit", documentController::editDocumentAction, Role.USER) + server.post("/document/{extId}/delete", documentController::deleteDocumentAction, Role.USER) server.get("/file/{extId}/download", documentController::downloadFile, Role.USER) + server.get("/file/{extId}/delete", documentController::deleteFileAction, Role.USER) server.exception(UnauthorizedResponse::class.java) { e, ctx -> ctx.tempolin( 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 49a9191..5f7645a 100644 --- a/app/src/main/kotlin/net/h34t/filemure/controller/DocumentController.kt +++ b/app/src/main/kotlin/net/h34t/filemure/controller/DocumentController.kt @@ -89,6 +89,7 @@ class DocumentController(val modifiers: TemplateModifiers, val repository: Sqlit referenceDate = formDtf.format(referenceDate), tags = { tags.map { TagsBlock(it) } }, description = description, + fileId = { selectedFiles.map { FileIdBlock(it.extId.value) }.asSequence() }, files = FileList( modifiers = modifiers, delete = true, @@ -147,6 +148,15 @@ class DocumentController(val modifiers: TemplateModifiers, val repository: Sqlit ctx.result(file.content) } + fun deleteFileAction(ctx: Context) { + val session = ctx.requireSession() + val extId = ExtId(ctx.pathParam("extId")) + + repository.setFileState(session.id, extId = extId, state = State.ARCHIVED) + + ctx.redirectPRG("/") + } + fun editDocumentForm(ctx: Context) { val session = ctx.requireSession() val extId = ctx.pathParam("extId") @@ -212,4 +222,13 @@ class DocumentController(val modifiers: TemplateModifiers, val repository: Sqlit ctx.redirectPRG("/document/$extId") } + + fun deleteDocumentAction(ctx: Context) { + val session = ctx.requireSession() + val extId = ExtId(ctx.pathParam("extId")) + + repository.setDocumentState(accountId = session.id, extId = extId, State.DELETED) + + ctx.redirectPRG("/") + } } \ 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 1604e37..203227b 100644 --- a/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt +++ b/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt @@ -153,25 +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 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( accountId: Long, @@ -246,4 +247,11 @@ class SqliteRepository(url: String) { ) } } + + fun setDocumentState(accountId: Long, extId: ExtId, state: State) { + database.databaseQueries.setDocumentState(account_id = accountId, ext_id = extId, state = state) + } + fun setFileState(accountId: Long, extId: ExtId, state: State) { + database.databaseQueries.setFileState(account_id = accountId, ext_id = extId, state = state) + } } \ No newline at end of file diff --git a/app/src/main/sqldelight/net/h34t/filemure/db/Database.sq b/app/src/main/sqldelight/net/h34t/filemure/db/Database.sq index 21897e6..0960fcc 100644 --- a/app/src/main/sqldelight/net/h34t/filemure/db/Database.sq +++ b/app/src/main/sqldelight/net/h34t/filemure/db/Database.sq @@ -199,10 +199,13 @@ getLastInsertRowId: SELECT last_insert_rowid(); setDocumentState: -UPDATE document SET state=? WHERE ext_id=?; +UPDATE document SET state=? WHERE account_id=? AND ext_id=?; -setDocumentStates: -UPDATE document SET state=? WHERE ext_id IN ?; +setDocumentsState: +UPDATE document SET state=? WHERE account_id=? AND ext_id IN ?; -setFilesStates: -UPDATE file SET state=? WHERE ext_id IN ?; +setFileState: +UPDATE file SET state=? WHERE account_id=? AND ext_id=?; + +setFilesState: +UPDATE file SET state=? WHERE account_id=? AND ext_id IN ?; 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 index db6ef1c..ed7d4de 100644 --- a/app/src/main/tpl/net.h34t.filemure.tpl/Document.tpl.html +++ b/app/src/main/tpl/net.h34t.filemure.tpl/Document.tpl.html @@ -10,4 +10,9 @@ {template $files} -<p><a href="/document/{*$extId}/edit">edit</a></p> \ No newline at end of file +<p><a href="/document/{*$extId}/edit">edit</a></p> + + +<form action="/document/{*$extId}/delete" method="post"> + <input type="submit" value="delete"> +</form> \ No newline at end of file diff --git a/app/src/main/tpl/net.h34t.filemure.tpl/DocumentCreateForm.tpl.html b/app/src/main/tpl/net.h34t.filemure.tpl/DocumentCreateForm.tpl.html index b5735fa..f80f86f 100644 --- a/app/src/main/tpl/net.h34t.filemure.tpl/DocumentCreateForm.tpl.html +++ b/app/src/main/tpl/net.h34t.filemure.tpl/DocumentCreateForm.tpl.html @@ -13,6 +13,10 @@ <textarea name="description" rows="40" cols="80">{*$description}</textarea> </label></p> + {for $fileId} + <input type="hidden" name="file_id" value="{*$extId}"> + {/for} + <p><input type="submit" value="create"></p> {template $files} diff --git a/app/src/main/tpl/net.h34t.filemure.tpl/Limbo.tpl.html b/app/src/main/tpl/net.h34t.filemure.tpl/Limbo.tpl.html index 236777d..8ce6df4 100644 --- a/app/src/main/tpl/net.h34t.filemure.tpl/Limbo.tpl.html +++ b/app/src/main/tpl/net.h34t.filemure.tpl/Limbo.tpl.html @@ -1,24 +1,32 @@ <h1>Filemure Limbo</h1> <p>{$limboFileCount} Files</p> -<form action="/document/new" method="get"> - <table> - <tr> - <th>-</th> - <th>Filename</th> - <th>Type</th> - <th>Size</th> - <th>Uploaded</th> - </tr> - {for $file} - <tr> - <td><label><input type="checkbox" name="file_id" value="{*$extId}"></label></td> - <td>{*$file}</td> - <td>{*$type}</td> - <td>{*$size}</td> - <td>{*$uploaded}</td> - </tr> - {/for} - </table> - <input type="submit" value="new document"> -</form> \ No newline at end of file +<form id="newdoc" action="/document/new" method="get"> +</form> +<table> + <tr> + <th>-</th> + <th>Filename</th> + <th>Type</th> + <th>Size</th> + <th>Uploaded</th> + <th>view</th> + <th>delete</th> + </tr> + {for $file} + <tr> + <td><label><input type="checkbox" name="file_id" value="{*$extId}" form="newdoc"></label></td> + <td>{*$file}</td> + <td>{*$type}</td> + <td>{*$size}</td> + <td>{*$uploaded}</td> + <td><a href="/file/{*$extId}/download"> + view + </a></td> + <td> + <form action="/file/{*$extId}/delete"><input type="submit" value="delete"></form> + </td> + </tr> + {/for} +</table> +<input type="submit" value="new document" form="newdoc">