Last active
June 14, 2017 12:10
-
-
Save noctifer20/be5204bd3b2c7f0ae42c8aaad811da98 to your computer and use it in GitHub Desktop.
Express Restful Route
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 Restful from '@project/restful-route'; | |
import conf from './route.conf'; | |
class Route extends Restful { | |
constructor() { | |
super(conf); | |
} | |
callBack(req, res, next) { | |
if(res.error) | |
return res.json(res.error); | |
return res.json(res.result); | |
} | |
customs(){ | |
this.get('customRoute', (res, req) => { | |
return res.json({ | |
message: 'ok' | |
}); | |
}); | |
} | |
} | |
module.exports = Route; |
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
"use strict"; | |
import express, { Router } from 'express'; | |
import _ from 'lodash'; | |
class Restful extens Router { | |
constructor($conf) { | |
this.table = $conf.table; | |
this.sql = $conf.sql; | |
this.AC = $conf.AC; | |
this.customs.call(this); | |
this.get('/', | |
this.auth('GET'), | |
this.getMiddleware || this.next, | |
this.get, | |
this.callBack | |
); | |
this.get('/:id', | |
this.getOneMiddleware || this.next, | |
this.getById, | |
this.callBack | |
); | |
this.post('/', | |
this.auth('ADD'), | |
this.postMiddleware || this.next, | |
this.add, | |
this.callBack.bind(this) | |
); | |
this.delete('/:id', | |
this.auth('DELETE'), | |
this.delete, | |
this.callBack.bind(this) | |
); | |
this.put('/:id', | |
this.auth('UPDATE'), | |
this.update, | |
this.callBack.bind(this) | |
); | |
} | |
auth = ($method) => { | |
return (req, res, next) => { | |
let rule = this.AC[$method]; | |
if (rule) { | |
if (!req.user) { | |
res.status(401); | |
return res.json({"error": 'Unauthorized'}); | |
} | |
if (rule == 'ADMIN' && !req.user.isAdmin) { | |
res.status(401); | |
return res.json({"error": 'Access Denied'}); | |
} | |
} | |
return next(); | |
}; | |
} | |
getById = (req, res, next) => { | |
const $sql = this.sql; | |
const id = req.params.id; | |
let $stmt = db($sql.table); | |
if ($sql.leftJoin) { | |
$sql.leftJoin.forEach(join => $stmt.leftJoin(join[0], join[1], join[2], join[3])); | |
} | |
$stmt.where(`${$sql.table}.id`, id); | |
$stmt.first(); | |
$stmt.select($sql.select); | |
$stmt.then(data => { | |
if (!data) | |
return res.json(); | |
res.result = data; | |
return next(); | |
}, err => { | |
res.error = err; | |
return next(); | |
}); | |
} | |
get = (req, res, next) => { | |
const $sql = this.sql; | |
const where = req.where || true; | |
let $stmt = db($sql.table); | |
if ($sql.leftJoin) { | |
$sql.leftJoin.map(join => { | |
$stmt.leftJoin(join[0], join[1], join[2], join[3]); | |
}); | |
} | |
if ($sql.orderByRaw) { | |
$stmt.orderByRaw($sql.orderByRaw); | |
} else if (req.orderByRaw) { | |
$stmt.orderByRaw(req.orderByRaw); | |
} | |
$stmt.where(where); | |
$stmt.limit(req.query.limit || null); | |
$stmt.offset(req.query.offset || 0); | |
$stmt.select($sql.select); | |
$stmt.then(data => { | |
if (!data) | |
return res.json(); | |
res.result = data; | |
return next(); | |
}, err => { | |
res.status(500); | |
res.error = err; | |
return next(); | |
}); | |
} | |
add = (req, res, next) => { | |
console.log('req.body', req.body); | |
const $sql = this.sql; | |
const query = req.body; | |
let $stmt = db($sql.table); | |
$stmt.returning('*'); | |
$stmt.insert(query); | |
$stmt.then(data => { | |
console.log('data', data); | |
res.result = data[0]; | |
return next(); | |
}, err => { | |
console.log('ERROR', err); | |
res.error = err; | |
res.status(500); | |
return next(); | |
}); | |
} | |
delete = (req, res, next) => { | |
const $sql = this.sql; | |
const id = req.params.id; | |
let $stmt = db($sql.table); | |
$stmt.returning('*'); | |
if (req.user.role != 1) { | |
$stmt.andWhere('owner_id', req.user.id); | |
} | |
$stmt.andWhere('id', id); | |
$stmt.del(); | |
$stmt.then(data => { | |
res.result = data[0] || {}; | |
return next(); | |
}, err => { | |
res.error = err; | |
res.status(500); | |
return next(); | |
}); | |
} | |
update = (req, res, next) => { | |
const $sql = this.sql; | |
const id = req.params.id; | |
const query = req.body; | |
let $stmt = db($sql.table); | |
$stmt.returning('*'); | |
// if (req.user.role != 1) { | |
// $stmt.andWhere('owner_id', req.user.id); | |
// } | |
$stmt.andWhere('id', id); | |
$stmt.andWhere(req.where || true); | |
$stmt.update(query); | |
$stmt.then(data => { | |
res.result = data[0] || {}; | |
return next(); | |
}, err => { | |
res.error = err; | |
res.status(500); | |
return next(); | |
}); | |
} | |
next(req, res, next) { | |
next(); | |
} | |
} | |
module.exports = Restful; |
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
module.exports ={ | |
// routs's path | |
"root": "/foo", | |
"sql": { | |
"table": "foo", | |
"select": ["*"] | |
}, | |
// Acess Control | |
"AC": { | |
"ADD": "USER", | |
"DELETE": "ADMIN", | |
"UPDATE": "USER", | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment