delete documents

master
Josha von Gizycki 2 weeks ago
parent d9e7b2e85a
commit d235451f07

@ -1,6 +1,7 @@
package wanijo.wanijo2.domain package wanijo.wanijo2.domain
import org.springframework.data.annotation.Id import org.springframework.data.annotation.Id
import org.springframework.data.jdbc.repository.query.Modifying
import org.springframework.data.jdbc.repository.query.Query import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.relational.core.mapping.Table import org.springframework.data.relational.core.mapping.Table
import org.springframework.data.repository.Repository import org.springframework.data.repository.Repository
@ -38,4 +39,13 @@ interface DocumentDao: Repository<Document, DocumentId> {
""" """
) )
fun findByTagName(tagName: String): List<Document> fun findByTagName(tagName: String): List<Document>
@Modifying
@Query(
"""
DELETE FROM t_document
WHERE id = :documentId
"""
)
fun delete(documentId: DocumentId)
} }

@ -1,15 +1,12 @@
package wanijo.wanijo2.domain.event package wanijo.wanijo2.domain.event
import jakarta.validation.constraints.Min import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotEmpty
import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.TagId import wanijo.wanijo2.domain.TagId
data class AssignTaggingCommand( data class AssignTaggingCommand(
@NotEmpty
@Min(1) @Min(1)
val tagId: TagId, val tagId: TagId,
@NotEmpty
@Min(1) @Min(1)
val documentId: DocumentId val documentId: DocumentId
) )

@ -0,0 +1,9 @@
package wanijo.wanijo2.domain.event
import jakarta.validation.constraints.Min
import wanijo.wanijo2.domain.DocumentId
data class DeleteDocumentCommand(
@Min(1)
val documentId: DocumentId
)

@ -1,15 +1,12 @@
package wanijo.wanijo2.domain.event package wanijo.wanijo2.domain.event
import jakarta.validation.constraints.Min import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotEmpty
import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.TagId import wanijo.wanijo2.domain.TagId
data class DeleteTaggingCommand( data class DeleteTaggingCommand(
@NotEmpty
@Min(1) @Min(1)
val tagId: TagId, val tagId: TagId,
@NotEmpty
@Min(1) @Min(1)
val documentId: DocumentId val documentId: DocumentId
) )

@ -0,0 +1,16 @@
package wanijo.wanijo2.domain.handler
import org.springframework.stereotype.Service
import wanijo.wanijo2.domain.DocumentDao
import wanijo.wanijo2.domain.event.DeleteDocumentCommand
@Service
class DeleteDocumentHandler(
val documentDao: DocumentDao
) {
fun exec(command: DeleteDocumentCommand) {
documentDao.delete(command.documentId)
}
}

@ -0,0 +1,24 @@
package wanijo.wanijo2.http.controller
import jakarta.validation.Valid
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PostMapping
import wanijo.wanijo2.domain.event.DeleteDocumentCommand
import wanijo.wanijo2.domain.handler.DeleteDocumentHandler
@Controller
class DeleteController(
val deleteDocumentHandler: DeleteDocumentHandler
) {
@PostMapping("/document/delete")
fun delete(
@Valid
command: DeleteDocumentCommand
): String {
deleteDocumentHandler.exec(command)
return "redirect:/"
}
}

@ -15,6 +15,7 @@ class ListController(
fun list( fun list(
model: Model model: Model
): String { ): String {
// TODO build overview class for documents without sub fields
model["documents"] = docDao.findAll() model["documents"] = docDao.findAll()
return "list" return "list"
} }

@ -6,7 +6,12 @@ CREATE TABLE t_label_field
"VALUE" VARCHAR, "VALUE" VARCHAR,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
t_document INT REFERENCES t_document (id) NOT NULL t_document INT NOT NULL,
CONSTRAINT fk_label_field_doc_id
FOREIGN KEY (t_document)
REFERENCES t_document
ON DELETE CASCADE
); );
CREATE TABLE t_date_field CREATE TABLE t_date_field
@ -17,7 +22,12 @@ CREATE TABLE t_date_field
"VALUE" TIMESTAMP WITH TIME ZONE, "VALUE" TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
t_document INT REFERENCES t_document (id) NOT NULL t_document INT NOT NULL,
CONSTRAINT fk_date_field_doc_id
FOREIGN KEY (t_document)
REFERENCES t_document
ON DELETE CASCADE
); );
CREATE TABLE t_tag CREATE TABLE t_tag
@ -30,6 +40,19 @@ CREATE TABLE t_tag
CREATE TABLE t_document_tagging CREATE TABLE t_document_tagging
( (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
tag_id INT references t_tag, tag_id INT NOT NULL,
document_id INT references t_document document_id INT NOT NULL,
CONSTRAINT fk_doc_tagging_tag_id
FOREIGN KEY (tag_id)
REFERENCES t_tag
ON DELETE CASCADE,
CONSTRAINT fk_doc_tagging_doc_id
FOREIGN KEY (document_id)
REFERENCES t_document
ON DELETE CASCADE,
CONSTRAINT uq_doc_tagging
UNIQUE (document_id, tag_id)
); );

@ -1,6 +1,7 @@
body { body {
--dark-grey: #717171; --dark-grey: #717171;
--light-grey: #858585; --light-grey: #858585;
--warning: #ce0000;
margin: 0 5rem; margin: 0 5rem;
font-family: sans-serif; font-family: sans-serif;
@ -13,6 +14,10 @@ body {
a:hover, a:active { a:hover, a:active {
text-decoration: underline; text-decoration: underline;
} }
.warning {
color: var(--warning);
}
} }
nav { nav {
@ -113,6 +118,10 @@ form {
textarea { textarea {
min-height: 10rem; min-height: 10rem;
} }
[type=submit] {
padding: .3rem;
}
} }
.form-link { .form-link {
@ -130,3 +139,7 @@ form {
} }
} }
} }
.form-inline {
display: block;
}

@ -33,11 +33,12 @@
<ul class="tag-container"> <ul class="tag-container">
<li th:each="tag : ${documentTags}" class="tag-block"> <li th:each="tag : ${documentTags}" class="tag-block">
<th:block th:text="${tag.name}"/>
<form class="form-link" th:action="@{/tagging/delete}" method="post"> <form class="form-link" th:action="@{/tagging/delete}" method="post">
<input type="hidden" name="tagId" th:value="${tag.id}"> <input type="hidden" name="tagId" th:value="${tag.id}">
<input type="hidden" name="documentId" th:value="${document.id}"> <input type="hidden" name="documentId" th:value="${document.id}">
<button type="submit">🞭</button> <button type="submit">
<th:block th:text="${tag.name}"/>
</button>
</form> </form>
</li> </li>
</ul> </ul>
@ -51,14 +52,23 @@
<h2 class="low-key-hint">taggen</h2> <h2 class="low-key-hint">taggen</h2>
<ul class="tag-container"> <ul class="tag-container">
<li th:each="tag : ${assignableTags}" class="tag-block"> <li th:each="tag : ${assignableTags}" class="tag-block">
<th:block th:text="${tag.name}"/>
<form class="form-link" th:action="@{/tagging/assign}" method="post"> <form class="form-link" th:action="@{/tagging/assign}" method="post">
<input type="hidden" name="tagId" th:value="${tag.id}"> <input type="hidden" name="tagId" th:value="${tag.id}">
<input type="hidden" name="documentId" th:value="${document.id}"> <input type="hidden" name="documentId" th:value="${document.id}">
<button type="submit"></button> <button type="submit">
<th:block th:text="${tag.name}"/>
</button>
</form> </form>
</li> </li>
</ul> </ul>
<h2 class="warning">gefahr</h2>
<form class="form-inline" th:action="@{/document/delete}" method="post">
<input type="hidden" name="documentId" th:value="${document.id}">
<button type="submit" class="warning">
LÖSCHEN
</button>
</form>
</main> </main>
</body> </body>
</html> </html>

Loading…
Cancel
Save