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

Commit 8631a5d

Browse files
author
fireice.topcoder
committed
Add Delete Question API
1 parent 2a661e4 commit 8631a5d

File tree

6 files changed

+299
-0
lines changed

6 files changed

+299
-0
lines changed

actions/srmRoundQuestions.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,38 @@ var modifyRoundQuestion = function (api, connection, dbConnectionMap, next) {
599599
});
600600
};
601601

602+
/**
603+
* Delete Round Question.
604+
*
605+
* @param api the api instance.
606+
* @param connection the connection instance
607+
* @param dbConnectionMap the database connection map
608+
* @param next the callback method
609+
*/
610+
var deleteRoundQuestion = function (api, connection, dbConnectionMap, next) {
611+
var helper = api.helper,
612+
sqlParams = {},
613+
questionId = Number(connection.params.questionId);
614+
615+
async.waterfall([
616+
function (cb) {
617+
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
618+
}, function (cb) {
619+
checkQuestionId(api, dbConnectionMap, questionId, cb);
620+
}, function (error, cb) {
621+
sqlParams.question_id = questionId;
622+
api.dataAccess.executeQuery("delete_round_question", sqlParams, dbConnectionMap, cb);
623+
}
624+
], function (err) {
625+
if (err) {
626+
helper.handleError(api, connection, err);
627+
} else {
628+
connection.response = {"success": true};
629+
}
630+
next(connection, true);
631+
});
632+
};
633+
602634
/**
603635
* The API for get Round Questions API.
604636
*/
@@ -748,3 +780,25 @@ exports.modifyRoundQuestion = {
748780
}
749781
}
750782
};
783+
784+
exports.deleteRoundQuestion = {
785+
name: "deleteRoundQuestion",
786+
description: "Delete Round Question",
787+
inputs: {
788+
required: ['questionId'],
789+
optional: []
790+
},
791+
blockedConnectionTypes: [],
792+
outputExample: {},
793+
version: 'v2',
794+
transaction: 'write',
795+
databases: ["informixoltp"],
796+
run: function (api, connection, next) {
797+
if (connection.dbConnectionMap) {
798+
api.log("Execute deleteRoundQuestion#run", 'debug');
799+
deleteRoundQuestion(api, connection, connection.dbConnectionMap, next);
800+
} else {
801+
api.helper.handleNoConnection(api, connection, next);
802+
}
803+
}
804+
};

apiary.apib

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12008,6 +12008,111 @@ Managing SRM Contests APIs
1200812008
}
1200912009
}
1201012010

12011+
## Delete Round Question API [/data/srm/rounds/:questionId/question]
12012+
12013+
### Delete Round Question API [DELETE]
12014+
12015+
+ Parameters
12016+
+ questionId (required, number, `1000001`) ... The id of the question to delete.
12017+
12018+
+ Request
12019+
12020+
+ Headers
12021+
12022+
Authorization : Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZHwxMzI0NTYiLCJleHAiOjEzOTI4MTc4ODQsImF1ZCI6InRvcGNvZGVyIiwiaWF0IjoxMzkyNzU3ODg0fQ.7X2IKkiyyI1ExSM5GNpdhJ8fGGK5-oAjzccX6YL_BKY
12023+
12024+
+ Response 200 (application/json)
12025+
12026+
{
12027+
"success": true
12028+
}
12029+
12030+
+ Response 400 (application/json)
12031+
12032+
{
12033+
"error": {
12034+
"name": "Bad Request",
12035+
"value": 400,
12036+
"description": "The request was invalid. An accompanying message will explain why.",
12037+
"details": "questionId should be number."
12038+
}
12039+
}
12040+
12041+
12042+
+ Response 400 (application/json)
12043+
12044+
{
12045+
"error": {
12046+
"name": "Bad Request",
12047+
"value": 400,
12048+
"description": "The request was invalid. An accompanying message will explain why.",
12049+
"details": "questionId should be Integer."
12050+
}
12051+
}
12052+
12053+
+ Response 400 (application/json)
12054+
12055+
{
12056+
"error": {
12057+
"name": "Bad Request",
12058+
"value": 400,
12059+
"description": "The request was invalid. An accompanying message will explain why.",
12060+
"details": "questionId should be positive."
12061+
}
12062+
}
12063+
12064+
+ Response 400 (application/json)
12065+
12066+
{
12067+
"error": {
12068+
"name": "Bad Request",
12069+
"value": 400,
12070+
"description": "The request was invalid. An accompanying message will explain why.",
12071+
"details": "questionId should be less or equal to 2147483647."
12072+
}
12073+
}
12074+
12075+
+ Response 401 (application/json)
12076+
12077+
{
12078+
"error": {
12079+
"name": "Unauthorized",
12080+
"value": 401,
12081+
"description": "The request is understood, but it has been refused or access is not allowed.",
12082+
"details": "Authorized information needed."
12083+
}
12084+
}
12085+
12086+
+ Response 403 (application/json)
12087+
12088+
{
12089+
"error": {
12090+
"name": "Forbidden",
12091+
"value": 403,
12092+
"description": "The request is understood, but it has been refused or access is not allowed.",
12093+
"details": "Admin access only."
12094+
}
12095+
}
12096+
12097+
+ Response 500 (application/json)
12098+
12099+
{
12100+
"error": {
12101+
"name":"Internal Server Error",
12102+
"value":"500",
12103+
"description":"Unknown server error. Please contact support."
12104+
}
12105+
}
12106+
12107+
+ Response 503 (application/json)
12108+
{
12109+
"error": {
12110+
"name":"Service Unavailable",
12111+
"value":"503",
12112+
"description":"Servers are up but overloaded. Try again later."
12113+
}
12114+
}
12115+
1201112116
## Get Round Question Answers API [/data/srm/rounds/:questionId/answers]
1201212117

1201312118
### Get Round Question Answers API [GET]

queries/delete_round_question

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DELETE FROM survey_question WHERE question_id=@question_id@;
2+
DELETE FROM round_question WHERE question_id=@question_id@;
3+
DELETE FROM answer WHERE question_id=@question_id@;
4+
DELETE FROM question WHERE question_id=@question_id@;

queries/delete_round_question.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name" : "delete_round_question",
3+
"db" : "informixoltp",
4+
"sqlfile" : "delete_round_question"
5+
}

routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ exports.routes = {
386386
{ path: "/:apiVersion/data/srm/rounds/:oldRoundId", action: "modifySRMContestRound" }
387387
],
388388
delete: [
389+
{ path: "/:apiVersion/data/srm/rounds/:questionId/question", action: "deleteRoundQuestion" },
389390
{ path: "/:apiVersion/data/srm/rounds/:roundId", action: "deleteSRMContestRound" }
390391
]
391392
};

test/test.srmRoundQuestions.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ function createPostRequest(queryString, user) {
7979
return req;
8080
}
8181

82+
/**
83+
* Create delete request and return it.
84+
*
85+
* @param queryString - the query string
86+
* @param user - the user handle
87+
* @returns {*} request
88+
*/
89+
function createDeleteRequest(queryString, user) {
90+
var req = request(API_ENDPOINT)
91+
.del(queryString)
92+
.set("Accept", "application/json")
93+
.expect("Content-Type", /json/);
94+
if (user) {
95+
req.set('Authorization', generateAuthHeader(user));
96+
}
97+
98+
return req;
99+
}
100+
82101
/**
83102
* Assert error request.
84103
*
@@ -127,6 +146,30 @@ function assertPostError(queryString, user, obj, statusCode, errorDetail, done)
127146
});
128147
}
129148

149+
/**
150+
* Assert delete response detail.
151+
*
152+
* @param queryString - the query string
153+
* @param user - the user handle
154+
* @param statusCode - the expected status code
155+
* @param errorDetail - the error detail.
156+
* @param done the callback function
157+
*/
158+
function assertDeleteError(queryString, user, statusCode, errorDetail, done) {
159+
createDeleteRequest(queryString, user).expect(statusCode).end(function (err, res) {
160+
if (err) {
161+
done(err);
162+
return;
163+
}
164+
if (statusCode === 200) {
165+
assert.equal(res.body.error, errorDetail, "Invalid error detail");
166+
} else {
167+
assert.equal(res.body.error.details, errorDetail, "Invalid error detail");
168+
}
169+
done();
170+
});
171+
}
172+
130173
/**
131174
* Create a long text.
132175
*
@@ -635,6 +678,34 @@ describe('SRM Round Questions APIs', function () {
635678
});
636679
});
637680

681+
describe('Delete Round Question API invalid test', function () {
682+
683+
it("No anonymous access.", function (done) {
684+
assertDeleteError("/v2/data/srm/rounds/306/question", null, 401, "Authorized information needed.", done);
685+
});
686+
687+
it("Admin access only.", function (done) {
688+
assertDeleteError("/v2/data/srm/rounds/306/question", 'user', 403, "Admin access only.", done);
689+
});
690+
691+
it("questionId should be number.", function (done) {
692+
assertDeleteError("/v2/data/srm/rounds/aaa/question", 'heffan', 400, "questionId should be number.", done);
693+
});
694+
695+
it("questionId should be Integer.", function (done) {
696+
assertDeleteError("/v2/data/srm/rounds/30.6/question", 'heffan', 400, "questionId should be Integer.", done);
697+
});
698+
699+
it("questionId should be positive.", function (done) {
700+
assertDeleteError("/v2/data/srm/rounds/-306/question", 'heffan', 400, "questionId should be positive.", done);
701+
});
702+
703+
it("questionId should be less or equal to 2147483647.", function (done) {
704+
assertDeleteError("/v2/data/srm/rounds/111111111111111111111111111/question", 'heffan', 400,
705+
"questionId should be less or equal to 2147483647.", done);
706+
});
707+
});
708+
638709
describe('Valid test', function () {
639710

640711
it("Valid set survey.", function (done) {
@@ -754,6 +825,65 @@ describe('SRM Round Questions APIs', function () {
754825
], done);
755826
});
756827

828+
it("Valid test for delete question.", function (done) {
829+
830+
var validRequest = {"contest_id": 123, "text": "text2", "styleId": 1, "typeId": 1, "statusId": 1, "keyword": "keyword1", "isRequired": true};
831+
832+
async.waterfall([
833+
function (cb) {
834+
var req = request(API_ENDPOINT)
835+
.post("/v2/data/srm/rounds/13673/questions")
836+
.set("Accept", "application/json")
837+
.expect("Content-Type", /json/);
838+
req.set('Authorization', generateAuthHeader('heffan'));
839+
req.expect(200).send(validRequest).end(function (err, res) {
840+
if (err) {
841+
cb(err);
842+
return;
843+
}
844+
assert.equal(res.body.success, true, "Invalid response detail");
845+
cb();
846+
});
847+
}, function (cb) {
848+
var sql = "question_id from survey_question where survey_id = 13673";
849+
testHelper.runSqlSelectQuery(sql, "informixoltp", function (err, result) {
850+
if (err) {
851+
cb(err);
852+
return;
853+
}
854+
assert.ok(result.length >= 1);
855+
cb(null, result[0].question_id);
856+
});
857+
}, function (questionId, cb) {
858+
createDeleteRequest("/v2/data/srm/rounds/" + questionId + "/question", 'heffan').expect(200).end(function (err, res) {
859+
if (err) {
860+
cb(err);
861+
return;
862+
}
863+
assert.equal(res.body.success, true, "Invalid response detail");
864+
cb(null, questionId);
865+
});
866+
}, function (questionId, cb) {
867+
var sqlQueries = [
868+
"question_id from survey_question where question_id = " + questionId,
869+
"question_id from round_question where question_id = " + questionId,
870+
"question_id from answer where question_id = " + questionId,
871+
"question_id from question where question_id = " + questionId
872+
];
873+
async.forEach(sqlQueries, function (sqlQuery, callback) {
874+
testHelper.runSqlSelectQuery(sqlQuery, "informixoltp", function (err, result) {
875+
if (err) {
876+
callback(err);
877+
return;
878+
}
879+
assert(!result || (Array.isArray(result) && !result.length), "Found question that was supposed to be deleted");
880+
callback();
881+
});
882+
}, cb);
883+
}
884+
], done);
885+
});
886+
757887
it("Valid test for add question answer and list question answer.", function (done) {
758888
var validRequest = {"text": "text2", "styleId": 1, "typeId": 1, "statusId": 1, "keyword": "keyword1", "isRequired": true},
759889
questionId;

0 commit comments

Comments
 (0)