Created
January 25, 2018 22:37
-
-
Save bludnic/6aeeb26d8fcc3ee8ac4b3ba7535ff50d to your computer and use it in GitHub Desktop.
Koa.js router example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Router from 'koa-router' | |
import Tour from './../models/tour' | |
import TourController from './../controllers/tour' | |
import setLang from './../middleware/setLang' | |
import { toLocalized, toLocalized2, bodyToLocalized, localizedToBody } from './../helpers/i18n' | |
const router = new Router() | |
/* | |
* List Tours. | |
*/ | |
router.get('/tour', async ctx => { | |
const tours = await Tour.find({}) | |
ctx.body = tours | |
}) | |
/* | |
* Create a Tour. | |
*/ | |
router.post('/tour', async ctx => { | |
try { | |
const body = bodyToLocalized(ctx.request.body, ['name', 'description', 'plan', 'included'], ctx.lang) | |
const tour = new Tour(body) | |
// return ctx.body = body | |
// tour.set('name.' + ctx.lang, ctx.request.body.name) | |
// tour.set('description.' + ctx.lang, ctx.request.body.description) | |
// tour.set('type', ctx.request.body.type) | |
// tour.set('price', ctx.request.body.price) | |
// const included = ctx.request.body.included.map(item => { | |
// const keys = Object.keys(item) | |
// let items = {} | |
// keys.map(key => items[key+'.'+ctx.lang] = item[key]) | |
// return items | |
// }) | |
// tour.set('included', included) | |
const result = await tour.save() | |
ctx.body = result | |
} catch(err) { | |
ctx.body = err | |
} | |
}) | |
/* | |
* Retrieve a Tour. | |
*/ | |
router.get('/tour/:id', async ctx => { | |
const tour = await Tour.findById(ctx.params.id) | |
const response = localizedToBody(tour.toObject(), ['name', 'description', 'plan', 'included'], ctx.lang) | |
//ctx.body = toLocalized(tour.toObject(), ctx.lang) | |
ctx.body = response | |
}) | |
/* | |
* Update a Tour. | |
*/ | |
router.put('/tour/:id', async ctx => { | |
try { | |
//return ctx.body = ctx.request.bodyLang | |
// const id = ctx.params.id | |
// const body = ctx.request.body | |
// const lang = ctx.lang | |
// const fields = ['name', 'description'] | |
// const bodyFiltered = TourController.filter({ id, body, lang, fields }) | |
const body = bodyToLocalized(ctx.request.body, ['name', 'description', 'plan', 'included'], ctx.lang) | |
const result = await Tour.findByIdAndUpdate(ctx.params.id, body) | |
if (result) return ctx.body = { message: 'Item updated Succesfully' } | |
ctx.body = { error: 'Item not exists' } | |
} catch(err) { | |
ctx.body = err | |
} | |
}) | |
/* | |
* Delete a Tour. | |
*/ | |
router.delete('/tour/:id', async ctx => { | |
const result = await Tour.findByIdAndRemove(ctx.params.id) | |
ctx.body = result | |
}) | |
/* | |
* Mass action. | |
*/ | |
router.patch('/tour', async ctx => { | |
const action = ctx.request.body.action | |
const ids = ctx.request.body.ids | |
if (action === 'delete') { | |
try { | |
const result = await Tour.remove({ _id: { $in: ids } }) | |
ctx.body = result | |
} catch(err) { | |
ctx.body = err | |
} | |
} else { | |
ctx.body = { error: 'No action' } | |
} | |
}) | |
export default router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment