From 7b8a089d956b124720e8ee344e51c395ce3378d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Sat, 16 Mar 2024 20:03:18 -0400 Subject: [PATCH] fix(api): fix story chapter ids disappearing after an edit --- server/api/story/[id]/index.put.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/server/api/story/[id]/index.put.ts b/server/api/story/[id]/index.put.ts index 68e7a63..79cd177 100644 --- a/server/api/story/[id]/index.put.ts +++ b/server/api/story/[id]/index.put.ts @@ -22,7 +22,6 @@ export default eventHandler(async (ev) => { title: body.title, completed: body.completed, coAuthor: !!body.coAuthor ? body.coAuthor : null, - chapters: [], }; for (const oc of os.chapters) { let filename = `/stories/${oc.id}.txt`; @@ -32,24 +31,31 @@ export default eventHandler(async (ev) => { await bucket.delete(d._id); } } + const cc = os.chapters; + os.chapters = []; + await os.save(); for (const c of body.chapters) { - let idx = os.chapters.findIndex((k) => k.id === c.id); + let idx = cc.findIndex((k) => k.id === c.id); const cont = await bodyHandler(c); if (idx === -1) { - update.chapters!.push({ + os.chapters!.push({ ...modelFormChapter(c), posted: new Date(Date.now()), }); } else { - update.chapters!.push({ + os.chapters!.push({ ...modelFormChapter(c), - id: os.chapters[idx].id, - posted: os.chapters[idx].posted, + // id: os.chapters[idx].id, + words: countWords(cont), + posted: cc[idx].posted, }); } - - await replaceOrUploadContent(os.chapters![idx]?.id ?? c._id, cont); - update.chapters![update.chapters!.length - 1].words = countWords(cont); + } + await os.save(); + for (let i = 0; i < os.chapters.length; i++) { + const c = os.chapters[i]; + const cont = await bodyHandler(body.chapters[i]); + await replaceOrUploadContent(c.id ?? c._id, cont); } os = await Story.findOneAndUpdate( { @@ -66,6 +72,6 @@ export default eventHandler(async (ev) => { } return { success: true, - data: os.toObject(), + story: os.toObject(), }; });