Adds document deletion and file deletion

This commit is contained in:
Stefan Schallerl 2025-02-06 18:41:32 +01:00
parent 0d93394008
commit c44fa80d7f
7 changed files with 94 additions and 45 deletions

View file

@ -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(

View file

@ -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("/")
}
}

View file

@ -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)
}
}

View file

@ -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 ?;

View file

@ -10,4 +10,9 @@
{template $files}
<p><a href="/document/{*$extId}/edit">edit</a></p>
<p><a href="/document/{*$extId}/edit">edit</a></p>
<form action="/document/{*$extId}/delete" method="post">
<input type="submit" value="delete">
</form>

View file

@ -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}

View file

@ -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>
<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">