Skip to content

Commit

Permalink
- Added lookupModel property in most controller calls to allow using …
Browse files Browse the repository at this point in the history
…a ManyToMany model.

- Added tables creation and migration scripts.
- Changed getRefIdField (routeHelper.js) method to return 'fileId' for any model name that contains 'File' on its name.
- Fixed swaggerTestHelper to allow definitions without a required setting.
  • Loading branch information
gfzabarino committed Nov 7, 2014
1 parent 6f09448 commit 3c73d6e
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 65 deletions.
9 changes: 5 additions & 4 deletions api/controllers/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

var datasource = require('./../../datasource').getDataSource();
var Challenge = datasource.Challenge;
var ChallengeFile = datasource.ChallengeFile;
var File = datasource.File;
var Participant = datasource.Participant;
var Submission = datasource.Submission;
Expand All @@ -20,21 +21,21 @@ var controllerHelper = require('./../../lib/controllerHelper');


// build controller for challenge resource
var challengeController = controllerHelper.buildController(Challenge, null, {filtering: true});
var challengeController = controllerHelper.buildController(Challenge, null, null, {filtering: true});


var filteringOff = {
filtering: false
};

// build controller for the nested files resource
var fileController = controllerHelper.buildController(File, [Challenge], filteringOff);
var fileController = controllerHelper.buildController(File, [Challenge], ChallengeFile, filteringOff);

// build controller for the nested participants resource
var participantController = controllerHelper.buildController(Participant, [Challenge], {filtering: true});
var participantController = controllerHelper.buildController(Participant, [Challenge], null, {filtering: true});

// build controller for the nested submissions resource
var submissionController = controllerHelper.buildController(Submission, [Challenge], filteringOff);
var submissionController = controllerHelper.buildController(Submission, [Challenge], null, filteringOff);



Expand Down
15 changes: 13 additions & 2 deletions api/controllers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@


var datasource = require('./../../datasource').getDataSource();
var Challenge = datasource.Challenge;
//var Challenge = datasource.Challenge;
//var ChallengeFile = datasource.ChallengeFile;
var Submission = datasource.Submission;
var SubmissionFile = datasource.SubmissionFile;
var File = datasource.File;
var controllerHelper = require('./../../lib/controllerHelper');


// build controller for the nested files resource
var fileController = controllerHelper.buildController(File, [Challenge, Submission], {filtering: false});
var fileController = controllerHelper.buildController(File, [Submission], SubmissionFile, {filtering: false});

//// build controller for the nested files resource
//var fileController = controllerHelper.buildController(File, [{
// manyToManyModel: ChallengeFile,
// referenceModel: Challenge
//}, {
// manyToManyModel: SubmissionFile,
// referenceModel: Submission
//}], {filtering: false});

//SELECT * FROM Files WHERE id IN (SELECT fileId FROM ChallengeFile WHERE challengeId = ?)
module.exports = {
getFiles: fileController.all,
addFile: fileController.create,
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/requirements.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var controllerHelper = require('./../../lib/controllerHelper');


// build controller for the nested files resource
var requirementController = controllerHelper.buildController(Requirement, [Challenge], {filtering: false});
var requirementController = controllerHelper.buildController(Requirement, [Challenge], null, {filtering: false});


module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/scorecardItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var options = {


// build controller for the nested scorecardItems resource
var scorecardItemController = controllerHelper.buildController(ScorecardItem, [Challenge, Scorecard], options);
var scorecardItemController = controllerHelper.buildController(ScorecardItem, [Challenge, Scorecard], null, options);


module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/scorecards.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var controllerHelper = require('./../../lib/controllerHelper');


// build controller for the nested scorecards resource
var scorecardController = controllerHelper.buildController(Scorecard, [Challenge], {filtering: false});
var scorecardController = controllerHelper.buildController(Scorecard, [Challenge], null, {filtering: false});


module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion api/models/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function(sequelize, DataTypes) {
}, {
tableName : 'challenges',
associate : function(models) {
Challenge.hasMany(models.File);
Challenge.hasMany(models.ChallengeFile);
Challenge.hasMany(models.Participant);
Challenge.hasMany(models.Submission);
Challenge.hasMany(models.Scorecard);
Expand Down
39 changes: 39 additions & 0 deletions api/models/challengeFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2014 TopCoder, Inc. All rights reserved.
*/
/**
* Represent File in the system.
*
* @author gfzabarino
*/
'use strict';

/**
* Defining ChallengeFile model
*/
module.exports = function(sequelize, DataTypes) {
var ChallengeFile = sequelize.define('ChallengeFile', {
challengeId: {
type: DataTypes.BIGINT, primaryKey: true,
get: function() {
return parseInt(this.getDataValue('challengeId'));
}
},
fileId: {
type: DataTypes.BIGINT, primaryKey: true,
get: function() {
return parseInt(this.getDataValue('fileId'));
}
},

}, {
tableName : 'challenge_files',
associate : function(models) {
ChallengeFile.belongsTo(models.Challenge, {foreignKey : 'challengeId'});
ChallengeFile.belongsTo(models.File, {foreignKey : 'fileId'});
}
});

return ChallengeFile;

};
17 changes: 2 additions & 15 deletions api/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ module.exports = function(sequelize, DataTypes) {
return parseInt(this.getDataValue('id'));
}
},
challengeId: {
type: DataTypes.BIGINT, allowNull: false,
get: function() {
return parseInt(this.getDataValue('challengeId'));
}
},
submissionId: {
type: DataTypes.BIGINT, allowNull: false,
get: function() {
return parseInt(this.getDataValue('submissionId'));
}
},
title : {type: DataTypes.TEXT},
filePath : {type: DataTypes.TEXT, allowNull: false},
size : {
Expand All @@ -54,9 +42,8 @@ module.exports = function(sequelize, DataTypes) {
}, {
tableName : 'files',
associate : function(models) {
File.belongsTo(models.Challenge, {foreignKey : 'challengeId'});
File.belongsTo(models.Submission, {foreignKey : 'submissionId'});

File.hasMany(models.ChallengeFile);
File.hasMany(models.SubmissionFile);
}
});

Expand Down
2 changes: 1 addition & 1 deletion api/models/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function(sequelize, DataTypes) {
}, {
tableName : 'submissions',
associate : function(models) {
Submission.hasMany(models.File);
Submission.hasMany(models.SubmissionFile);
Submission.hasMany(models.Scorecard);
Submission.belongsTo(models.User, {foreignKey : 'submitterId'});
Submission.belongsTo(models.Challenge, {foreignKey : 'challengeId'});
Expand Down
38 changes: 38 additions & 0 deletions api/models/submissionFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2014 TopCoder, Inc. All rights reserved.
*/
/**
* Represent File in the system.
*
* @author gfzabarino
*/
'use strict';

/**
* Defining ChallengeFile model
*/
module.exports = function(sequelize, DataTypes) {
var SubmissionFile = sequelize.define('SubmissionFile', {
submissionId: {
type: DataTypes.BIGINT, primaryKey: true,
get: function() {
return parseInt(this.getDataValue('submissionId'));
}
},
fileId: {
type: DataTypes.BIGINT, primaryKey: true,
get: function() {
return parseInt(this.getDataValue('fileId'));
}
}
}, {
tableName : 'submission_files',
associate : function(models) {
SubmissionFile.belongsTo(models.Submission, {foreignKey : 'submissionId'});
SubmissionFile.belongsTo(models.File, {foreignKey : 'fileId'});
}
});

return SubmissionFile;

};
4 changes: 1 addition & 3 deletions config/schema-migrations/20141010160840-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ exports.up = function (db, callback) {
'"createdAt" timestamp with time zone NOT NULL, ' +
'"updatedAt" timestamp with time zone NOT NULL, ' +
'"createdBy" character varying(128), ' +
'"updatedBy" character varying(128), ' +
'"submissionId" bigint, ' +
'"challengeId" bigint NOT NULL ' +
'"updatedBy" character varying(128) ' +
');'),
db.runSql.bind(db, 'ALTER TABLE ONLY files ADD CONSTRAINT files_pkey PRIMARY KEY (id);'),

Expand Down
54 changes: 54 additions & 0 deletions config/schema-migrations/20141106235712-files-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var dbm = require('db-migrate');
var type = dbm.dataType;
var async = require('async');

exports.up = function(db, callback) {

async.series([
function (callback) {
// challenge files table
db.createTable('challenge_files', {
columns: {
'"fileId"': {type: 'int', primaryKey: true},
'"challengeId"': {type: 'int', primaryKey: true}
},
ifNotExists: true
}, callback);
}, function (callback) {
// submission files table
db.createTable('submission_files', {
columns: {
'"fileId"': {type: 'int', primaryKey: true},
'"submissionId"': {type: 'int', primaryKey: true}
},
ifNotExists: true
}, callback);
}, function (callback) {
db.runSql('ALTER TABLE challenge_files ADD CONSTRAINT challenge_fk FOREIGN KEY ("challengeId") REFERENCES challenges (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
}, function (callback) {
db.runSql('ALTER TABLE submission_files ADD CONSTRAINT submission_fk FOREIGN KEY ("submissionId") REFERENCES submissions (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
}, function (callback) {
db.runSql('ALTER TABLE challenge_files ADD CONSTRAINT file_fk FOREIGN KEY ("fileId") REFERENCES files (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
}, function (callback) {
db.runSql('ALTER TABLE submission_files ADD CONSTRAINT file_fk FOREIGN KEY ("fileId") REFERENCES files (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
}, function (callback) {
db.runSql('ALTER TABLE challenge_files ADD COLUMN "createdAt" timestamp with time zone NOT NULL;', callback);
}, function (callback) {
db.runSql('ALTER TABLE challenge_files ADD COLUMN "updatedAt" timestamp with time zone NOT NULL;', callback);
}, function (callback) {
db.runSql('ALTER TABLE submission_files ADD COLUMN "createdAt" timestamp with time zone NOT NULL;', callback);
}, function (callback) {
db.runSql('ALTER TABLE submission_files ADD COLUMN "updatedAt" timestamp with time zone NOT NULL;', callback);
}
], callback);
};

exports.down = function(db, callback) {
async.series([
function (callback) {
db.dropTable('challenge_files', callback);
}, function (callback) {
db.dropTable('submission_files', callback);
}
], callback);
};
37 changes: 37 additions & 0 deletions config/schema-migrations/20141107002604-files-migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var dbm = require('db-migrate');
var async = require('async');
var type = dbm.dataType;

exports.up = function(db, callback) {
console.info('migrate!!!');
db.all('SELECT "column_name" ' +
'FROM "information_schema"."columns" ' +
'WHERE "table_name"=\'files\' and "column_name"=\'challengeId\';', function (err, results) {
if (err) {
return callback(err);
}
if (results && results.length) {
async.series([
function (callback) {
db.runSql('INSERT INTO challenge_files ("fileId", "challengeId", "createdAt", "updatedAt") ' +
'SELECT id, "challengeId", "createdAt", "updatedAt" ' +
'FROM files WHERE "challengeId" IS NOT NULL', callback);
}, function (callback) {
db.runSql('INSERT INTO submission_files ("fileId", "submissionId", "createdAt", "updatedAt") ' +
'SELECT id, "submissionId", "createdAt", "updatedAt" ' +
'FROM files WHERE "submissionId" IS NOT NULL', callback);
}, function (callback) {
db.runSql('ALTER TABLE files DROP COLUMN "submissionId"', callback);
}, function (callback) {
db.runSql('ALTER TABLE files DROP COLUMN "challengeId"', callback);
}
], callback);
} else {
callback();
}
});
};

exports.down = function(db, callback) {
callback();
};
Loading

0 comments on commit 3c73d6e

Please sign in to comment.