You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
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
|
|
}
|
|
|
|
}
|