adding labels
parent
c532fe4add
commit
14352bcfe9
@ -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"
|
||||
}
|
||||
|
||||
}
|
@ -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})}"><< 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>
|
Loading…
Reference in New Issue