fix(api): fix story chapter ids disappearing after an edit

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2024-03-16 20:03:18 -04:00
parent f1a5cfffd4
commit 7b8a089d95
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

View File

@ -22,7 +22,6 @@ export default eventHandler(async (ev) => {
title: body.title, title: body.title,
completed: body.completed, completed: body.completed,
coAuthor: !!body.coAuthor ? body.coAuthor : null, coAuthor: !!body.coAuthor ? body.coAuthor : null,
chapters: [],
}; };
for (const oc of os.chapters) { for (const oc of os.chapters) {
let filename = `/stories/${oc.id}.txt`; let filename = `/stories/${oc.id}.txt`;
@ -32,24 +31,31 @@ export default eventHandler(async (ev) => {
await bucket.delete(d._id); await bucket.delete(d._id);
} }
} }
const cc = os.chapters;
os.chapters = [];
await os.save();
for (const c of body.chapters) { 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); const cont = await bodyHandler(c);
if (idx === -1) { if (idx === -1) {
update.chapters!.push({ os.chapters!.push({
...modelFormChapter(c), ...modelFormChapter(c),
posted: new Date(Date.now()), posted: new Date(Date.now()),
}); });
} else { } else {
update.chapters!.push({ os.chapters!.push({
...modelFormChapter(c), ...modelFormChapter(c),
id: os.chapters[idx].id, // id: os.chapters[idx].id,
posted: os.chapters[idx].posted, words: countWords(cont),
posted: cc[idx].posted,
}); });
} }
}
await replaceOrUploadContent(os.chapters![idx]?.id ?? c._id, cont); await os.save();
update.chapters![update.chapters!.length - 1].words = countWords(cont); 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( os = await Story.findOneAndUpdate(
{ {
@ -66,6 +72,6 @@ export default eventHandler(async (ev) => {
} }
return { return {
success: true, success: true,
data: os.toObject(), story: os.toObject(),
}; };
}); });