Skip to content

Commit

Permalink
fixed(post): deleting removed captions and chapters on save
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgobich committed Dec 29, 2024
1 parent 9fec009 commit 18fe093
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/actions/posts/sync_captions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class SyncCaptions {
return
}

const postCaptions = await post.related('captions').query().orderBy('sort_order')

const promises = captions.map(async (caption, index) => {
if (!caption.id) {
return post.related('captions').create(caption, { client: trx })
Expand All @@ -31,6 +33,14 @@ export default class SyncCaptions {
return row.save()
})

const deletedCaptionIds = postCaptions
.filter(({ id }) => !captions.find((c) => c.id === id))
.map(({ id }) => id)

if (deletedCaptionIds.length) {
await post.related('captions').query().whereIn('id', deletedCaptionIds).delete()
}

await Promise.all(promises)
}
}
10 changes: 10 additions & 0 deletions app/actions/posts/sync_chapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class SyncChapters {
return
}

const postChapters = await post.related('chapters').query().orderBy('sort_order')

const promises = chapters.map(async (chapter, index) => {
if (!chapter.id) {
return post.related('chapters').create(chapter, { client: trx })
Expand All @@ -31,6 +33,14 @@ export default class SyncChapters {
return row.save()
})

const deletedChapterIds = postChapters
.filter(({ id }) => !chapters.find((c) => c.id === id))
.map(({ id }) => id)

if (deletedChapterIds.length) {
await post.related('chapters').query().whereIn('id', deletedChapterIds).delete()
}

await Promise.all(promises)
}
}

0 comments on commit 18fe093

Please sign in to comment.