/** * Create new user. */ async create(req, res, next) { try { const user = await User.query().insert(req.body) return res.json(success('Successfully added new user.', user)) } catch (excepetion) { console.log(excepetion) return res.json(error(500)) } }
whereByOr(obj) { this.where((q) => { _.forEach(obj, (value, key) => { q.orWhere(key, value); }); }); return this; }
router.post('/subscriptions', async(ctx) => { const { body } = ctx.request; let subscription = { user_id: body.user_id, podcast_id: body.podcast_id } try { let response = await Subscription.query().insert(subscription) ctx.body = response; } catch(err) { console.error(err); ctx.throw(422); } });
/** * this is equivalent to `this.where(column, values).orderByArrayPos(column, values)` * use this for returning results ordered in the way you gave them */ whereInOrdered(column, values) { this.whereIn(column, values).orderByArrayPos(column, values); return this; }
static getDeleteByIdResolver() { return (async (root, obj) => this.query().deleteById(obj[this.idColumn]) .then(() => ({id: obj[this.idColumn]}))); }
async show(req, res) { try { const user = await User.query().findById(parseInt(req.params.id)) if (!user) { return res.json(error(400, 'User not found')) } return res.json(success('User details fetched successfully.', user)) } catch (excepetion) { logger.error(excepetion) return res.json(error(500)) } }
authenticated(async (parent, args, context) => Comment.query().insert({ body: args.body, post_id: args.post_id, author_id: context.user.id }) )
router.post('/access_grants', async(ctx) => { const { body } = ctx.request; let token = uuidV4(); let accessGrant = {token: token, user_id: 1} try { let response = await AccessGrant.query().insert(accessGrant) ctx.body = response; } catch(err) { console.error(err); ctx.throw(422); } });
whereByAnd(obj) { this.orWhere((q) => { _.forEach(obj, (value, key) => { q.where(key, value); }); }); return this; }
router.delete('podcasts.delete', '/podcasts/:id', api.version({'1.0.0': async (ctx, next) => { try { response = await Podcast.query().deleteById(ctx.params.id); ctx.body = response; } catch(err) { console.error(err); ctx.throw(422); }; }}));
router.get('/users', (req, res) => { User.query() .then(users => { res.json(users) }) })