Last active
November 23, 2015 11:28
-
-
Save NikolayGalkin/811e59bb33c879b8575c to your computer and use it in GitHub Desktop.
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 route from 'koa-route'; | |
import glob from 'glob'; | |
export default app => { | |
glob.sync('../controllers/*.js', {cwd: __dirname}).map(file => { | |
const Item = require(file); // How to import via `import * from file` ? | |
for (const key in Item.default.routes || {}) { | |
const [method, path] = key.split(' '); | |
app.use(route[method](path, Item.default[Item.default.routes[key]])); | |
} | |
}); | |
return async (ctx, next) => { | |
await next(); | |
}; | |
}; |
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
// Works correctly! | |
export default { | |
routes: { | |
'get /events': 'list' | |
}, | |
list: async (ctx) => { | |
const result = await ctx.db.raw('SELECT * FROM events_list(?, ?)', [ctx.query.page, ctx.query.limit]); | |
ctx.body = result.rows; | |
} | |
}; |
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
function list(ctx) { | |
var _this = this; | |
return _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { | |
return regeneratorRuntime.wrap(function _callee5$(_context5) { | |
while (1) { | |
switch (_context5.prev = _context5.next) { | |
case 0: | |
_context5.next = 2; | |
return ctx.db.raw('SELECT * FROM events_list(?, ?)', [ctx.query.page, ctx.query.limit]); | |
case 2: | |
result = _context5.sent; | |
ctx.body = result.rows; | |
case 4: | |
case 'end': | |
return _context5.stop(); | |
} | |
} | |
}, _callee5, _this); | |
}))(); | |
} |
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
export default class Events { | |
static routes = { | |
'get /events': 'list', | |
'get /events/count': 'count', | |
'get /events/:id': 'detail', | |
'post /events': 'create' | |
} | |
static async list(ctx) { | |
this.result = await ctx.db.raw('SELECT * FROM events_list(?, ?)', [ctx.query.page, ctx.query.limit]); | |
ctx.body = this.result.rows; | |
} | |
static async detail(ctx, id) { | |
this.result = await ctx.db.raw('SELECT * FROM events_detail(?)', [id]); | |
ctx.body = this.result.rows[0]; | |
} | |
static async create(ctx) { | |
await ctx.db.raw('SELECT * FROM events_create(?, ?)', [ctx.request.body.webhookEvent, ctx.request.body]); | |
ctx.body = {}; | |
} | |
static async count(ctx) { | |
this.result = await ctx.db.raw('SELECT * FROM events_count()'); | |
ctx.body = this.result.rows[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Но без переменной работает: