/** * Update a role by id * @return {object} body * @swagger * /roles/{id}: * put: * operationId: updateRolesV1 * summary: Update role by id * parameters: * - name: id * in: path * description: ID of role to update * type: integer * required: true * tags: * - Roles * responses: * 200: * description: Update role by id * schema: * $ref: '#/definitions/Role' */ function *update() { var body = this.request.body; try { var role = yield Roles.findById(this.params.id); if (!role) { role = yield Roles.find({ where: {id: this.params.id}, paranoid: false }); } for (let key in body) { if (body.hasOwnProperty(key) && key !== 'id') { role[key] = body[key]; } } if (role.deletedAt && role.isActive) { yield role.restore(); } yield role.save(); this.body = role; } catch (e) { logger.error('Error: ', e.stack); this.status = 500; this.body = { error: true , msg: e.errors || e.message }; } return this.body; }
/** * Get a role by id * @return {object} body * @swagger * /roles/{id}: * get: * operationId: listRolesByIdV1 * summary: List Roles by id * parameters: * - name: id * in: path * description: ID of role to fetch * required: true * type: integer * format: int64 * tags: * - Roles * security: * - Authorization: [] * responses: * 200: * description: Roles by ID * schema: * $ref: '#/definitions/Role' */ function *byId() { logger.log('ID: ', this.params.id); try { var role = JSON.parse( JSON.stringify( yield Roles.findById( this.params.id ) ) ); this.body = role; } catch (e) { switch (e.name) { case 'TypeError': this.status = 404; break; default: this.status = 500; this.body = { error: true , msg: e.message }; } } return this.body; }
before(function(done) { user = db.User .build({ firstName: 'Full', lastName: 'Name', displayName: 'Full Name', email: '*****@*****.**', username: '******', password: '******', provider: 'local' }); // Get roles db.Role .findAll () .then(function(roles) { _.each(roles, function(value) { if (value.name === 'admin') { roleAdmin = value.id; } else if (value.name === 'user') { roleUser = value.id; } }); }) .catch(function(err) { return done(err); }); user.save() .then(function(user){ user.addRoles([roleUser, roleAdmin]) .then(function(roles) { //console.log(user.dataValues.id); study = db.Study .build({ title: 'Study Title', content: 'Study Content', userId: user.dataValues.id }); done(); }) .catch(function(err) { return done(err); }); }) .catch(function(err) { return done(err); }); });
function findRoles () { //get the respected role instances // Create an object to use with the $or operator var or = _.map(roles, function (role) { return {name : role}; }); var roleQuery = { where: { $or: or } }; return db.Role.findAll(roleQuery); }
function addDefaultRole (user) { // Get default role from database var defaultRoleQuery = { where: { name: 'user' } }; return db.Role.findOne(defaultRoleQuery) .then(setRole); function setRole (role) { return user.addRole(role); } }
router.get('/', async function (ctx, next) { let query = ctx.request.query, id = query.id; if (!id){ return response.error(ctx, { message: '参数错误'}); } await models.Role.findOne({ where: { id: id }, include: [models.Route] }).then((result) => { response.success(ctx, result || {}); }, (err) => { console.log(err); response.sqlError(ctx, err); }) })
/** * Delete a role by id * @return {object} body * @swagger * /roles/{id}: * delete: * operationId: deleteRolesV1 * summary: Remove Roles by id * parameters: * - name: id * in: path * required: true * description: ID of role to delete * type: integer * tags: * - Roles * responses: * 204: * description: Delete role by id */ function *remove() { try { var role = yield Roles.findById(this.params.id); role.isActive = false; yield role.save(); yield role.destroy(); this.status = 204; } catch (e) { this.status = 500; this.body = { error: true , msg: e.errors || e.message }; } return this.body; }
/** * Create a new role * @return {object} body * @swagger * /roles: * post: * operationId: createRolesV1 * summary: Creates a new Roles. * produces: * - application/json * tags: * - Roles * security: * - Authorization: [] * parameters: * - name: Roles * in: body * required: true * schema: * $ref: '#/definitions/Role' * responses: * 200: * description: Creates a role * schema: * $ref: '#/definitions/Role' */ function *create() { var body = this.request.body; body.level = 1; try { var res = yield Roles.create(body); logger.log('Res: ', res); this.status = 201; delete res.password; this.body = res; } catch (e) { this.status = 400; this.body = { error: true , msg: e.errors || 'Invalid Input' }; } return this.body; }
router.get('/', async function (ctx, next) { let body = ctx.request.query, where, keyword = body.keyword; if(keyword){ where = { name: { $like: '%' + keyword + '%' } } } await models.Role.findAll({ where: where, raw: true }).then((result) => { response.success(ctx, result); }, (err) => { response.sqlError(ctx, err); }) })
/** * Get all roles * @return {object} body * @swagger * /roles: * get: * operationId: listRolesV1 * summary: List Roles * produces: * - application/json * tags: * - Roles * security: * - Authorization: [] * responses: * 200: * description: Roles get all * schema: * type: array * items: * $ref: '#/definitions/Role' */ function *all() { try { var roles = JSON.parse( JSON.stringify( yield Roles.findAll({}) ) ); this.status = 200; this.body = roles; } catch (e) { logger.error('Error: ', e.stack || e); this.status = 500; this.body = { error: true , msg: 'Error returning roles' , develeoperMsg: e.message }; } return this.body; }
before(function(done) { // Get application app = express.init(db.sequelize); agent = request.agent(app); // Get roles db.Role .findAll() .then(function(roles) { _.each(roles, function(value) { if (value.name === 'admin') { roleAdmin = value.id; } else if (value.name === 'user') { roleUser = value.id; } }); done(); }) .catch(function(err) { return done(err); }); });