creating and displaying links

master
Josha von Gizycki 3 days ago
parent c788004de0
commit b9e52b6ab1

@ -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<Document, DocumentId> {
"""
)
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<DocumentBrief, DocumentId> {

@ -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<DocumentId>,
val direction: Direction
)

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

@ -22,11 +22,7 @@ class AssignTaggingHandler(
command.tagId
)
)
documentDao.save(
(documentDao
.findById(command.documentId) ?: throw DocumentNotFound())
.updated()
)
documentDao.updated(command.documentId)
}
}

@ -19,11 +19,7 @@ class DeleteTaggingHandler(
command.documentId,
command.tagId
)
documentDao.save(
(documentDao
.findById(command.documentId) ?: throw DocumentNotFound())
.updated()
)
documentDao.updated(command.documentId)
}
}

@ -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<Document, DocumentId>,
@Column("DOCUMENT_TO")
val to: AggregateReference<Document, DocumentId>,
val createdAt: Instant = Instant.now(),
)
enum class Direction {
OUTGOING,
INCOMING
interface LinkDao : Repository<Link, LinkId> {
fun save(link: Link): Link
fun findByFrom(from: AggregateReference<Document, Long>): List<Link>
}
interface LinkDao : Repository<Link, LinkId> {}
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<EnrichedLink, LinkId> {
@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<EnrichedLink>
@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<EnrichedLink>
}

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

@ -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<Link>()
model["outgoingLinks"] = emptyList<Link>()
model["outgoingLinks"] = enrichedLinkDao.findOutgoing(id)
model["incomingLinks"] = enrichedLinkDao.findIncoming(id)
return "show"
}

@ -29,12 +29,9 @@
th:text="${doc.name}"></option>
</select>
<button type="submit" value="OUTGOING" name="direction">
<button type="submit">
ausgehend
</button>
<button type="submit" value="INCOMING" name="direction">
eingehend
</button>
</form>
</main>
</body>

@ -66,8 +66,8 @@
</h3>
<ol>
<li th:each="link : ${incomingLinks}">
<a th:href="@{/document/{id}(id=${link.documentFrom})}"
th:text="link.documentFrom"></a>
<a th:href="@{/document/{id}(id=${link.from})}"
th:text="${link.fromName}"></a>
</li>
</ol>
<h3 class="low-key-hint">
@ -75,8 +75,8 @@
</h3>
<ol>
<li th:each="link : ${outgoingLinks}">
<a th:href="@{/document/{id}(id=${link.documentTo})}"
th:text="link.documentFrom"></a>
<a th:href="@{/document/{id}(id=${link.to})}"
th:text="${link.toName}"></a>
</li>
</ol>

Loading…
Cancel
Save