From b9e52b6ab1dadde300ec41bd5151c7bacd942456 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 29 Dec 2025 19:40:27 +0100 Subject: [PATCH] creating and displaying links --- .../kotlin/wanijo/wanijo2/domain/document.kt | 15 +++-- .../wanijo2/domain/event/AddLinkCommand.kt | 2 - .../wanijo2/domain/handler/AddLinkHandler.kt | 30 ++++++++++ .../domain/handler/AssignTaggingHandler.kt | 6 +- .../domain/handler/DeleteTaggingHandler.kt | 6 +- src/main/kotlin/wanijo/wanijo2/domain/link.kt | 55 +++++++++++++++++-- .../wanijo2/http/controller/LinkController.kt | 18 +++--- .../wanijo2/http/controller/ShowController.kt | 13 ++--- src/main/resources/templates/addLink.html | 5 +- src/main/resources/templates/show.html | 8 +-- 10 files changed, 113 insertions(+), 45 deletions(-) create mode 100644 src/main/kotlin/wanijo/wanijo2/domain/handler/AddLinkHandler.kt diff --git a/src/main/kotlin/wanijo/wanijo2/domain/document.kt b/src/main/kotlin/wanijo/wanijo2/domain/document.kt index 87eb19f..c7bb747 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/document.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/document.kt @@ -53,11 +53,6 @@ data class Document( updatedAt = Instant.now(), dateFields = dateFields.filter { it.id != fieldId }.toSet() ) - - fun updated() = - copy( - updatedAt = Instant.now() - ) } @Table("T_DOCUMENT") @@ -116,6 +111,16 @@ interface DocumentDao : Repository { """ ) fun delete(documentId: DocumentId) + + @Modifying + @Query( + """ + UPDATE t_document + SET updated_at = :updatedAt + WHERE id = :documentId + """ + ) + fun updated(documentId: DocumentId, updatedAt: Instant = Instant.now()) } interface DocumentBriefDao : Repository { diff --git a/src/main/kotlin/wanijo/wanijo2/domain/event/AddLinkCommand.kt b/src/main/kotlin/wanijo/wanijo2/domain/event/AddLinkCommand.kt index 7d9fbe4..574b5f9 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/event/AddLinkCommand.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/event/AddLinkCommand.kt @@ -1,7 +1,6 @@ package wanijo.wanijo2.domain.event import jakarta.validation.constraints.Min -import wanijo.wanijo2.domain.Direction import wanijo.wanijo2.domain.DocumentId data class AddLinkCommand( @@ -9,5 +8,4 @@ data class AddLinkCommand( val documentId: DocumentId, @Min.List(Min(1)) val otherDocuments: List, - val direction: Direction ) diff --git a/src/main/kotlin/wanijo/wanijo2/domain/handler/AddLinkHandler.kt b/src/main/kotlin/wanijo/wanijo2/domain/handler/AddLinkHandler.kt new file mode 100644 index 0000000..1596c67 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/domain/handler/AddLinkHandler.kt @@ -0,0 +1,30 @@ +package wanijo.wanijo2.domain.handler + +import org.springframework.data.jdbc.core.mapping.AggregateReference +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import wanijo.wanijo2.domain.DocumentDao +import wanijo.wanijo2.domain.Link +import wanijo.wanijo2.domain.LinkDao +import wanijo.wanijo2.domain.event.AddLinkCommand + +@Service +class AddLinkHandler( + val documentDao: DocumentDao, + val linkDao: LinkDao +) { + + @Transactional + fun exec(command: AddLinkCommand) { + command.otherDocuments.forEach { + linkDao.save( + Link( + from = AggregateReference.to(command.documentId), + to = AggregateReference.to(it) + ) + ) + } + documentDao.updated(command.documentId) + } + +} diff --git a/src/main/kotlin/wanijo/wanijo2/domain/handler/AssignTaggingHandler.kt b/src/main/kotlin/wanijo/wanijo2/domain/handler/AssignTaggingHandler.kt index 3eaa258..51d1384 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/handler/AssignTaggingHandler.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/handler/AssignTaggingHandler.kt @@ -22,11 +22,7 @@ class AssignTaggingHandler( command.tagId ) ) - documentDao.save( - (documentDao - .findById(command.documentId) ?: throw DocumentNotFound()) - .updated() - ) + documentDao.updated(command.documentId) } } diff --git a/src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteTaggingHandler.kt b/src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteTaggingHandler.kt index 062c1b2..b6adc54 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteTaggingHandler.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/handler/DeleteTaggingHandler.kt @@ -19,11 +19,7 @@ class DeleteTaggingHandler( command.documentId, command.tagId ) - documentDao.save( - (documentDao - .findById(command.documentId) ?: throw DocumentNotFound()) - .updated() - ) + documentDao.updated(command.documentId) } } diff --git a/src/main/kotlin/wanijo/wanijo2/domain/link.kt b/src/main/kotlin/wanijo/wanijo2/domain/link.kt index 07f3f59..16cc85e 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/link.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/link.kt @@ -2,6 +2,8 @@ package wanijo.wanijo2.domain import org.springframework.data.annotation.Id import org.springframework.data.jdbc.core.mapping.AggregateReference +import org.springframework.data.jdbc.repository.query.Query +import org.springframework.data.relational.core.mapping.Column import org.springframework.data.relational.core.mapping.Table import org.springframework.data.repository.Repository import java.time.Instant @@ -12,14 +14,59 @@ typealias LinkId = Long data class Link( @Id val id: LinkId = 0, + @Column("DOCUMENT_FROM") val from: AggregateReference, + @Column("DOCUMENT_TO") val to: AggregateReference, val createdAt: Instant = Instant.now(), ) -enum class Direction { - OUTGOING, - INCOMING +interface LinkDao : Repository { + fun save(link: Link): Link + fun findByFrom(from: AggregateReference): List } -interface LinkDao : Repository {} +data class EnrichedLink( + @Id + val id: LinkId = 0, + @Column("DOCUMENT_FROM") + val from: DocumentId, + @Column("DOCUMENT_TO") + val to: DocumentId, + val fromName: String, + val toName: String +) + +interface EnrichedLinkDao : Repository { + @Query( + """ + SELECT link.id, + link.document_from, + link.document_to, + docfrom.name from_name, + docto.name to_name + FROM t_link link + JOIN t_document docfrom ON link.document_from = docfrom.id + JOIN t_document docto ON link.document_to = docto.id + WHERE link.document_from = :documentId + ORDER BY link.created_at + """ + ) + fun findOutgoing(documentId: DocumentId): List + + @Query( + """ + SELECT link.id, + link.document_from, + link.document_to, + docfrom.name from_name, + docto.name to_name + FROM t_link link + JOIN t_document docfrom ON link.document_from = docfrom.id + JOIN t_document docto ON link.document_to = docto.id + WHERE link.document_to = :documentId + ORDER BY link.created_at + """ + ) + fun findIncoming(documentId: DocumentId): List +} diff --git a/src/main/kotlin/wanijo/wanijo2/http/controller/LinkController.kt b/src/main/kotlin/wanijo/wanijo2/http/controller/LinkController.kt index dc09816..9340921 100644 --- a/src/main/kotlin/wanijo/wanijo2/http/controller/LinkController.kt +++ b/src/main/kotlin/wanijo/wanijo2/http/controller/LinkController.kt @@ -1,23 +1,26 @@ package wanijo.wanijo2.http.controller +import jakarta.validation.Valid import org.springframework.data.domain.Sort 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 wanijo.wanijo2.domain.Direction +import org.springframework.web.bind.annotation.PostMapping import wanijo.wanijo2.domain.DocumentBriefDao import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.event.AddLinkCommand +import wanijo.wanijo2.domain.handler.AddLinkHandler @Controller class LinkController( val documentBriefDao: DocumentBriefDao, + val addLinkHandler: AddLinkHandler ) { @GetMapping("/document/{id}/link/add") - fun addLabel( + fun addLink( @PathVariable id: DocumentId, model: Model @@ -28,21 +31,20 @@ class LinkController( model["addForm"] = AddLinkCommand( id, emptyList(), - Direction.OUTGOING ) return "addLink" } - /*@PostMapping("/document/{id}/field/label/add") - fun addLabel( + @PostMapping("/document/{id}/link/add") + fun addLink( @PathVariable id: DocumentId, @Valid - command: AddLabelFieldCommand + command: AddLinkCommand ): String { - addLabelFieldHandler.exec(command) + addLinkHandler.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 5fab584..ef6db67 100644 --- a/src/main/kotlin/wanijo/wanijo2/http/controller/ShowController.kt +++ b/src/main/kotlin/wanijo/wanijo2/http/controller/ShowController.kt @@ -2,22 +2,19 @@ package wanijo.wanijo2.http.controller import org.commonmark.parser.Parser import org.commonmark.renderer.html.HtmlRenderer +import org.springframework.data.jdbc.core.mapping.AggregateReference 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 wanijo.wanijo2.domain.DocumentDao -import wanijo.wanijo2.domain.DocumentId -import wanijo.wanijo2.domain.Link -import wanijo.wanijo2.domain.LinkDao -import wanijo.wanijo2.domain.TagDao +import wanijo.wanijo2.domain.* import wanijo.wanijo2.http.DocumentNotFound @Controller class ShowController( val docDao: DocumentDao, - val linkDao: LinkDao, + val enrichedLinkDao: EnrichedLinkDao, val tagDao: TagDao ) { @@ -40,8 +37,8 @@ class ShowController( model["documentTags"] = documentTags.sortedBy { it.name.lowercase() } model["assignableTags"] = (tagDao.findAll() - documentTags).sortedBy { it.name.lowercase() } - model["incomingLinks"] = emptyList() - model["outgoingLinks"] = emptyList() + model["outgoingLinks"] = enrichedLinkDao.findOutgoing(id) + model["incomingLinks"] = enrichedLinkDao.findIncoming(id) return "show" } diff --git a/src/main/resources/templates/addLink.html b/src/main/resources/templates/addLink.html index e8b3cb3..044ad24 100644 --- a/src/main/resources/templates/addLink.html +++ b/src/main/resources/templates/addLink.html @@ -29,12 +29,9 @@ th:text="${doc.name}"> - - diff --git a/src/main/resources/templates/show.html b/src/main/resources/templates/show.html index b7997f1..f2d94a8 100644 --- a/src/main/resources/templates/show.html +++ b/src/main/resources/templates/show.html @@ -66,8 +66,8 @@
  1. - +

@@ -75,8 +75,8 @@

  1. - +