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

Commit 4e854fd

Browse files
committed
Merge pull request #386 from isvisv/dev
TC API - Modify Answer API And Delete Answer API
2 parents 156a5da + 1f873b3 commit 4e854fd

13 files changed

+823
-11
lines changed

actions/srmRoundQuestions.js

Lines changed: 170 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*
22
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
33
*/
4-
/**
4+
/**
55
* - Implement the srm round questions / answers / survey api.
66
* Changes in version 1.1 (Module Assembly - Web Arena - Match Configurations):
77
* - Updated getRoundQuestions to send roundId with response
8+
* Changes in 1.2:
9+
* - Added actions for modifying/deleting round question answers.
810
*
9-
* @version 1.1
10-
* @author TCSASSEMBLER
11+
* @version 1.2
12+
* @author isv
1113
*/
1214

1315
/*jslint node: true, nomen: true, plusplus: true, stupid: true, unparam: true */
@@ -16,6 +18,7 @@ var async = require('async');
1618
var _ = require('underscore');
1719
var moment = require('moment');
1820
var IllegalArgumentError = require('../errors/IllegalArgumentError');
21+
var NotFoundError = require('../errors/NotFoundError');
1922

2023
var DATE_FORMAT = "YYYY-MM-DD HH:mm";
2124

@@ -327,7 +330,8 @@ function checkAnswerValues(api, text, sortOrder, correct, callback) {
327330
error = helper.checkStringParameter(text, "text", 250);
328331

329332
if (!error && _.isDefined(sortOrder)) {
330-
error = helper.checkPositiveInteger(sortOrder, "sortOrder");
333+
error = helper.checkPositiveInteger(sortOrder, "sortOrder")
334+
|| helper.checkMaxInt(sortOrder, "sortOrder");
331335
}
332336

333337
if (!error && _.isDefined(correct)) {
@@ -609,8 +613,8 @@ var modifyRoundQuestion = function (api, connection, dbConnectionMap, next) {
609613
*/
610614
var deleteRoundQuestion = function (api, connection, dbConnectionMap, next) {
611615
var helper = api.helper,
612-
sqlParams = {},
613-
questionId = Number(connection.params.questionId);
616+
sqlParams = {},
617+
questionId = Number(connection.params.questionId);
614618

615619
async.waterfall([
616620
function (cb) {
@@ -631,6 +635,115 @@ var deleteRoundQuestion = function (api, connection, dbConnectionMap, next) {
631635
});
632636
};
633637

638+
/**
639+
* Checks if answer with specified ID exists in database.
640+
*
641+
* @param api the api instance.
642+
* @param dbConnectionMap the database connection map.
643+
* @param answerId - the answerId parameter.
644+
* @param callback the callback method.
645+
*/
646+
function checkAnswerId(api, dbConnectionMap, answerId, callback) {
647+
var helper = api.helper,
648+
error = helper.checkIdParameter(answerId, "answerId");
649+
650+
async.waterfall([
651+
function (cb) {
652+
if (!error) {
653+
api.dataAccess.executeQuery("get_answer_id", {answerId: answerId}, dbConnectionMap, cb);
654+
} else {
655+
cb(null, null);
656+
}
657+
}, function (results, cb) {
658+
if (!error) {
659+
if (results.length === 0) {
660+
error = new NotFoundError("The answerId does not exist in database.");
661+
}
662+
}
663+
cb(error);
664+
}
665+
], function (err) {
666+
if (err) {
667+
callback(err);
668+
return;
669+
}
670+
671+
callback(null, error);
672+
});
673+
}
674+
675+
/**
676+
* Modify Round Question Answer.
677+
*
678+
* @param api the api instance.
679+
* @param connection the connection instance.
680+
* @param dbConnectionMap the database connection map.
681+
* @param next the callback method.
682+
*/
683+
var modifyRoundQuestionAnswer = function (api, connection, dbConnectionMap, next) {
684+
var helper = api.helper,
685+
sqlParams = {},
686+
answerId = Number(connection.params.answerId),
687+
text = connection.params.text,
688+
sortOrder = connection.params.sortOrder,
689+
correct = connection.params.correct;
690+
691+
async.waterfall([
692+
function (cb) {
693+
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
694+
}, function (cb) {
695+
checkAnswerValues(api, text, sortOrder, correct, cb);
696+
}, function (error, cb) {
697+
checkAnswerId(api, dbConnectionMap, answerId, cb);
698+
}, function (error, cb) {
699+
sqlParams.answerId = answerId;
700+
sqlParams.answerText = text;
701+
sqlParams.sortOrder = sortOrder;
702+
sqlParams.correct = (correct === true || correct.toLowerCase() === "true") ? 1 : 0;
703+
api.dataAccess.executeQuery("update_answer", sqlParams, dbConnectionMap, cb);
704+
}
705+
], function (err) {
706+
if (err) {
707+
helper.handleError(api, connection, err);
708+
} else {
709+
connection.response = {"success": true};
710+
}
711+
next(connection, true);
712+
});
713+
};
714+
715+
/**
716+
* Delete Round Question Answer.
717+
*
718+
* @param api the api instance.
719+
* @param connection the connection instance.
720+
* @param dbConnectionMap the database connection map.
721+
* @param next the callback method.
722+
*/
723+
var deleteRoundQuestionAnswer = function (api, connection, dbConnectionMap, next) {
724+
var helper = api.helper,
725+
sqlParams = {},
726+
answerId = Number(connection.params.answerId);
727+
728+
async.waterfall([
729+
function (cb) {
730+
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
731+
}, function (cb) {
732+
cb(helper.checkIdParameter(answerId, 'answerId'));
733+
}, function (cb) {
734+
sqlParams.answerId = answerId;
735+
api.dataAccess.executeQuery("delete_answer", sqlParams, dbConnectionMap, cb);
736+
}
737+
], function (err, result) {
738+
if (err) {
739+
helper.handleError(api, connection, err);
740+
} else {
741+
connection.response = {"success": result > 0};
742+
}
743+
next(connection, true);
744+
});
745+
};
746+
634747
/**
635748
* The API for get Round Questions API.
636749
*/
@@ -802,3 +915,54 @@ exports.deleteRoundQuestion = {
802915
}
803916
}
804917
};
918+
919+
920+
/**
921+
* The API for Modify Round Question Answer API.
922+
*/
923+
exports.modifyRoundQuestionAnswer = {
924+
name: "modifyRoundQuestionAnswer",
925+
description: "Modify Round Question Answer",
926+
inputs: {
927+
required: ['answerId', 'text', 'sortOrder', 'correct'],
928+
optional: []
929+
},
930+
blockedConnectionTypes: [],
931+
outputExample: {},
932+
version: 'v2',
933+
transaction: 'write',
934+
databases: ["informixoltp"],
935+
run: function (api, connection, next) {
936+
if (connection.dbConnectionMap) {
937+
api.log("Execute modifyRoundQuestionAnswer#run", 'debug');
938+
modifyRoundQuestionAnswer(api, connection, connection.dbConnectionMap, next);
939+
} else {
940+
api.helper.handleNoConnection(api, connection, next);
941+
}
942+
}
943+
};
944+
945+
/**
946+
* The API for Delete Round Question Answer API.
947+
*/
948+
exports.deleteRoundQuestionAnswer = {
949+
name: "deleteRoundQuestionAnswer",
950+
description: "Delete Round Question Answer",
951+
inputs: {
952+
required: ['answerId'],
953+
optional: []
954+
},
955+
blockedConnectionTypes: [],
956+
outputExample: {},
957+
version: 'v2',
958+
transaction: 'write',
959+
databases: ["informixoltp"],
960+
run: function (api, connection, next) {
961+
if (connection.dbConnectionMap) {
962+
api.log("Execute deleteRoundQuestionAnswer#run", 'debug');
963+
deleteRoundQuestionAnswer(api, connection, connection.dbConnectionMap, next);
964+
} else {
965+
api.helper.handleNoConnection(api, connection, next);
966+
}
967+
}
968+
};

0 commit comments

Comments
 (0)