11/*
22 * Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
3- */
4- /**
5- * - Implement the srm round questions / answers / survey api.
3+ *
4+ * @version 1.2
5+ * @author isv
6+ *
7+ * - Implement the srm round questions / answers / survey api.
68 * Changes in version 1.1 (Module Assembly - Web Arena - Match Configurations):
79 * - Updated getRoundQuestions to send roundId with response
8- *
9- * @version 1.1
10- * @author TCSASSEMBLER
10+ * Changes in 1.2:
11+ * - Added actions for modifying/deleting round question answers.
1112 */
1213
1314/*jslint node: true, nomen: true, plusplus: true, stupid: true, unparam: true */
@@ -16,6 +17,7 @@ var async = require('async');
1617var _ = require ( 'underscore' ) ;
1718var moment = require ( 'moment' ) ;
1819var IllegalArgumentError = require ( '../errors/IllegalArgumentError' ) ;
20+ var NotFoundError = require ( '../errors/NotFoundError' ) ;
1921
2022var DATE_FORMAT = "YYYY-MM-DD HH:mm" ;
2123
@@ -327,7 +329,8 @@ function checkAnswerValues(api, text, sortOrder, correct, callback) {
327329 error = helper . checkStringParameter ( text , "text" , 250 ) ;
328330
329331 if ( ! error && _ . isDefined ( sortOrder ) ) {
330- error = helper . checkPositiveInteger ( sortOrder , "sortOrder" ) ;
332+ error = helper . checkPositiveInteger ( sortOrder , "sortOrder" )
333+ || helper . checkMaxInt ( sortOrder , "sortOrder" ) ;
331334 }
332335
333336 if ( ! error && _ . isDefined ( correct ) ) {
@@ -609,8 +612,8 @@ var modifyRoundQuestion = function (api, connection, dbConnectionMap, next) {
609612 */
610613var deleteRoundQuestion = function ( api , connection , dbConnectionMap , next ) {
611614 var helper = api . helper ,
612- sqlParams = { } ,
613- questionId = Number ( connection . params . questionId ) ;
615+ sqlParams = { } ,
616+ questionId = Number ( connection . params . questionId ) ;
614617
615618 async . waterfall ( [
616619 function ( cb ) {
@@ -631,6 +634,115 @@ var deleteRoundQuestion = function (api, connection, dbConnectionMap, next) {
631634 } ) ;
632635} ;
633636
637+ /**
638+ * Checks if answer with specified ID exists in database.
639+ *
640+ * @param api the api instance.
641+ * @param dbConnectionMap the database connection map.
642+ * @param answerId - the answerId parameter.
643+ * @param callback the callback method.
644+ */
645+ function checkAnswerId ( api , dbConnectionMap , answerId , callback ) {
646+ var helper = api . helper ,
647+ error = helper . checkIdParameter ( answerId , "answerId" ) ;
648+
649+ async . waterfall ( [
650+ function ( cb ) {
651+ if ( ! error ) {
652+ api . dataAccess . executeQuery ( "get_answer_id" , { answerId : answerId } , dbConnectionMap , cb ) ;
653+ } else {
654+ cb ( null , null ) ;
655+ }
656+ } , function ( results , cb ) {
657+ if ( ! error ) {
658+ if ( results . length === 0 ) {
659+ error = new NotFoundError ( "The answerId does not exist in database." ) ;
660+ }
661+ }
662+ cb ( error ) ;
663+ }
664+ ] , function ( err ) {
665+ if ( err ) {
666+ callback ( err ) ;
667+ return ;
668+ }
669+
670+ callback ( null , error ) ;
671+ } ) ;
672+ }
673+
674+ /**
675+ * Modify Round Question Answer.
676+ *
677+ * @param api the api instance.
678+ * @param connection the connection instance.
679+ * @param dbConnectionMap the database connection map.
680+ * @param next the callback method.
681+ */
682+ var modifyRoundQuestionAnswer = function ( api , connection , dbConnectionMap , next ) {
683+ var helper = api . helper ,
684+ sqlParams = { } ,
685+ answerId = Number ( connection . params . answerId ) ,
686+ text = connection . params . text ,
687+ sortOrder = connection . params . sortOrder ,
688+ correct = connection . params . correct ;
689+
690+ async . waterfall ( [
691+ function ( cb ) {
692+ cb ( helper . checkAdmin ( connection , 'Authorized information needed.' , 'Admin access only.' ) ) ;
693+ } , function ( cb ) {
694+ checkAnswerValues ( api , text , sortOrder , correct , cb ) ;
695+ } , function ( error , cb ) {
696+ checkAnswerId ( api , dbConnectionMap , answerId , cb ) ;
697+ } , function ( error , cb ) {
698+ sqlParams . answerId = answerId ;
699+ sqlParams . answerText = text ;
700+ sqlParams . sortOrder = sortOrder ;
701+ sqlParams . correct = ( correct === true || correct . toLowerCase ( ) === "true" ) ? 1 : 0 ;
702+ api . dataAccess . executeQuery ( "update_answer" , sqlParams , dbConnectionMap , cb ) ;
703+ }
704+ ] , function ( err ) {
705+ if ( err ) {
706+ helper . handleError ( api , connection , err ) ;
707+ } else {
708+ connection . response = { "success" : true } ;
709+ }
710+ next ( connection , true ) ;
711+ } ) ;
712+ } ;
713+
714+ /**
715+ * Delete Round Question Answer.
716+ *
717+ * @param api the api instance.
718+ * @param connection the connection instance.
719+ * @param dbConnectionMap the database connection map.
720+ * @param next the callback method.
721+ */
722+ var deleteRoundQuestionAnswer = function ( api , connection , dbConnectionMap , next ) {
723+ var helper = api . helper ,
724+ sqlParams = { } ,
725+ answerId = Number ( connection . params . answerId ) ;
726+
727+ async . waterfall ( [
728+ function ( cb ) {
729+ cb ( helper . checkAdmin ( connection , 'Authorized information needed.' , 'Admin access only.' ) ) ;
730+ } , function ( cb ) {
731+ cb ( helper . checkIdParameter ( answerId , 'answerId' ) ) ;
732+ } , function ( cb ) {
733+ sqlParams . answerId = answerId ;
734+ api . dataAccess . executeQuery ( "delete_answer" , sqlParams , dbConnectionMap , cb ) ;
735+ }
736+ ] , function ( err , result ) {
737+ if ( err ) {
738+ helper . handleError ( api , connection , err ) ;
739+ } else {
740+ connection . response = { "success" : result > 0 } ;
741+ }
742+ next ( connection , true ) ;
743+ } ) ;
744+ } ;
745+
634746/**
635747 * The API for get Round Questions API.
636748 */
@@ -802,3 +914,53 @@ exports.deleteRoundQuestion = {
802914 }
803915 }
804916} ;
917+
918+ /**
919+ * The API for Modify Round Question Answer API.
920+ */
921+ exports . modifyRoundQuestionAnswer = {
922+ name : "modifyRoundQuestionAnswer" ,
923+ description : "Modify Round Question Answer" ,
924+ inputs : {
925+ required : [ 'answerId' , 'text' , 'sortOrder' , 'correct' ] ,
926+ optional : [ ]
927+ } ,
928+ blockedConnectionTypes : [ ] ,
929+ outputExample : { } ,
930+ version : 'v2' ,
931+ transaction : 'write' ,
932+ databases : [ "informixoltp" ] ,
933+ run : function ( api , connection , next ) {
934+ if ( connection . dbConnectionMap ) {
935+ api . log ( "Execute modifyRoundQuestionAnswer#run" , 'debug' ) ;
936+ modifyRoundQuestionAnswer ( api , connection , connection . dbConnectionMap , next ) ;
937+ } else {
938+ api . helper . handleNoConnection ( api , connection , next ) ;
939+ }
940+ }
941+ } ;
942+
943+ /**
944+ * The API for Delete Round Question Answer API.
945+ */
946+ exports . deleteRoundQuestionAnswer = {
947+ name : "deleteRoundQuestionAnswer" ,
948+ description : "Delete Round Question Answer" ,
949+ inputs : {
950+ required : [ 'answerId' ] ,
951+ optional : [ ]
952+ } ,
953+ blockedConnectionTypes : [ ] ,
954+ outputExample : { } ,
955+ version : 'v2' ,
956+ transaction : 'write' ,
957+ databases : [ "informixoltp" ] ,
958+ run : function ( api , connection , next ) {
959+ if ( connection . dbConnectionMap ) {
960+ api . log ( "Execute deleteRoundQuestionAnswer#run" , 'debug' ) ;
961+ deleteRoundQuestionAnswer ( api , connection , connection . dbConnectionMap , next ) ;
962+ } else {
963+ api . helper . handleNoConnection ( api , connection , next ) ;
964+ }
965+ }
966+ } ;
0 commit comments