Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 5ac78a5

Browse files
committed
- Added lookupModel property in most controller calls to allow using a ManyToMany model.
- Added tables creation and migration scripts. - Fixed swaggerTestHelper to allow definitions without a required setting.
1 parent 6f09448 commit 5ac78a5

17 files changed

+280
-65
lines changed

api/controllers/challenges.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
var datasource = require('./../../datasource').getDataSource();
1414
var Challenge = datasource.Challenge;
15+
var ChallengeFile = datasource.ChallengeFile;
1516
var File = datasource.File;
1617
var Participant = datasource.Participant;
1718
var Submission = datasource.Submission;
@@ -20,21 +21,21 @@ var controllerHelper = require('./../../lib/controllerHelper');
2021

2122

2223
// build controller for challenge resource
23-
var challengeController = controllerHelper.buildController(Challenge, null, {filtering: true});
24+
var challengeController = controllerHelper.buildController(Challenge, null, null, {filtering: true});
2425

2526

2627
var filteringOff = {
2728
filtering: false
2829
};
2930

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

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

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

3940

4041

api/controllers/files.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212

1313
var datasource = require('./../../datasource').getDataSource();
14-
var Challenge = datasource.Challenge;
1514
var Submission = datasource.Submission;
15+
var SubmissionFile = datasource.SubmissionFile;
1616
var File = datasource.File;
1717
var controllerHelper = require('./../../lib/controllerHelper');
1818

1919

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

2423
module.exports = {
2524
getFiles: fileController.all,

api/controllers/requirements.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var controllerHelper = require('./../../lib/controllerHelper');
1717

1818

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

2222

2323
module.exports = {

api/controllers/scorecardItems.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var options = {
2424

2525

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

2929

3030
module.exports = {

api/controllers/scorecards.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var controllerHelper = require('./../../lib/controllerHelper');
1717

1818

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

2222

2323
module.exports = {

api/models/challenge.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = function(sequelize, DataTypes) {
5555
}, {
5656
tableName : 'challenges',
5757
associate : function(models) {
58-
Challenge.hasMany(models.File);
58+
Challenge.hasMany(models.ChallengeFile);
5959
Challenge.hasMany(models.Participant);
6060
Challenge.hasMany(models.Submission);
6161
Challenge.hasMany(models.Scorecard);

api/models/challengeFile.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2014 TopCoder, Inc. All rights reserved.
3+
*/
4+
/**
5+
* Represent File in the system.
6+
*
7+
* @author gfzabarino
8+
*/
9+
'use strict';
10+
11+
/**
12+
* Defining ChallengeFile model
13+
*/
14+
module.exports = function(sequelize, DataTypes) {
15+
var ChallengeFile = sequelize.define('ChallengeFile', {
16+
challengeId: {
17+
type: DataTypes.BIGINT, primaryKey: true,
18+
get: function() {
19+
return parseInt(this.getDataValue('challengeId'));
20+
}
21+
},
22+
fileId: {
23+
type: DataTypes.BIGINT, primaryKey: true,
24+
get: function() {
25+
return parseInt(this.getDataValue('fileId'));
26+
}
27+
},
28+
29+
}, {
30+
tableName : 'challenge_files',
31+
associate : function(models) {
32+
ChallengeFile.belongsTo(models.Challenge, {foreignKey : 'challengeId'});
33+
ChallengeFile.belongsTo(models.File, {foreignKey : 'fileId'});
34+
}
35+
});
36+
37+
return ChallengeFile;
38+
39+
};

api/models/file.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ module.exports = function(sequelize, DataTypes) {
2121
return parseInt(this.getDataValue('id'));
2222
}
2323
},
24-
challengeId: {
25-
type: DataTypes.BIGINT, allowNull: false,
26-
get: function() {
27-
return parseInt(this.getDataValue('challengeId'));
28-
}
29-
},
30-
submissionId: {
31-
type: DataTypes.BIGINT, allowNull: false,
32-
get: function() {
33-
return parseInt(this.getDataValue('submissionId'));
34-
}
35-
},
3624
title : {type: DataTypes.TEXT},
3725
filePath : {type: DataTypes.TEXT, allowNull: false},
3826
size : {
@@ -54,9 +42,8 @@ module.exports = function(sequelize, DataTypes) {
5442
}, {
5543
tableName : 'files',
5644
associate : function(models) {
57-
File.belongsTo(models.Challenge, {foreignKey : 'challengeId'});
58-
File.belongsTo(models.Submission, {foreignKey : 'submissionId'});
59-
45+
File.hasMany(models.ChallengeFile);
46+
File.hasMany(models.SubmissionFile);
6047
}
6148
});
6249

api/models/submission.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = function(sequelize, DataTypes) {
3939
}, {
4040
tableName : 'submissions',
4141
associate : function(models) {
42-
Submission.hasMany(models.File);
42+
Submission.hasMany(models.SubmissionFile);
4343
Submission.hasMany(models.Scorecard);
4444
Submission.belongsTo(models.User, {foreignKey : 'submitterId'});
4545
Submission.belongsTo(models.Challenge, {foreignKey : 'challengeId'});

api/models/submissionFile.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) 2014 TopCoder, Inc. All rights reserved.
3+
*/
4+
/**
5+
* Represent File in the system.
6+
*
7+
* @author gfzabarino
8+
*/
9+
'use strict';
10+
11+
/**
12+
* Defining ChallengeFile model
13+
*/
14+
module.exports = function(sequelize, DataTypes) {
15+
var SubmissionFile = sequelize.define('SubmissionFile', {
16+
submissionId: {
17+
type: DataTypes.BIGINT, primaryKey: true,
18+
get: function() {
19+
return parseInt(this.getDataValue('submissionId'));
20+
}
21+
},
22+
fileId: {
23+
type: DataTypes.BIGINT, primaryKey: true,
24+
get: function() {
25+
return parseInt(this.getDataValue('fileId'));
26+
}
27+
}
28+
}, {
29+
tableName : 'submission_files',
30+
associate : function(models) {
31+
SubmissionFile.belongsTo(models.Submission, {foreignKey : 'submissionId'});
32+
SubmissionFile.belongsTo(models.File, {foreignKey : 'fileId'});
33+
}
34+
});
35+
36+
return SubmissionFile;
37+
38+
};

config/schema-migrations/20141010160840-tables.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ exports.up = function (db, callback) {
4040
'"createdAt" timestamp with time zone NOT NULL, ' +
4141
'"updatedAt" timestamp with time zone NOT NULL, ' +
4242
'"createdBy" character varying(128), ' +
43-
'"updatedBy" character varying(128), ' +
44-
'"submissionId" bigint, ' +
45-
'"challengeId" bigint NOT NULL ' +
43+
'"updatedBy" character varying(128) ' +
4644
');'),
4745
db.runSql.bind(db, 'ALTER TABLE ONLY files ADD CONSTRAINT files_pkey PRIMARY KEY (id);'),
4846

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var dbm = require('db-migrate');
2+
var type = dbm.dataType;
3+
var async = require('async');
4+
5+
exports.up = function(db, callback) {
6+
7+
async.series([
8+
function (callback) {
9+
// challenge files table
10+
db.createTable('challenge_files', {
11+
columns: {
12+
'"fileId"': {type: 'int', primaryKey: true},
13+
'"challengeId"': {type: 'int', primaryKey: true}
14+
},
15+
ifNotExists: true
16+
}, callback);
17+
}, function (callback) {
18+
// submission files table
19+
db.createTable('submission_files', {
20+
columns: {
21+
'"fileId"': {type: 'int', primaryKey: true},
22+
'"submissionId"': {type: 'int', primaryKey: true}
23+
},
24+
ifNotExists: true
25+
}, callback);
26+
}, function (callback) {
27+
db.runSql('ALTER TABLE challenge_files ADD CONSTRAINT challenge_fk FOREIGN KEY ("challengeId") REFERENCES challenges (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
28+
}, function (callback) {
29+
db.runSql('ALTER TABLE submission_files ADD CONSTRAINT submission_fk FOREIGN KEY ("submissionId") REFERENCES submissions (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
30+
}, function (callback) {
31+
db.runSql('ALTER TABLE challenge_files ADD CONSTRAINT file_fk FOREIGN KEY ("fileId") REFERENCES files (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
32+
}, function (callback) {
33+
db.runSql('ALTER TABLE submission_files ADD CONSTRAINT file_fk FOREIGN KEY ("fileId") REFERENCES files (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
34+
}, function (callback) {
35+
db.runSql('ALTER TABLE challenge_files ADD COLUMN "createdAt" timestamp with time zone NOT NULL;', callback);
36+
}, function (callback) {
37+
db.runSql('ALTER TABLE challenge_files ADD COLUMN "updatedAt" timestamp with time zone NOT NULL;', callback);
38+
}, function (callback) {
39+
db.runSql('ALTER TABLE submission_files ADD COLUMN "createdAt" timestamp with time zone NOT NULL;', callback);
40+
}, function (callback) {
41+
db.runSql('ALTER TABLE submission_files ADD COLUMN "updatedAt" timestamp with time zone NOT NULL;', callback);
42+
}
43+
], callback);
44+
};
45+
46+
exports.down = function(db, callback) {
47+
async.series([
48+
function (callback) {
49+
db.dropTable('challenge_files', callback);
50+
}, function (callback) {
51+
db.dropTable('submission_files', callback);
52+
}
53+
], callback);
54+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var dbm = require('db-migrate');
2+
var async = require('async');
3+
var type = dbm.dataType;
4+
5+
exports.up = function(db, callback) {
6+
console.info('migrate!!!');
7+
db.all('SELECT "column_name" ' +
8+
'FROM "information_schema"."columns" ' +
9+
'WHERE "table_name"=\'files\' and "column_name"=\'challengeId\';', function (err, results) {
10+
if (err) {
11+
return callback(err);
12+
}
13+
if (results && results.length) {
14+
async.series([
15+
function (callback) {
16+
db.runSql('INSERT INTO challenge_files ("fileId", "challengeId", "createdAt", "updatedAt") ' +
17+
'SELECT id, "challengeId", "createdAt", "updatedAt" ' +
18+
'FROM files WHERE "challengeId" IS NOT NULL', callback);
19+
}, function (callback) {
20+
db.runSql('INSERT INTO submission_files ("fileId", "submissionId", "createdAt", "updatedAt") ' +
21+
'SELECT id, "submissionId", "createdAt", "updatedAt" ' +
22+
'FROM files WHERE "submissionId" IS NOT NULL', callback);
23+
}, function (callback) {
24+
db.runSql('ALTER TABLE files DROP COLUMN "submissionId"', callback);
25+
}, function (callback) {
26+
db.runSql('ALTER TABLE files DROP COLUMN "challengeId"', callback);
27+
}
28+
], callback);
29+
} else {
30+
callback();
31+
}
32+
});
33+
};
34+
35+
exports.down = function(db, callback) {
36+
callback();
37+
};

0 commit comments

Comments
 (0)