1
1
/*
2
2
* 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.
6
8
* Changes in version 1.1 (Module Assembly - Web Arena - Match Configurations):
7
9
* - 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.
11
12
*/
12
13
13
14
/*jslint node: true, nomen: true, plusplus: true, stupid: true, unparam: true */
@@ -16,6 +17,7 @@ var async = require('async');
16
17
var _ = require ( 'underscore' ) ;
17
18
var moment = require ( 'moment' ) ;
18
19
var IllegalArgumentError = require ( '../errors/IllegalArgumentError' ) ;
20
+ var NotFoundError = require ( '../errors/NotFoundError' ) ;
19
21
20
22
var DATE_FORMAT = "YYYY-MM-DD HH:mm" ;
21
23
@@ -327,7 +329,8 @@ function checkAnswerValues(api, text, sortOrder, correct, callback) {
327
329
error = helper . checkStringParameter ( text , "text" , 250 ) ;
328
330
329
331
if ( ! error && _ . isDefined ( sortOrder ) ) {
330
- error = helper . checkPositiveInteger ( sortOrder , "sortOrder" ) ;
332
+ error = helper . checkPositiveInteger ( sortOrder , "sortOrder" )
333
+ || helper . checkMaxInt ( sortOrder , "sortOrder" ) ;
331
334
}
332
335
333
336
if ( ! error && _ . isDefined ( correct ) ) {
@@ -609,8 +612,8 @@ var modifyRoundQuestion = function (api, connection, dbConnectionMap, next) {
609
612
*/
610
613
var deleteRoundQuestion = function ( api , connection , dbConnectionMap , next ) {
611
614
var helper = api . helper ,
612
- sqlParams = { } ,
613
- questionId = Number ( connection . params . questionId ) ;
615
+ sqlParams = { } ,
616
+ questionId = Number ( connection . params . questionId ) ;
614
617
615
618
async . waterfall ( [
616
619
function ( cb ) {
@@ -631,6 +634,115 @@ var deleteRoundQuestion = function (api, connection, dbConnectionMap, next) {
631
634
} ) ;
632
635
} ;
633
636
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
+
634
746
/**
635
747
* The API for get Round Questions API.
636
748
*/
@@ -802,3 +914,53 @@ exports.deleteRoundQuestion = {
802
914
}
803
915
}
804
916
} ;
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