From d235451f07bd4185c5ac335857fbc3fdf4712272 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 4 Aug 2025 16:11:50 +0200 Subject: [PATCH] delete documents --- .../kotlin/wanijo/wanijo2/domain/document.kt | 10 ++++ .../domain/event/AssignTaggingCommand.kt | 3 -- .../domain/event/DeleteDocumentCommand.kt | 9 ++++ .../domain/event/DeleteTaggingCommand.kt | 3 -- .../domain/handler/DeleteDocumentHandler.kt | 16 +++++++ .../http/controller/DeleteController.kt | 24 ++++++++++ .../wanijo2/http/controller/ListController.kt | 1 + .../V202507181738__label_date_fields.sql | 47 ++++++++++++++----- src/main/resources/static/stylesheet.css | 13 +++++ src/main/resources/templates/show.html | 18 +++++-- 10 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 src/main/kotlin/wanijo/wanijo2/domain/event/DeleteDocumentCommand.kt create mode 100644 src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteDocumentHandler.kt create mode 100644 src/main/kotlin/wanijo/wanijo2/http/controller/DeleteController.kt diff --git a/src/main/kotlin/wanijo/wanijo2/domain/document.kt b/src/main/kotlin/wanijo/wanijo2/domain/document.kt index ea364f1..9ade1fc 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/document.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/document.kt @@ -1,6 +1,7 @@ package wanijo.wanijo2.domain 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.relational.core.mapping.Table import org.springframework.data.repository.Repository @@ -38,4 +39,13 @@ interface DocumentDao: Repository { """ ) fun findByTagName(tagName: String): List + + @Modifying + @Query( + """ + DELETE FROM t_document + WHERE id = :documentId + """ + ) + fun delete(documentId: DocumentId) } diff --git a/src/main/kotlin/wanijo/wanijo2/domain/event/AssignTaggingCommand.kt b/src/main/kotlin/wanijo/wanijo2/domain/event/AssignTaggingCommand.kt index cb1edc0..7c9befc 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/event/AssignTaggingCommand.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/event/AssignTaggingCommand.kt @@ -1,15 +1,12 @@ package wanijo.wanijo2.domain.event import jakarta.validation.constraints.Min -import jakarta.validation.constraints.NotEmpty import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.TagId data class AssignTaggingCommand( - @NotEmpty @Min(1) val tagId: TagId, - @NotEmpty @Min(1) val documentId: DocumentId ) diff --git a/src/main/kotlin/wanijo/wanijo2/domain/event/DeleteDocumentCommand.kt b/src/main/kotlin/wanijo/wanijo2/domain/event/DeleteDocumentCommand.kt new file mode 100644 index 0000000..7aa6d71 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/domain/event/DeleteDocumentCommand.kt @@ -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 +) diff --git a/src/main/kotlin/wanijo/wanijo2/domain/event/DeleteTaggingCommand.kt b/src/main/kotlin/wanijo/wanijo2/domain/event/DeleteTaggingCommand.kt index 4fc8d4c..9ad1cb8 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/event/DeleteTaggingCommand.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/event/DeleteTaggingCommand.kt @@ -1,15 +1,12 @@ package wanijo.wanijo2.domain.event import jakarta.validation.constraints.Min -import jakarta.validation.constraints.NotEmpty import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.TagId data class DeleteTaggingCommand( - @NotEmpty @Min(1) val tagId: TagId, - @NotEmpty @Min(1) val documentId: DocumentId ) diff --git a/src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteDocumentHandler.kt b/src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteDocumentHandler.kt new file mode 100644 index 0000000..98cd8f2 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteDocumentHandler.kt @@ -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) + } + +} diff --git a/src/main/kotlin/wanijo/wanijo2/http/controller/DeleteController.kt b/src/main/kotlin/wanijo/wanijo2/http/controller/DeleteController.kt new file mode 100644 index 0000000..71086c8 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/http/controller/DeleteController.kt @@ -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:/" + } + +} diff --git a/src/main/kotlin/wanijo/wanijo2/http/controller/ListController.kt b/src/main/kotlin/wanijo/wanijo2/http/controller/ListController.kt index a3d077b..bdab2bc 100644 --- a/src/main/kotlin/wanijo/wanijo2/http/controller/ListController.kt +++ b/src/main/kotlin/wanijo/wanijo2/http/controller/ListController.kt @@ -15,6 +15,7 @@ class ListController( fun list( model: Model ): String { + // TODO build overview class for documents without sub fields model["documents"] = docDao.findAll() return "list" } diff --git a/src/main/resources/db/migration/V202507181738__label_date_fields.sql b/src/main/resources/db/migration/V202507181738__label_date_fields.sql index 300dc09..ac71369 100644 --- a/src/main/resources/db/migration/V202507181738__label_date_fields.sql +++ b/src/main/resources/db/migration/V202507181738__label_date_fields.sql @@ -1,23 +1,33 @@ CREATE TABLE t_label_field ( id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - name VARCHAR NOT NULL, - "ORDER" INT NOT NULL DEFAULT 0, + name VARCHAR NOT NULL, + "ORDER" INT NOT NULL DEFAULT 0, "VALUE" VARCHAR, - created_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 + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), + 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 ( id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - name VARCHAR NOT NULL, - "ORDER" INT NOT NULL DEFAULT 0, + name VARCHAR NOT NULL, + "ORDER" INT NOT NULL DEFAULT 0, "VALUE" TIMESTAMP WITH TIME ZONE, - created_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 + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), + 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 @@ -30,6 +40,19 @@ CREATE TABLE t_tag CREATE TABLE t_document_tagging ( id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - tag_id INT references t_tag, - document_id INT references t_document + tag_id INT NOT NULL, + 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) ); diff --git a/src/main/resources/static/stylesheet.css b/src/main/resources/static/stylesheet.css index 9ada344..08d82b7 100644 --- a/src/main/resources/static/stylesheet.css +++ b/src/main/resources/static/stylesheet.css @@ -1,6 +1,7 @@ body { --dark-grey: #717171; --light-grey: #858585; + --warning: #ce0000; margin: 0 5rem; font-family: sans-serif; @@ -13,6 +14,10 @@ body { a:hover, a:active { text-decoration: underline; } + + .warning { + color: var(--warning); + } } nav { @@ -113,6 +118,10 @@ form { textarea { min-height: 10rem; } + + [type=submit] { + padding: .3rem; + } } .form-link { @@ -130,3 +139,7 @@ form { } } } + +.form-inline { + display: block; +} diff --git a/src/main/resources/templates/show.html b/src/main/resources/templates/show.html index 68af1e7..0e8302c 100644 --- a/src/main/resources/templates/show.html +++ b/src/main/resources/templates/show.html @@ -33,11 +33,12 @@
  • -
@@ -51,14 +52,23 @@

taggen

  • -
+ +

gefahr

+
+ + +