diff --git a/src/main/kotlin/wanijo/wanijo2/domain/document.kt b/src/main/kotlin/wanijo/wanijo2/domain/document.kt index 77bbc9b..c639d79 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/document.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/document.kt @@ -22,7 +22,13 @@ data class Document( val createdAt: ZonedDateTime = ZonedDateTime.now(), val labelFields: Set = emptySet(), val dateFields: Set = emptySet(), -) +) { + fun withLabel(field: LabelField) = + copy( + labelFields = labelFields + field, + updatedAt = ZonedDateTime.now() + ) +} @Table("T_DOCUMENT") data class DocumentBrief( @@ -80,4 +86,5 @@ interface DocumentDao : Repository { interface DocumentBriefDao : Repository { fun findAll(sort: Sort): List + fun findById(documentId: DocumentId): DocumentBrief? } diff --git a/src/main/kotlin/wanijo/wanijo2/domain/event/AddLabelFieldCommand.kt b/src/main/kotlin/wanijo/wanijo2/domain/event/AddLabelFieldCommand.kt new file mode 100644 index 0000000..d6bb90e --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/domain/event/AddLabelFieldCommand.kt @@ -0,0 +1,15 @@ +package wanijo.wanijo2.domain.event + +import jakarta.validation.constraints.Min +import jakarta.validation.constraints.NotEmpty +import wanijo.wanijo2.domain.DocumentId + +data class AddLabelFieldCommand( + @Min(1) + val documentId: DocumentId, + @NotEmpty + val labelName: String, + @NotEmpty + val labelValue: String, + val order: Int = 0 +) diff --git a/src/main/kotlin/wanijo/wanijo2/domain/handler/AddLabelFieldHandler.kt b/src/main/kotlin/wanijo/wanijo2/domain/handler/AddLabelFieldHandler.kt new file mode 100644 index 0000000..9c71250 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/domain/handler/AddLabelFieldHandler.kt @@ -0,0 +1,27 @@ +package wanijo.wanijo2.domain.handler + +import org.springframework.stereotype.Service +import wanijo.wanijo2.domain.DocumentDao +import wanijo.wanijo2.domain.LabelField +import wanijo.wanijo2.domain.event.AddLabelFieldCommand +import wanijo.wanijo2.http.DocumentNotFound + +@Service +class AddLabelFieldHandler( + val documentDao: DocumentDao +) { + + fun exec(command: AddLabelFieldCommand) { + val doc = documentDao.findById(command.documentId) ?: throw DocumentNotFound() + documentDao.save( + doc.withLabel( + LabelField( + order = command.order, + name = command.labelName, + value = command.labelValue + ) + ) + ) + } + +} diff --git a/src/main/kotlin/wanijo/wanijo2/http/controller/AddLabelFieldController.kt b/src/main/kotlin/wanijo/wanijo2/http/controller/AddLabelFieldController.kt new file mode 100644 index 0000000..96046f4 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/http/controller/AddLabelFieldController.kt @@ -0,0 +1,50 @@ +package wanijo.wanijo2.http.controller + +import jakarta.validation.Valid +import org.springframework.stereotype.Controller +import org.springframework.ui.Model +import org.springframework.ui.set +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestMapping +import wanijo.wanijo2.domain.DocumentBriefDao +import wanijo.wanijo2.domain.DocumentId +import wanijo.wanijo2.domain.event.AddLabelFieldCommand +import wanijo.wanijo2.domain.handler.AddLabelFieldHandler + +@Controller +@RequestMapping("/document/{id}/field/label/add") +class AddLabelFieldController( + val documentBriefDao: DocumentBriefDao, + val addLabelFieldHandler: AddLabelFieldHandler +) { + + @GetMapping + fun addLabel( + @PathVariable + id: DocumentId, + model: Model + ): String { + model["form"] = AddLabelFieldCommand( + documentId = id, + labelName = "", + labelValue = "" + ) + model["document"] = documentBriefDao.findById(id) + + return "addLabelField" + } + + @PostMapping + fun addLabel( + @PathVariable + id: DocumentId, + @Valid + command: AddLabelFieldCommand + ): String { + addLabelFieldHandler.exec(command) + return "redirect:/document/$id" + } + +} diff --git a/src/main/kotlin/wanijo/wanijo2/http/controller/ShowController.kt b/src/main/kotlin/wanijo/wanijo2/http/controller/ShowController.kt index a1c770c..a6d50db 100644 --- a/src/main/kotlin/wanijo/wanijo2/http/controller/ShowController.kt +++ b/src/main/kotlin/wanijo/wanijo2/http/controller/ShowController.kt @@ -8,6 +8,7 @@ import org.springframework.ui.set import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import wanijo.wanijo2.domain.DocumentDao +import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.TagDao import wanijo.wanijo2.http.DocumentNotFound @@ -20,7 +21,7 @@ class ShowController( @GetMapping("/document/{id}") fun show( model: Model, - @PathVariable id: Long + @PathVariable id: DocumentId ): String { val document = docDao.findById(id) ?: throw DocumentNotFound() model["document"] = document diff --git a/src/main/resources/static/stylesheet.css b/src/main/resources/static/stylesheet.css index 1c6607f..6ac4952 100644 --- a/src/main/resources/static/stylesheet.css +++ b/src/main/resources/static/stylesheet.css @@ -18,6 +18,11 @@ body { .warning { color: var(--warning); } + + h2 > a:link, h3 > a:link { + font-size: 1rem; + font-weight: normal; + } } nav { diff --git a/src/main/resources/templates/addLabelField.html b/src/main/resources/templates/addLabelField.html new file mode 100644 index 0000000..6b90bb7 --- /dev/null +++ b/src/main/resources/templates/addLabelField.html @@ -0,0 +1,37 @@ + + + + + + +
+ << zurück + +

+ label zu + + hinzufügen +

+ +
+ + + + + + + + + + + + +
+
+ + diff --git a/src/main/resources/templates/show.html b/src/main/resources/templates/show.html index 289e72c..eee31b0 100644 --- a/src/main/resources/templates/show.html +++ b/src/main/resources/templates/show.html @@ -53,6 +53,27 @@
+

felder

+

+ label + (+) +

+
+ +
+
+
+
+

+ datumsangaben +

+
+ +
+
+
+
+

taggen