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

Commit 7838f14

Browse files
committed
- Removed File model and added ChallengeFile and SubmissionFile models.
- Swagger: FileResource still exists and acts as entity of both ChallengeFile and SubmissionFile. - Added tables creation and migration scripts. - Changed getRefIdField (routeHelper.js) method to return 'fileId' for any mdel name that contains 'File' on its name. - Fixed swaggerTestHelper to allow definitions without a required setting.
1 parent 6f09448 commit 7838f14

15 files changed

+236
-73
lines changed

Diff for: api/controllers/challenges.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
var datasource = require('./../../datasource').getDataSource();
1414
var Challenge = datasource.Challenge;
15-
var File = datasource.File;
15+
var ChallengeFile = datasource.ChallengeFile;
1616
var Participant = datasource.Participant;
1717
var Submission = datasource.Submission;
1818
var controllerHelper = require('./../../lib/controllerHelper');
@@ -27,8 +27,8 @@ var filteringOff = {
2727
filtering: false
2828
};
2929

30-
// build controller for the nested files resource
31-
var fileController = controllerHelper.buildController(File, [Challenge], filteringOff);
30+
// build controller for the nested challenge files resource
31+
var challengeFileController = controllerHelper.buildController(ChallengeFile, [Challenge], filteringOff);
3232

3333
// build controller for the nested participants resource
3434
var participantController = controllerHelper.buildController(Participant, [Challenge], {filtering: true});
@@ -45,11 +45,11 @@ module.exports = {
4545
update: challengeController.update,
4646
delete: challengeController.delete,
4747

48-
getChallengeFiles: fileController.all,
49-
addChallengeFile: fileController.create,
50-
getChallengeFile: fileController.get,
51-
updateChallengeFile: fileController.update,
52-
deleteChallengeFile: fileController.delete,
48+
getChallengeFiles: challengeFileController.all,
49+
addChallengeFile: challengeFileController.create,
50+
getChallengeFile: challengeFileController.get,
51+
updateChallengeFile: challengeFileController.update,
52+
deleteChallengeFile: challengeFileController.delete,
5353

5454
getAllParticipants: participantController.all,
5555
addParticipant: participantController.create,

Diff for: api/controllers/files.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111

1212

1313
var datasource = require('./../../datasource').getDataSource();
14-
var Challenge = datasource.Challenge;
1514
var Submission = datasource.Submission;
16-
var File = datasource.File;
15+
var SubmissionFile = datasource.SubmissionFile;
1716
var controllerHelper = require('./../../lib/controllerHelper');
1817

1918

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

2322

2423
module.exports = {
25-
getFiles: fileController.all,
26-
addFile: fileController.create,
27-
getFile: fileController.get,
28-
updateFile: fileController.update,
29-
deleteFile: fileController.delete
24+
getFiles: submissionFileController.all,
25+
addFile: submissionFileController.create,
26+
getFile: submissionFileController.get,
27+
updateFile: submissionFileController.update,
28+
deleteFile: submissionFileController.delete
3029
};

Diff for: 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);

Diff for: api/models/challengeFile.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// primary key
17+
id: {
18+
type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true,
19+
get: function() {
20+
return parseInt(this.getDataValue('id'));
21+
}
22+
},
23+
challengeId: {
24+
type: DataTypes.BIGINT, allowNull: false,
25+
get: function() {
26+
return parseInt(this.getDataValue('challengeId'));
27+
}
28+
},
29+
title : {type: DataTypes.TEXT},
30+
filePath : {type: DataTypes.TEXT, allowNull: false},
31+
size : {
32+
type: DataTypes.BIGINT, allowNull: false,
33+
get: function() {
34+
return parseInt(this.getDataValue('size'));
35+
}
36+
},
37+
fileName : {type: DataTypes.TEXT, allowNull: false},
38+
// file storage location
39+
storageLocation : {
40+
type: DataTypes.ENUM,
41+
values: ['local', 's3'],
42+
allowNull: false
43+
},
44+
createdBy: DataTypes.STRING(128),
45+
updatedBy: DataTypes.STRING(128)
46+
47+
}, {
48+
tableName : 'challenge_files',
49+
associate : function(models) {
50+
ChallengeFile.belongsTo(models.Challenge, {foreignKey : 'challengeId'});
51+
}
52+
});
53+
54+
return ChallengeFile;
55+
56+
};

Diff for: 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'});

Diff for: api/models/file.js renamed to api/models/submissionFile.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@
44
/**
55
* Represent File in the system.
66
*
7-
* @version 1.0
8-
* @author peakpado
7+
* @author gfzabarino
98
*/
109
'use strict';
1110

1211
/**
13-
* Defining File model
14-
*/
12+
* Defining SubmissionFile model
13+
*/
1514
module.exports = function(sequelize, DataTypes) {
16-
var File = sequelize.define('File', {
15+
var SubmissionFile = sequelize.define('SubmissionFile', {
1716
// primary key
1817
id: {
1918
type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true,
2019
get: function() {
2120
return parseInt(this.getDataValue('id'));
2221
}
2322
},
24-
challengeId: {
25-
type: DataTypes.BIGINT, allowNull: false,
26-
get: function() {
27-
return parseInt(this.getDataValue('challengeId'));
28-
}
29-
},
3023
submissionId: {
3124
type: DataTypes.BIGINT, allowNull: false,
3225
get: function() {
@@ -52,14 +45,12 @@ module.exports = function(sequelize, DataTypes) {
5245
updatedBy: DataTypes.STRING(128)
5346

5447
}, {
55-
tableName : 'files',
48+
tableName : 'submission_files',
5649
associate : function(models) {
57-
File.belongsTo(models.Challenge, {foreignKey : 'challengeId'});
58-
File.belongsTo(models.Submission, {foreignKey : 'submissionId'});
59-
50+
SubmissionFile.belongsTo(models.Submission, {foreignKey : 'submissionId'});
6051
}
6152
});
6253

63-
return File;
54+
return SubmissionFile;
6455

6556
};

Diff for: api/swagger/swagger.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,14 @@ definitions:
14961496
updatedBy:
14971497
type: string
14981498
description: The handle of the user last updated the row
1499+
challengeId:
1500+
type: integer
1501+
format: int64
1502+
description: The Challenge id for the file
1503+
submissionId:
1504+
type: integer
1505+
format: int64
1506+
description: The Submission id for the file
14991507
required:
15001508
- fileName
15011509
- filePath

Diff for: config/schema-migrations/20141010160840-tables.js

-19
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,6 @@ exports.up = function (db, callback) {
2828
');'),
2929
db.runSql.bind(db, 'ALTER TABLE ONLY challenges ADD CONSTRAINT challenges_pkey PRIMARY KEY (id);'),
3030

31-
// files table
32-
db.runSql.bind(db,
33-
'CREATE TABLE files ( ' +
34-
'id bigserial NOT NULL, ' +
35-
'title text, ' +
36-
'"filePath" text NOT NULL, ' +
37-
'size bigint NOT NULL, ' +
38-
'"fileName" text NOT NULL, ' +
39-
'"storageLocation" "enum_files_storageLocation" NOT NULL, ' +
40-
'"createdAt" timestamp with time zone NOT NULL, ' +
41-
'"updatedAt" timestamp with time zone NOT NULL, ' +
42-
'"createdBy" character varying(128), ' +
43-
'"updatedBy" character varying(128), ' +
44-
'"submissionId" bigint, ' +
45-
'"challengeId" bigint NOT NULL ' +
46-
');'),
47-
db.runSql.bind(db, 'ALTER TABLE ONLY files ADD CONSTRAINT files_pkey PRIMARY KEY (id);'),
48-
4931
// participants table
5032
db.runSql.bind(db,
5133
'CREATE TABLE participants ( ' +
@@ -143,7 +125,6 @@ exports.down = function (db, callback) {
143125
db.dropTable.bind(db, 'scorecards'),
144126
db.dropTable.bind(db, 'submissions'),
145127
db.dropTable.bind(db, 'participants'),
146-
db.dropTable.bind(db, 'files'),
147128
db.dropTable.bind(db, 'challenges')
148129
], callback);
149130
};
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
id: {type: 'int', primaryKey: true, autoIncrement: true},
13+
challengeId: {type: 'bigint', notNull: true},
14+
title: {type: 'text'},
15+
filePath: {type: 'text', notNull: true},
16+
size: {type: 'bigint', notNull: true},
17+
fileName: {type: 'text', notNull: true},
18+
createdAt: {type: 'timestamp', notNull: true},
19+
updatedAt: {type: 'timestamp', notNull: true},
20+
createdBy: {type: 'string'},
21+
updatedBy: {type: 'string'}
22+
},
23+
ifNotExists: true
24+
}, callback);
25+
}, function (callback) {
26+
// submission files table
27+
db.createTable('submission_files', {
28+
columns: {
29+
id: {type: 'int', primaryKey: true, autoIncrement: true},
30+
submissionId: {type: 'bigint', notNull: true},
31+
title: {type: 'text'},
32+
filePath: {type: 'text', notNull: true},
33+
size: {type: 'bigint', notNull: true},
34+
fileName: {type: 'text', notNull: true},
35+
createdAt: {type: 'timestamp', notNull: true},
36+
updatedAt: {type: 'timestamp', notNull: true},
37+
createdBy: {type: 'string'},
38+
updatedBy: {type: 'string'}
39+
},
40+
ifNotExists: true
41+
}, callback);
42+
}, function (callback) {
43+
db.runSql('ALTER TABLE challenge_files ADD CONSTRAINT challenge_fk FOREIGN KEY ("challengeId") REFERENCES challenges (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
44+
}, function (callback) {
45+
db.runSql('ALTER TABLE challenge_files ADD COLUMN "storageLocation" "enum_files_storageLocation" NOT NULL;', callback);
46+
}, function (callback) {
47+
db.runSql('ALTER TABLE submission_files ADD CONSTRAINT submission_fk FOREIGN KEY ("submissionId") REFERENCES submissions (id) ON UPDATE CASCADE ON DELETE CASCADE;', callback);
48+
}, function (callback) {
49+
db.runSql('ALTER TABLE submission_files ADD COLUMN "storageLocation" "enum_files_storageLocation" NOT NULL;', callback);
50+
}
51+
], callback);
52+
};
53+
54+
exports.down = function(db, callback) {
55+
async.series([
56+
function (callback) {
57+
db.dropTable('challenge_files', callback);
58+
}, function (callback) {
59+
db.dropTable('submission_files', callback);
60+
}
61+
], callback);
62+
};
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var dbm = require('db-migrate');
2+
var async = require('async');
3+
var type = dbm.dataType;
4+
5+
exports.up = function(db, callback) {
6+
db.all('SELECT EXISTS(' +
7+
'SELECT * ' +
8+
'FROM information_schema.tables ' +
9+
'WHERE ' +
10+
'table_name = \'files\')', function (err, results) {
11+
if (err) {
12+
return callback(err);
13+
}
14+
if (results[0].exists) {
15+
async.series([
16+
function (callback) {
17+
db.runSql('INSERT INTO challenge_files (id, "challengeId", title, "filePath", size, "fileName", "storageLocation", "createdAt", "updatedAt", "createdBy", "updatedBy") ' +
18+
'SELECT id, "challengeId", title, "filePath", size, "fileName", "storageLocation", "createdAt", "updatedAt", "createdBy", "updatedBy" ' +
19+
'FROM files WHERE "challengeId" IS NOT NULL', callback);
20+
}, function (callback) {
21+
db.runSql('INSERT INTO submission_files (id, "submissionId", title, "filePath", size, "fileName", "storageLocation", "createdAt", "updatedAt", "createdBy", "updatedBy") ' +
22+
'SELECT id, "submissionId", title, "filePath", size, "fileName", "storageLocation", "createdAt", "updatedAt", "createdBy", "updatedBy" ' +
23+
'FROM files WHERE "submissionId" IS NOT NULL', callback);
24+
}, function (callback) {
25+
db.dropTable('files', callback);
26+
}
27+
], callback);
28+
} else {
29+
callback();
30+
}
31+
});
32+
};
33+
34+
exports.down = function(db, callback) {
35+
callback();
36+
};

Diff for: lib/routeHelper.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ exports.getSigninUser = function(req) {
8585
*/
8686
exports.getRefIdField = function(refModel) {
8787
var name = refModel.name;
88-
return name.charAt(0).toLowerCase() + name.slice(1) + 'Id';
88+
if (name.indexOf('File') === -1) {
89+
return name.charAt(0).toLowerCase() + name.slice(1) + 'Id';
90+
} else {
91+
return 'fileId';
92+
}
8993
};
9094

9195

Diff for: test/controllers/challenges.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var db = datasource.getDataSource();
2727
var sequelize = db.sequelize;
2828
sequelize.options.logging = false; // turn of sequelize logging.
2929
var Challenge = db.Challenge;
30-
var File = db.File;
30+
var ChallengeFile = db.ChallengeFile;
3131
var Participant = db.Participant;
3232
var Submission = db.Submission;
3333

@@ -527,6 +527,7 @@ describe('Challenges Controller', function() {
527527
var fileId;
528528
beforeEach(function(done) {
529529
reqData = _.clone(sampleData.challengeFileData, true);
530+
reqData.challengeId = challenge.id;
530531
done();
531532
});
532533

@@ -849,7 +850,7 @@ describe('Challenges Controller', function() {
849850

850851
after(function(done) {
851852
// delete data created during test.
852-
async.eachSeries([Submission, Participant, File, Challenge], function(model, callback) {
853+
async.eachSeries([Submission, Participant, ChallengeFile, Challenge], function(model, callback) {
853854
model.findAll().success(function(entities) {
854855
async.each(entities, function(entity, cb) {
855856
entity.destroy().success(function() {

0 commit comments

Comments
 (0)