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(), updatedAt = Instant.now(),
dateFields = dateFields.filter { it.id != fieldId }.toSet() dateFields = dateFields.filter { it.id != fieldId }.toSet()
) )
fun updated() =
copy(
updatedAt = Instant.now()
)
} }
@Table("T_DOCUMENT") @Table("T_DOCUMENT")
@ -116,6 +111,16 @@ interface DocumentDao : Repository<Document, DocumentId> {
""" """
) )
fun delete(documentId: 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> { interface DocumentBriefDao : Repository<DocumentBrief, DocumentId> {

@ -1,7 +1,6 @@
package wanijo.wanijo2.domain.event package wanijo.wanijo2.domain.event
import jakarta.validation.constraints.Min import jakarta.validation.constraints.Min
import wanijo.wanijo2.domain.Direction
import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.DocumentId
data class AddLinkCommand( data class AddLinkCommand(
@ -9,5 +8,4 @@ data class AddLinkCommand(
val documentId: DocumentId, val documentId: DocumentId,
@Min.List(Min(1)) @Min.List(Min(1))
val otherDocuments: List<DocumentId>, 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 command.tagId
) )
) )
documentDao.save( documentDao.updated(command.documentId)
(documentDao
.findById(command.documentId) ?: throw DocumentNotFound())
.updated()
)
} }
} }

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

@ -2,6 +2,8 @@ package wanijo.wanijo2.domain
import org.springframework.data.annotation.Id import org.springframework.data.annotation.Id
import org.springframework.data.jdbc.core.mapping.AggregateReference 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.relational.core.mapping.Table
import org.springframework.data.repository.Repository import org.springframework.data.repository.Repository
import java.time.Instant import java.time.Instant
@ -12,14 +14,59 @@ typealias LinkId = Long
data class Link( data class Link(
@Id @Id
val id: LinkId = 0, val id: LinkId = 0,
@Column("DOCUMENT_FROM")
val from: AggregateReference<Document, DocumentId>, val from: AggregateReference<Document, DocumentId>,
@Column("DOCUMENT_TO")
val to: AggregateReference<Document, DocumentId>, val to: AggregateReference<Document, DocumentId>,
val createdAt: Instant = Instant.now(), val createdAt: Instant = Instant.now(),
) )
enum class Direction { interface LinkDao : Repository<Link, LinkId> {
OUTGOING, fun save(link: Link): Link
INCOMING 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 package wanijo.wanijo2.http.controller
import jakarta.validation.Valid
import org.springframework.data.domain.Sort import org.springframework.data.domain.Sort
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.ui.Model import org.springframework.ui.Model
import org.springframework.ui.set 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.Direction import org.springframework.web.bind.annotation.PostMapping
import wanijo.wanijo2.domain.DocumentBriefDao import wanijo.wanijo2.domain.DocumentBriefDao
import wanijo.wanijo2.domain.DocumentId import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.event.AddLinkCommand import wanijo.wanijo2.domain.event.AddLinkCommand
import wanijo.wanijo2.domain.handler.AddLinkHandler
@Controller @Controller
class LinkController( class LinkController(
val documentBriefDao: DocumentBriefDao, val documentBriefDao: DocumentBriefDao,
val addLinkHandler: AddLinkHandler
) { ) {
@GetMapping("/document/{id}/link/add") @GetMapping("/document/{id}/link/add")
fun addLabel( fun addLink(
@PathVariable @PathVariable
id: DocumentId, id: DocumentId,
model: Model model: Model
@ -28,21 +31,20 @@ class LinkController(
model["addForm"] = AddLinkCommand( model["addForm"] = AddLinkCommand(
id, id,
emptyList(), emptyList(),
Direction.OUTGOING
) )
return "addLink" return "addLink"
} }
/*@PostMapping("/document/{id}/field/label/add") @PostMapping("/document/{id}/link/add")
fun addLabel( fun addLink(
@PathVariable @PathVariable
id: DocumentId, id: DocumentId,
@Valid @Valid
command: AddLabelFieldCommand command: AddLinkCommand
): String { ): String {
addLabelFieldHandler.exec(command) addLinkHandler.exec(command)
return "redirect:/document/$id" return "redirect:/document/$id"
}*/ }
} }

@ -2,22 +2,19 @@ package wanijo.wanijo2.http.controller
import org.commonmark.parser.Parser import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer import org.commonmark.renderer.html.HtmlRenderer
import org.springframework.data.jdbc.core.mapping.AggregateReference
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.ui.Model import org.springframework.ui.Model
import org.springframework.ui.set 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.*
import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.Link
import wanijo.wanijo2.domain.LinkDao
import wanijo.wanijo2.domain.TagDao
import wanijo.wanijo2.http.DocumentNotFound import wanijo.wanijo2.http.DocumentNotFound
@Controller @Controller
class ShowController( class ShowController(
val docDao: DocumentDao, val docDao: DocumentDao,
val linkDao: LinkDao, val enrichedLinkDao: EnrichedLinkDao,
val tagDao: TagDao val tagDao: TagDao
) { ) {
@ -40,8 +37,8 @@ class ShowController(
model["documentTags"] = documentTags.sortedBy { it.name.lowercase() } model["documentTags"] = documentTags.sortedBy { it.name.lowercase() }
model["assignableTags"] = (tagDao.findAll() - documentTags).sortedBy { it.name.lowercase() } model["assignableTags"] = (tagDao.findAll() - documentTags).sortedBy { it.name.lowercase() }
model["incomingLinks"] = emptyList<Link>() model["outgoingLinks"] = enrichedLinkDao.findOutgoing(id)
model["outgoingLinks"] = emptyList<Link>() model["incomingLinks"] = enrichedLinkDao.findIncoming(id)
return "show" return "show"
} }

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

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

Loading…
Cancel
Save