13 changed files with 114 additions and 144 deletions
@ -1,24 +1,57 @@ |
|||
'use strict'; |
|||
|
|||
import mongoose from 'mongoose'; |
|||
|
|||
import { SiteError } from '../../../lib/site-lib.js'; |
|||
|
|||
/* |
|||
* This is a sample populator. It doesn't run. There is no sample service, etc. |
|||
* This is simply the pattern you follow to declare a new ExpressJS parameter |
|||
* populator and export it from your populators library. |
|||
*/ |
|||
export function populateSampleParameter (controller) { |
|||
return async function (req, res, next, sampleParameter) { |
|||
const { sample: sampleService } = controller.dtp.services; |
|||
export function populateImageId (controller) { |
|||
const { image: imageService } = controller.dtp.services; |
|||
return async function (req, res, next, imageId) { |
|||
try { |
|||
res.locals.imageId = mongoose.Types.ObjectId.createFromHexString(imageId); |
|||
res.locals.image = await imageService.getImageById(res.locals.imageId); |
|||
if (!res.locals.image) { |
|||
throw new SiteError(404, 'Image not found'); |
|||
} |
|||
return next(); |
|||
} catch (error) { |
|||
controller.log.error('failed to populate imageId', { imageId, error }); |
|||
return next(error); |
|||
} |
|||
}; |
|||
} |
|||
|
|||
export function populateUserId (controller) { |
|||
const { user: userService } = controller.dtp.services; |
|||
return async function (req, res, next, userId) { |
|||
if (!mongoose.Types.ObjectId.isValid(userId)) { |
|||
return next(new SiteError(406, 'Invalid User')); |
|||
} |
|||
try { |
|||
res.locals.userProfile = await userService.getUserAccount(userId); |
|||
if (!res.locals.userProfile) { |
|||
throw new SiteError(404, 'User not found'); |
|||
} |
|||
return next(); |
|||
} catch (error) { |
|||
controller.log.error('failed to populate userId', { userId, error }); |
|||
return next(error); |
|||
} |
|||
}; |
|||
} |
|||
|
|||
export function populateVideoId (controller) { |
|||
const { video: videoService } = controller.dtp.services; |
|||
return async function (req, res, next, videoId) { |
|||
try { |
|||
res.locals.sample = await sampleService.getSample(sampleParameter); |
|||
if (!res.locals.sample) { |
|||
throw new SiteError(404, 'Sample not found'); |
|||
res.locals.video = await videoService.getVideoById(videoId); |
|||
if (!res.locals.video) { |
|||
throw new SiteError(404, 'Video not found'); |
|||
} |
|||
return next(); |
|||
} catch (error) { |
|||
controller.log.error('failed to populate sampleParameter', { sampleParamater, error }); |
|||
controller.log.error('failed to populate video', { videoId, error }); |
|||
return next(error); |
|||
} |
|||
}; |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue