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); } });
/** * 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)) } }
authenticated(async (parent, { title, body }, context) => Post.query().insert({ title, body, author_id: context.user.id }) )
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); } });
describe('subscriptions', () => { describe('POST /subscriptions', () => { it ('should create a subscription', async() => { let user = await User.query().insert({email: 'test@example.com', password_digest: 'test123'}) let token = uuidV4(); let accessGrant = await AccessGrant.query().insert({token: token, user_id:user.id}) let podcast = await Podcast.query().insert({title: 'This is a sample podcast', description: 'description'}) let res = await chai .request(server) .post('/subscriptions') .set('Authorization', 'Bearer ' + accessGrant.token) .send({user_id: user.id, podcast_id: podcast.id}) res.should.have.status(200); res.body.user_id.should.eq(user.id) res.body.podcast_id.should.eq(podcast.id) }); }); });
router.post('podcasts.create', '/podcasts', api.version({'1.0.0': async (ctx, next) => { const { body } = ctx.request; console.log(ctx.state.user) var {error, value} = Joi.validate(body, PodcastSchema); if (error) { console.error(error); ctx.throw(400); } let podcast = {title: body.name, description: body.description, website: body.website, author: body.author, thumbnail: body.thumbnail, user_id: ctx.state.user.id} try { let response = await Podcast.query().insert(podcast) ctx.body = response; } catch(err) { console.error(err); ctx.throw(422); } }}));
async function main() { await Person.query().insert({ name: 'John' }) console.log(await Person.query().where('name', 'John')) console.log(await knex.raw('SELECT * FROM persons')) }
router.post('/users', async(ctx) => { const { body } = ctx.request; if (body.password != body.password_confirmation) { ctx.throw(400); } let passwordDigest = body.password; let user = {email: body.email, password_digest: passwordDigest} try { let user_res = await User.query().insert(user); let token = uuidV4(); let accessGrant = {token: token, user_id: user_res.id } let token_res = await AccessGrant.query().insert(accessGrant) ctx.body = {'user': user_res, 'token': token} } catch(err) { console.error(err); ctx.throw(422); } });
authenticated(async (parent, args, context) => Comment.query().insert({ body: args.body, post_id: args.post_id, author_id: context.user.id }) )