fix(api): fix story chapter ids disappearing after an edit
This commit is contained in:
		
							parent
							
								
									f1a5cfffd4
								
							
						
					
					
						commit
						7b8a089d95
					
				| @ -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(), | ||||
| 	}; | ||||
| }); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user