feat(api): add endpoints for retrieving a band's info and its associated stories
This commit is contained in:
		
							parent
							
								
									66d6168a31
								
							
						
					
					
						commit
						8070e8c555
					
				
							
								
								
									
										8
									
								
								server/api/bands/[id]/index.get.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								server/api/bands/[id]/index.get.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | import { Band } from "~/models/band"; | ||||||
|  | 
 | ||||||
|  | export default eventHandler(async (ev) => { | ||||||
|  | 	const id = parseInt(getRouterParam(ev, "id") as string); | ||||||
|  | 	const band = await Band.findById(id).exec(); | ||||||
|  | 
 | ||||||
|  | 	return band; | ||||||
|  | }); | ||||||
							
								
								
									
										28
									
								
								server/api/bands/[id]/stories.get.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								server/api/bands/[id]/stories.get.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,28 @@ | |||||||
|  | import listQuerier from "~/lib/server/dbHelpers/listQuerier"; | ||||||
|  | import { Band } from "~/models/band"; | ||||||
|  | import { Story } from "~/models/stories"; | ||||||
|  | 
 | ||||||
|  | export default eventHandler(async (event) => { | ||||||
|  | 	const params = getRouterParams(event); | ||||||
|  | 	const query = getQuery(event); | ||||||
|  | 	let band = await Band.findById(params.id); | ||||||
|  | 	if (!band) { | ||||||
|  | 		throw createError({ statusCode: 404, message: "not found." }); | ||||||
|  | 	} | ||||||
|  | 	let skipAmt = 20 * (parseInt((query.page as string) || "1") - 1) - 1; | ||||||
|  | 	if (skipAmt < 0) skipAmt = 0; | ||||||
|  | 	let stories = await listQuerier( | ||||||
|  | 		{ | ||||||
|  | 			"chapters.bands": { | ||||||
|  | 				$in: [parseInt(params["id"])], | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		event.context, | ||||||
|  | 	); /*  */ | ||||||
|  | 	console.log(skipAmt); | ||||||
|  | 	return { | ||||||
|  | 		...band.toObject(), | ||||||
|  | 		stories: stories /* .slice(skipAmt, skipAmt + 20 + 1) */, | ||||||
|  | 		total: stories.length, | ||||||
|  | 	}; | ||||||
|  | }); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user