editing documents

master
Josha von Gizycki 6 days ago
parent d235451f07
commit 8e4b400193

@ -8,13 +8,15 @@ import org.springframework.data.repository.Repository
import java.time.ZonedDateTime
typealias DocumentId = Long
typealias DocumentName = String
typealias DocumentDescription = String
@Table("T_DOCUMENT")
data class Document(
@Id
val id: DocumentId = 0,
val name: String,
val description: String = "",
val name: DocumentName,
val description: DocumentDescription = "",
val updatedAt: ZonedDateTime = ZonedDateTime.now(),
val createdAt: ZonedDateTime = ZonedDateTime.now(),
val labelFields: Set<LabelField> = emptySet(),
@ -23,8 +25,8 @@ data class Document(
interface DocumentDao: Repository<Document, DocumentId> {
fun findAll(): List<Document>
fun findById(id: Long): Document?
fun findByName(name: String): List<Document>
fun findById(id: DocumentId): Document?
fun findByName(name: DocumentName): List<Document>
fun save(doc: Document): Document
@Query(
@ -38,7 +40,22 @@ interface DocumentDao: Repository<Document, DocumentId> {
WHERE LOWER(tag.name) LIKE '%' || LOWER(:tagName) || '%'
"""
)
fun findByTagName(tagName: String): List<Document>
fun findByTagName(tagName: TagName): List<Document>
@Modifying
@Query(
"""
UPDATE t_document
SET name = :name,
description = :description
WHERE id = :id
"""
)
fun edit(
id: DocumentId,
name: DocumentName,
description: DocumentDescription
)
@Modifying
@Query(

@ -0,0 +1,25 @@
package wanijo.wanijo2.domain.event
import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotEmpty
import wanijo.wanijo2.domain.Document
import wanijo.wanijo2.domain.DocumentDescription
import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.DocumentName
data class EditDocumentCommand(
@Min(0)
val id: DocumentId,
@NotEmpty
val name: DocumentName,
val description: DocumentDescription
) {
companion object {
fun fromDocument(doc: Document) =
EditDocumentCommand(
id = doc.id,
name = doc.name,
description = doc.description
)
}
}

@ -0,0 +1,20 @@
package wanijo.wanijo2.domain.handler
import org.springframework.stereotype.Service
import wanijo.wanijo2.domain.DocumentDao
import wanijo.wanijo2.domain.event.EditDocumentCommand
@Service
class EditDocumentHandler(
val documentDao: DocumentDao
) {
fun exec(command: EditDocumentCommand) {
documentDao.edit(
id = command.id,
name = command.name,
description = command.description
)
}
}

@ -9,12 +9,13 @@ import org.springframework.data.repository.Repository
import java.time.ZonedDateTime
typealias TagId = Long
typealias TagName = String
@Table("T_TAG")
data class Tag(
@Id
val id: TagId = 0,
val name: String,
val name: TagName,
val createdAt: ZonedDateTime = ZonedDateTime.now()
)

@ -0,0 +1,43 @@
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 wanijo.wanijo2.domain.DocumentDao
import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.event.EditDocumentCommand
import wanijo.wanijo2.domain.handler.EditDocumentHandler
@Controller
class EditController(
val documentDao: DocumentDao,
val editDocumentHandler: EditDocumentHandler
) {
@GetMapping("/document/{id}/edit")
fun edit(
@PathVariable
id: DocumentId,
model: Model
): String {
val doc = documentDao.findById(id)!!
model["form"] = EditDocumentCommand.fromDocument(doc)
return "edit"
}
@PostMapping("/document/edit")
fun edit(
@Valid
command: EditDocumentCommand
): String {
editDocumentHandler.exec(command)
return "redirect:/document/${command.id}"
}
}

@ -116,7 +116,7 @@ form {
}
textarea {
min-height: 10rem;
min-height: 20rem;
}
[type=submit] {

@ -0,0 +1,26 @@
<!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="${form.name}"></title>
</head>
<body>
<main id="content" layout:fragment="content">
<form th:action="@{/document/edit}" th:object="${form}" method="post">
<input type="hidden" th:field="*{id}">
<label for="name">name</label>
<input id="name" type="text" th:field="*{name}" required>
<label for="description">beschreibung</label>
<textarea id="description" th:field="*{description}"></textarea>
<button type="submit">
bearbeiten
</button>
</form>
</main>
</body>
</html>

@ -4,7 +4,7 @@
layout:decorate="~{base}"
lang="">
<head>
<title>Home</title>
<title>neu</title>
</head>
<body>
<main id="content" layout:fragment="content">

@ -4,7 +4,7 @@
layout:decorate="~{base}"
lang="">
<head>
<title>Home</title>
<title th:text="${document.name}"></title>
</head>
<body>
<main id="content" layout:fragment="content">
@ -14,7 +14,10 @@
</h2>
<fieldset class="show__meta">
<legend>meta</legend>
<legend>
meta
<a th:href="@{/document/{id}/edit(id=${document.id})}">bearbeiten</a>
</legend>
<dl>
<dt>id:</dt>

Loading…
Cancel
Save