parent
ae0a87b53d
commit
7bc290a3ad
@ -0,0 +1,11 @@
|
|||||||
|
package wanijo.wanijo2.domain.event
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty
|
||||||
|
|
||||||
|
data class NewDocumentCommand(
|
||||||
|
@NotEmpty
|
||||||
|
val name: String = "",
|
||||||
|
val description: String = "",
|
||||||
|
val tagIds: List<Long> = emptyList(),
|
||||||
|
val newTags: String = ""
|
||||||
|
)
|
@ -0,0 +1,49 @@
|
|||||||
|
package wanijo.wanijo2.domain.handler
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import wanijo.wanijo2.domain.Document
|
||||||
|
import wanijo.wanijo2.domain.DocumentDao
|
||||||
|
import wanijo.wanijo2.domain.DocumentTagging
|
||||||
|
import wanijo.wanijo2.domain.DocumentTaggingDao
|
||||||
|
import wanijo.wanijo2.domain.Tag
|
||||||
|
import wanijo.wanijo2.domain.TagDao
|
||||||
|
import wanijo.wanijo2.domain.event.NewDocumentCommand
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class NewDocumentCommandHandler(
|
||||||
|
val tagDao: TagDao,
|
||||||
|
val documentDao: DocumentDao,
|
||||||
|
val documentTaggingDao: DocumentTaggingDao
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun exec(event: NewDocumentCommand): Document {
|
||||||
|
val document = documentDao.save(
|
||||||
|
Document(
|
||||||
|
name = event.name,
|
||||||
|
description = event.description
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
val tags = event.newTags
|
||||||
|
.split(",")
|
||||||
|
.map { it.trim() }
|
||||||
|
.map { it to tagDao.findByName(it) }
|
||||||
|
.map { (name, tag) ->
|
||||||
|
if (tag == null) {
|
||||||
|
tagDao.save(Tag(name = name))
|
||||||
|
} else {
|
||||||
|
tag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val tagIdsToAttach = tags.map { it.id } + event.tagIds
|
||||||
|
tagIdsToAttach.forEach {
|
||||||
|
documentTaggingDao.save(
|
||||||
|
DocumentTagging.between(document, it)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return document
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
package wanijo.wanijo2.http.form
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotEmpty
|
|
||||||
|
|
||||||
data class NewForm (
|
|
||||||
@NotEmpty
|
|
||||||
val name: String = "",
|
|
||||||
val description: String = "",
|
|
||||||
val tagIds: List<Long> = emptyList()
|
|
||||||
)
|
|
Loading…
Reference in new issue