Skip to content

Commit

Permalink
Merge branch 'master' into uiAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
iamigo committed Jul 28, 2017
2 parents f9b2ecb + 21512cd commit 40dd8ae
Show file tree
Hide file tree
Showing 18 changed files with 1,415 additions and 52 deletions.
71 changes: 62 additions & 9 deletions api/v1/controllers/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
*/
'use strict'; // eslint-disable-line strict

const helper = require('../helpers/nouns/generators');
const userProps = require('../helpers/nouns/users');
const doDeleteOneAssoc =
require('../helpers/verbs/doDeleteOneBToMAssoc');
const doPostAssoc =
require('../helpers/verbs/doPostBToMAssoc');
const doFind = require('../helpers/verbs/doFind');
const doGet = require('../helpers/verbs/doGet');
const doPatch = require('../helpers/verbs/doPatch');
const doPost = require('../helpers/verbs/doPost');
const doDelete = require('../helpers/verbs/doDelete');
const doPut = require('../helpers/verbs/doPut');
const u = require('../helpers/verbs/utils');
const httpStatus = require('../constants').httpStatus;

module.exports = {

/**
Expand All @@ -23,7 +38,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
findGenerators(req, res, next) {
res.send({ status: 200, text: 'Get all generators' });
doFind(req, res, next, helper);
},

/**
Expand All @@ -36,7 +51,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
getGenerator(req, res, next) {
res.send({ status: 200, text: 'Get a generator' });
doGet(req, res, next, helper);
},

/**
Expand All @@ -49,7 +64,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
patchGenerator(req, res, next) {
res.send({ status: 200, text: 'PATCH a generator' });
doPatch(req, res, next, helper);
},

/**
Expand All @@ -62,7 +77,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
postGenerator(req, res, next) {
res.send({ status: 201, text: 'POST a generator' });
doPost(req, res, next, helper);
},

/**
Expand All @@ -75,7 +90,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
putGenerator(req, res, next) {
res.send({ status: 200, text: 'PUT a generator' });
doPut(req, res, next, helper);
},

/**
Expand All @@ -88,7 +103,18 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
getGeneratorWriters(req, res, next) {
res.send({ status: 200, text: 'Get writers for generator Y' });
const resultObj = { reqStartTime: new Date() };
const params = req.swagger.params;
const options = {};
u.findAssociatedInstances(helper,
params, helper.belongsToManyAssoc.users, options)
.then((o) => {
resultObj.dbTime = new Date() - resultObj.reqStartTime;

const retval = u.responsify(o, helper, req.method);
u.logAPI(req, resultObj, retval);
res.status(httpStatus.OK).json(retval);
}).catch((err) => u.handleError(next, err, helper.modelName));
},

/**
Expand All @@ -102,7 +128,23 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
getGeneratorWriter(req, res, next) {
res.send({ status: 200, text: 'Get writer X for generator Y' });
const resultObj = { reqStartTime: new Date() };
const params = req.swagger.params;
const options = {};
options.where = u.whereClauseForNameOrId(params.userNameOrId.value);
u.findAssociatedInstances(helper,
params, helper.belongsToManyAssoc.users, options)
.then((o) => {
resultObj.dbTime = new Date() - resultObj.reqStartTime;

// throw ResourceNotFound error if resolved object is empty array
u.throwErrorForEmptyArray(o,
params.userNameOrId.value, userProps.modelName);
const retval = u.responsify(o, helper, req.method);
u.logAPI(req, resultObj, retval);
res.status(httpStatus.OK).json(retval);
})
.catch((err) => u.handleError(next, err, helper.modelName));
},

/**
Expand All @@ -117,7 +159,16 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
postGeneratorWriters(req, res, next) {
res.send({ status: 201, text: 'Post generator writer' });
const params = req.swagger.params;
const toPost = params.queryBody.value;
const options = {};
options.where = u.whereClauseForNameInArr(toPost);
userProps.model.findAll(options)
.then((usrs) => {
doPostAssoc(req, res, next, helper,
helper.belongsToManyAssoc.users, usrs);
})
.catch((err) => u.handleError(next, err, helper.modelName));
},

/**
Expand All @@ -132,6 +183,8 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
deleteGeneratorWriter(req, res, next) {
res.send({ status: 204, text: 'Delete generator writer' });
const userNameOrId = req.swagger.params.userNameOrId.value;
doDeleteOneAssoc(req, res, next, helper,
helper.belongsToManyAssoc.users, userNameOrId);
},
}; // exports
34 changes: 34 additions & 0 deletions api/v1/helpers/nouns/generators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

/**
* api/v1/helpers/nouns/generators.js
*/
'use strict';

const Generator = require('../../../../db/index').Generator;

const m = 'generator';

module.exports = {
apiLinks: {
DELETE: `Delete this ${m}`,
GET: `Retrieve this ${m}`,
PATCH: `Update selected attributes of this ${m}`,
POST: `Create a new ${m}`,
PUT: `Overwrite all attributes of this ${m}`,
},
baseUrl: '/v1/generators',
model: Generator,
modelName: 'Generator',

// define the associations that are to be deleted here
belongsToManyAssoc: {
users: 'writers',
},
}; // exports
9 changes: 5 additions & 4 deletions api/v1/helpers/verbs/doPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ function doPost(req, res, next, props) {
postPromise = u.createSample(req, props);
} else {

// cache is off and returnUser is false.
// not a sample
/**
* cache is off and returnUser is false.
* not a sample
*/
postPromise = props.model.create(toPost);
}

Expand All @@ -111,8 +113,7 @@ function doPost(req, res, next, props) {
.then(() => res.status(httpStatus.CREATED).json(
u.responsify(o, props, req.method)));
} else {
return res.status(httpStatus.CREATED)
.json(u.responsify(o, props, req.method));
return res.status(httpStatus.CREATED).json(u.responsify(o, props, req.method));
}
})
.catch((err) => u.handleError(next, err, props.modelName));
Expand Down
22 changes: 13 additions & 9 deletions api/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,7 @@ paths:
# ---------------------------------------------------------------------------

/generators:
x-swagger-router-controller: generators
get:
security:
- jwt: []
Expand Down Expand Up @@ -1949,11 +1950,9 @@ paths:
The url where a user can go to get more help about the generator.
name:
type: string
readOnly: true
description: The generator's name.
subjectQuery:
type: string
readOnly: true
description: The query to append to GET subjects.
subjects:
description: AbsolutePaths of the subjects.
Expand All @@ -1978,7 +1977,8 @@ paths:
description: >-
Variables accessible inside the transform function.
required:
- name, subjectQuery, subjects, aspects
- name
- aspects
responses:
201:
description: >-
Expand All @@ -1995,6 +1995,7 @@ paths:
# ---------------------------------------------------------------------------

/generators/{key}:
x-swagger-router-controller: generators
get:
security:
- jwt: []
Expand Down Expand Up @@ -2175,7 +2176,8 @@ paths:
description: >-
Variables accessible inside the transform function.
required:
- name, subjectQuery, subjects, aspects
- name
- aspects
responses:
200:
description: Success, returns the updated generator.
Expand All @@ -2193,6 +2195,7 @@ paths:
# ---------------------------------------------------------------------------

/generators/{key}/writers:
x-swagger-router-controller: generators
get:
security:
- jwt: []
Expand Down Expand Up @@ -2276,6 +2279,7 @@ paths:
# ---------------------------------------------------------------------------

/generators/{key}/writers/{userNameOrId}:
x-swagger-router-controller: generators
get:
security:
- jwt: []
Expand Down Expand Up @@ -2307,7 +2311,11 @@ paths:
description: >-
Success, returns requested user.
schema:
$ref: "#/definitions/UsersResponse"
type: array
items:
$ref: "#/definitions/UsersResponse"
description: >-
The users having write permission to the aspect
400:
$ref: "#/responses/400"
404:
Expand Down Expand Up @@ -9729,10 +9737,6 @@ definitions:
readOnly: true
description: >
The collector id.
isDeleted:
type: string
readOnly: true
description: 0 if this generator is NOT deleted. Timestamp of the deletion otherwise.
isActive:
type: boolean
description: Whether or not the generator is running on a collector.
Expand Down
6 changes: 6 additions & 0 deletions db/model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ module.exports = function user(seq, dataTypes) {
through: 'GeneratorTemplateWriters',
foreignKey: 'userId',
});
assoc.writableGenerators =
User.belongsToMany(models.Generator, {
as: 'writableGenerators',
through: 'GeneratorWriters',
foreignKey: 'userId',
});
assoc.writableAspects = User.belongsToMany(models.Aspect, {
as: 'writableAspects',
through: 'AspectWriters',
Expand Down

0 comments on commit 40dd8ae

Please sign in to comment.