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