adding labels

master
Josha von Gizycki 2 months ago
parent c532fe4add
commit 14352bcfe9

@ -22,7 +22,13 @@ data class Document(
val createdAt: ZonedDateTime = ZonedDateTime.now(), val createdAt: ZonedDateTime = ZonedDateTime.now(),
val labelFields: Set<LabelField> = emptySet(), val labelFields: Set<LabelField> = emptySet(),
val dateFields: Set<DateField> = emptySet(), val dateFields: Set<DateField> = emptySet(),
) {
fun withLabel(field: LabelField) =
copy(
labelFields = labelFields + field,
updatedAt = ZonedDateTime.now()
) )
}
@Table("T_DOCUMENT") @Table("T_DOCUMENT")
data class DocumentBrief( data class DocumentBrief(
@ -80,4 +86,5 @@ interface DocumentDao : Repository<Document, DocumentId> {
interface DocumentBriefDao : Repository<DocumentBrief, DocumentId> { interface DocumentBriefDao : Repository<DocumentBrief, DocumentId> {
fun findAll(sort: Sort): List<DocumentBrief> fun findAll(sort: Sort): List<DocumentBrief>
fun findById(documentId: DocumentId): DocumentBrief?
} }

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

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

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

@ -8,6 +8,7 @@ import org.springframework.ui.set
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PathVariable
import wanijo.wanijo2.domain.DocumentDao import wanijo.wanijo2.domain.DocumentDao
import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.TagDao import wanijo.wanijo2.domain.TagDao
import wanijo.wanijo2.http.DocumentNotFound import wanijo.wanijo2.http.DocumentNotFound
@ -20,7 +21,7 @@ class ShowController(
@GetMapping("/document/{id}") @GetMapping("/document/{id}")
fun show( fun show(
model: Model, model: Model,
@PathVariable id: Long @PathVariable id: DocumentId
): String { ): String {
val document = docDao.findById(id) ?: throw DocumentNotFound() val document = docDao.findById(id) ?: throw DocumentNotFound()
model["document"] = document model["document"] = document

@ -18,6 +18,11 @@ body {
.warning { .warning {
color: var(--warning); color: var(--warning);
} }
h2 > a:link, h3 > a:link {
font-size: 1rem;
font-weight: normal;
}
} }
nav { nav {

@ -0,0 +1,37 @@
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{base}"
lang="">
<head>
<title th:text="${document.name}"></title>
</head>
<body>
<main id="content" layout:fragment="content">
<a th:href="@{/document/{id}(id=${document.id})}">&lt;&lt; zurück</a>
<h2>
<span class="low-key-hint">label zu</span>
<th:block th:text="${document.name}"/>
<span class="low-key-hint">hinzufügen</span>
</h2>
<form th:action="@{/document/{id}/field/label/add(id=${document.id})}" th:object="${form}" method="post">
<input type="hidden" th:field="*{documentId}">
<label for="labelName">name</label>
<input id="labelName" type="text" th:field="*{labelName}" required>
<label for="labelValue">wert</label>
<input id="labelValue" th:field="*{labelValue}" type="text" required>
<label for="order">sortierung</label>
<input id="order" th:field="*{order}" type="number" required>
<button type="submit">
hinzufügen
</button>
</form>
</main>
</body>
</html>

@ -53,6 +53,27 @@
<div class="show__markdown" th:utext="${descHtml}"></div> <div class="show__markdown" th:utext="${descHtml}"></div>
</fieldset> </fieldset>
<h2 class="low-key-hint">felder</h2>
<h3 class="low-key-hint">
label
<a th:href="@{/document/{id}/field/label/add(id=${document.id})}">(+)</a>
</h3>
<dl>
<th:block th:each="field : ${document.labelFields}">
<dt th:text="${field.name}"></dt>
<dd th:text="${field.value}"></dd>
</th:block>
</dl>
<h3 class="low-key-hint">
datumsangaben
</h3>
<dl>
<th:block th:each="field : ${document.dateFields}">
<dt th:text="${field.name}"></dt>
<dd th:text="${#temporals.format(field.value, 'yyyy-MM-dd HH:mm')}"></dd>
</th:block>
</dl>
<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">

Loading…
Cancel
Save