delete tags

master
Josha von Gizycki 2 weeks ago
parent db065fafe2
commit 97fe0acdea

@ -1,16 +1,17 @@
package wanijo.wanijo2.domain package wanijo.wanijo2.domain
import org.springframework.data.annotation.Id import org.springframework.data.annotation.Id
import org.springframework.data.annotation.Transient
import org.springframework.data.jdbc.repository.query.Query import org.springframework.data.jdbc.repository.query.Query
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.ZonedDateTime import java.time.ZonedDateTime
typealias DocumentId = Long
@Table("T_DOCUMENT") @Table("T_DOCUMENT")
data class Document( data class Document(
@Id @Id
val id: Long = 0, val id: DocumentId = 0,
val name: String, val name: String,
val description: String = "", val description: String = "",
val updatedAt: ZonedDateTime = ZonedDateTime.now(), val updatedAt: ZonedDateTime = ZonedDateTime.now(),
@ -19,7 +20,7 @@ data class Document(
val dateFields: Set<DateField> = emptySet(), val dateFields: Set<DateField> = emptySet(),
) )
interface DocumentDao: Repository<Document, Int> { interface DocumentDao: Repository<Document, DocumentId> {
fun findAll(): List<Document> fun findAll(): List<Document>
fun findById(id: Long): Document? fun findById(id: Long): Document?
fun findByName(name: String): List<Document> fun findByName(name: String): List<Document>

@ -0,0 +1,15 @@
package wanijo.wanijo2.domain.event
import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotEmpty
import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.TagId
data class DeleteTaggingCommand(
@NotEmpty
@Min(1)
val tagId: TagId,
@NotEmpty
@Min(1)
val documentId: DocumentId
)

@ -0,0 +1,19 @@
package wanijo.wanijo2.domain.handler
import org.springframework.stereotype.Service
import wanijo.wanijo2.domain.DocumentTaggingDao
import wanijo.wanijo2.domain.event.DeleteTaggingCommand
@Service
class DeleteTaggingHandler(
val taggingDao: DocumentTaggingDao
) {
fun exec(command: DeleteTaggingCommand) {
taggingDao.delete(
command.documentId,
command.tagId
)
}
}

@ -10,7 +10,7 @@ import wanijo.wanijo2.domain.TagDao
import wanijo.wanijo2.domain.event.NewDocumentCommand import wanijo.wanijo2.domain.event.NewDocumentCommand
@Service @Service
class NewDocumentCommandHandler( class NewDocumentHandler(
val tagDao: TagDao, val tagDao: TagDao,
val documentDao: DocumentDao, val documentDao: DocumentDao,
val documentTaggingDao: DocumentTaggingDao val documentTaggingDao: DocumentTaggingDao

@ -2,28 +2,33 @@ 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.Modifying
import org.springframework.data.jdbc.repository.query.Query import org.springframework.data.jdbc.repository.query.Query
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.ZonedDateTime import java.time.ZonedDateTime
typealias TagId = Long
@Table("T_TAG") @Table("T_TAG")
data class Tag( data class Tag(
@Id @Id
val id: Long = 0, val id: TagId = 0,
val name: String, val name: String,
val createdAt: ZonedDateTime = ZonedDateTime.now() val createdAt: ZonedDateTime = ZonedDateTime.now()
) )
typealias DocumentTaggingId = Long
@Table("T_DOCUMENT_TAGGING") @Table("T_DOCUMENT_TAGGING")
data class DocumentTagging( data class DocumentTagging(
@Id @Id
val id: Long = 0, val id: DocumentTaggingId = 0,
val tagId: AggregateReference<Tag, Long>, val tagId: AggregateReference<Tag, TagId>,
val documentId: AggregateReference<Document, Long> val documentId: AggregateReference<Document, DocumentId>
) { ) {
companion object { companion object {
fun between(doc: Document, tagId: Long) = fun between(doc: Document, tagId: TagId) =
DocumentTagging( DocumentTagging(
tagId = AggregateReference.to(tagId), tagId = AggregateReference.to(tagId),
documentId = AggregateReference.to(doc.id) documentId = AggregateReference.to(doc.id)
@ -31,11 +36,20 @@ data class DocumentTagging(
} }
} }
interface DocumentTaggingDao : Repository<DocumentTagging, Long> { interface DocumentTaggingDao : Repository<DocumentTagging, DocumentTaggingId> {
fun save(tagging: DocumentTagging) fun save(tagging: DocumentTagging)
@Modifying
@Query(
"""
DELETE FROM t_document_tagging
WHERE document_id = :documentId
AND tag_id = :tagId
"""
)
fun delete(documentId: DocumentId, tagId: TagId)
} }
interface TagDao : Repository<Tag, Long> { interface TagDao : Repository<Tag, TagId> {
fun save(tag: Tag): Tag fun save(tag: Tag): Tag
fun findByName(name: String): Tag? fun findByName(name: String): Tag?
fun findAll(): Set<Tag> fun findAll(): Set<Tag>

@ -10,13 +10,13 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import wanijo.wanijo2.domain.TagDao import wanijo.wanijo2.domain.TagDao
import wanijo.wanijo2.domain.event.NewDocumentCommand import wanijo.wanijo2.domain.event.NewDocumentCommand
import wanijo.wanijo2.domain.handler.NewDocumentCommandHandler import wanijo.wanijo2.domain.handler.NewDocumentHandler
@Controller @Controller
@RequestMapping("/document") @RequestMapping("/document")
class NewController( class NewController(
private val tagDao: TagDao, private val tagDao: TagDao,
private val newDocumentCommandHandler: NewDocumentCommandHandler, private val newDocumentCommandHandler: NewDocumentHandler,
) { ) {
@GetMapping("/new") @GetMapping("/new")

@ -0,0 +1,32 @@
package wanijo.wanijo2.http.controller
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestParam
import wanijo.wanijo2.domain.DocumentId
import wanijo.wanijo2.domain.TagId
import wanijo.wanijo2.domain.event.DeleteTaggingCommand
import wanijo.wanijo2.domain.handler.DeleteTaggingHandler
@Controller
class TaggingController(
val deleteTaggingHandler: DeleteTaggingHandler
) {
@PostMapping("/tagging/delete")
fun delete(
@RequestParam("docId")
documentId: DocumentId,
@RequestParam("tagId")
tagId: TagId
): String {
deleteTaggingHandler.exec(
DeleteTaggingCommand(
tagId = tagId,
documentId = documentId
)
)
return "redirect:/document/${documentId}"
}
}

@ -38,14 +38,14 @@ main {
display: flex; display: flex;
justify-content: space-evenly; justify-content: space-evenly;
li {
border-left: .5rem solid AccentColor;
padding-left: .5rem;
}
} }
} }
.tag-block {
border-left: .5rem solid AccentColor;
padding-left: .5rem;
}
fieldset { fieldset {
border: 1px solid AccentColor; border: 1px solid AccentColor;
margin-bottom: 1rem; margin-bottom: 1rem;
@ -96,3 +96,19 @@ form {
min-height: 10rem; min-height: 10rem;
} }
} }
.form-link {
display: inline;
[type=submit] {
border: none;
background: none;
color: LinkText;
&:hover {
text-decoration: underline;
cursor: pointer;
}
}
}

@ -34,7 +34,14 @@
<legend>tags</legend> <legend>tags</legend>
<ul> <ul>
<li th:each="tag : ${tags}" th:text="${tag.name}"></li> <li th:each="tag : ${tags}" class="tag-block">
<th:block th:text="${tag.name}"/>
<form class="form-link" th:action="@{/tagging/delete}" method="post">
<input type="hidden" name="tagId" th:value="${tag.id}">
<input type="hidden" name="docId" th:value="${document.id}">
<button type="submit">🞭</button>
</form>
</li>
</ul> </ul>
</fieldset> </fieldset>
@ -42,6 +49,8 @@
<legend>beschreibung</legend> <legend>beschreibung</legend>
<div class="show__markdown" th:utext="${descHtml}"></div> <div class="show__markdown" th:utext="${descHtml}"></div>
</fieldset> </fieldset>
<h2 class="low-key-hint">taggen</h2>
</main> </main>
</body> </body>
</html> </html>

Loading…
Cancel
Save