refactor(api/utils): add story query helper function
to avoid having to write the same `populate` calls over and over
This commit is contained in:
		
							parent
							
								
									bef6e79317
								
							
						
					
					
						commit
						dd9fcaf5ae
					
				
							
								
								
									
										23
									
								
								lib/server/dbHelpers/storyQuerier.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								lib/server/dbHelpers/storyQuerier.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					import type { H3Event, EventHandlerRequest } from "h3";
 | 
				
			||||||
 | 
					import { Challenge } from "~/models/challenges/gen";
 | 
				
			||||||
 | 
					import { Story } from "~/models/stories";
 | 
				
			||||||
 | 
					export default async function (ev: H3Event<EventHandlerRequest>) {
 | 
				
			||||||
 | 
						const id = parseInt(getRouterParam(ev, "id") as string);
 | 
				
			||||||
 | 
						const chapterIndex = ev.context.chapterIndex;
 | 
				
			||||||
 | 
						if (isNaN(id) || isNaN(chapterIndex))
 | 
				
			||||||
 | 
							throw createError({
 | 
				
			||||||
 | 
								statusCode: 404,
 | 
				
			||||||
 | 
								message: "Not found.",
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						const story = await Story.findById(id)
 | 
				
			||||||
 | 
							.populate("author", "username profile blocked")
 | 
				
			||||||
 | 
							.populate("chapters.bands")
 | 
				
			||||||
 | 
							.populate({
 | 
				
			||||||
 | 
								path: "ficmas",
 | 
				
			||||||
 | 
								populate: { path: "wisher", select: "_id username" },
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
							.populate({ path: "challenge", model: Challenge })
 | 
				
			||||||
 | 
							.exec();
 | 
				
			||||||
 | 
						if (!story) throw createError({ statusCode: 404, message: "Not found." });
 | 
				
			||||||
 | 
						return story;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user