@@ -69,8 +69,7 @@ internal class AnnouncementRepository(private val database: Database) {
6969
7070 fun latestId () = latestAnnouncement?.id?.value.toApiResponseAnnouncementId()
7171
72- fun latestId (tags : Set <String >) =
73- tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
72+ fun latestId (tags : Set <String >) = tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
7473
7574 suspend fun paged (cursor : Int , count : Int , tags : Set <String >? ) = transaction {
7675 Announcement .find {
@@ -103,11 +102,13 @@ internal class AnnouncementRepository(private val database: Database) {
103102 createdAt = new.createdAt
104103 archivedAt = new.archivedAt
105104 level = new.level
106- tags = SizedCollection (
107- new.tags.map { tag -> Tag .find { Tags .name eq tag }.firstOrNull() ? : Tag .new { name = tag } },
108- )
105+ if (new.tags != null ) {
106+ tags = SizedCollection (
107+ new.tags.map { tag -> Tag .find { Tags .name eq tag }.firstOrNull() ? : Tag .new { name = tag } },
108+ )
109+ }
109110 }.apply {
110- new.attachments.map { attachmentUrl ->
111+ new.attachments? .map { attachmentUrl ->
111112 Attachment .new {
112113 url = attachmentUrl
113114 announcement = this @apply
@@ -125,24 +126,28 @@ internal class AnnouncementRepository(private val database: Database) {
125126 it.archivedAt = new.archivedAt
126127 it.level = new.level
127128
128- // Get the old tags, create new tags if they don't exist,
129- // and delete tags that are not in the new tags, after updating the announcement.
130- val oldTags = it.tags.toList()
131- val updatedTags = new.tags.map { name ->
132- Tag .find { Tags .name eq name }.firstOrNull() ? : Tag .new { this .name = name }
133- }
134- it.tags = SizedCollection (updatedTags)
135- oldTags.forEach { tag ->
136- if (tag in updatedTags || ! tag.announcements.empty()) return @forEach
137- tag.delete()
129+ if (new.tags != null ) {
130+ // Get the old tags, create new tags if they don't exist,
131+ // and delete tags that are not in the new tags, after updating the announcement.
132+ val oldTags = it.tags.toList()
133+ val updatedTags = new.tags.map { name ->
134+ Tag .find { Tags .name eq name }.firstOrNull() ? : Tag .new { this .name = name }
135+ }
136+ it.tags = SizedCollection (updatedTags)
137+ oldTags.forEach { tag ->
138+ if (tag in updatedTags || ! tag.announcements.empty()) return @forEach
139+ tag.delete()
140+ }
138141 }
139142
140143 // Delete old attachments and create new attachments.
141- it.attachments.forEach { attachment -> attachment.delete() }
142- new.attachments.map { attachment ->
143- Attachment .new {
144- url = attachment
145- announcement = it
144+ if (new.attachments != null ) {
145+ it.attachments.forEach { attachment -> attachment.delete() }
146+ new.attachments.map { attachment ->
147+ Attachment .new {
148+ url = attachment
149+ announcement = it
150+ }
146151 }
147152 }
148153 }?.let (::updateLatestAnnouncement) ? : Unit
@@ -175,8 +180,7 @@ internal class AnnouncementRepository(private val database: Database) {
175180 Tag .all().toList().toApiTag()
176181 }
177182
178- private suspend fun <T > transaction (statement : suspend Transaction .() -> T ) =
179- newSuspendedTransaction(Dispatchers .IO , database, statement = statement)
183+ private suspend fun <T > transaction (statement : suspend Transaction .() -> T ) = newSuspendedTransaction(Dispatchers .IO , database, statement = statement)
180184
181185 private object Announcements : IntIdTable() {
182186 val author = varchar(" author" , 32 ).nullable()
0 commit comments