11/*
22 * Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
33 *
4- * @version 1.0
4+ * @version 1.1
55 * @author Ghost_141
6+ * Changes in 1.1:
7+ * - Add technologies and platforms filter.
68 */
79"use strict" ;
810
911var async = require ( 'async' ) ;
1012var _ = require ( 'underscore' ) ;
13+ var fs = require ( 'fs' ) ;
1114var BadRequestError = require ( '../errors/BadRequestError' ) ;
1215var UnauthorizedError = require ( '../errors/UnauthorizedError' ) ;
1316var ForbiddenError = require ( '../errors/ForbiddenError' ) ;
@@ -17,6 +20,38 @@ var ForbiddenError = require('../errors/ForbiddenError');
1720 */
1821var VALID_CHALLENGE_TYPE = [ 'develop' , 'design' , 'data' ] ;
1922
23+ /**
24+ * The technology filter for challenges api.
25+ * @since 1.1
26+ */
27+ var TECHNOLOGY_FILTER = ' AND EXISTS (SELECT DISTINCT 1 FROM comp_technology ct WHERE ct.comp_vers_id = pi1.value ' +
28+ 'AND ct.technology_type_id IN (@filter@))' ;
29+
30+ /**
31+ * The platform filter for challenges api.
32+ * @since 1.1
33+ */
34+ var PLATFORM_FILTER = ' AND EXISTS (SELECT 1 FROM project_platform pp WHERE pp.project_platform_id IN (@filter@) ' +
35+ 'AND p.project_id = pp.project_id)' ;
36+
37+ /**
38+ * Add tech filter and platform filter.
39+ * @param query
40+ * @param techId
41+ * @param platformId
42+ * @param helper
43+ * @returns {* }
44+ */
45+ function addFilter ( query , techId , platformId , helper ) {
46+ if ( _ . isDefined ( techId ) ) {
47+ query = helper . editSql ( query , TECHNOLOGY_FILTER , techId . join ( ',' ) ) ;
48+ }
49+ if ( _ . isDefined ( platformId ) ) {
50+ query = helper . editSql ( query , PLATFORM_FILTER , platformId . join ( ',' ) ) ;
51+ }
52+ return query ;
53+ }
54+
2055/**
2156 * Get the challenges RSS information.
2257 *
@@ -30,6 +65,10 @@ function getChallengesRSS(api, connection, next) {
3065 RSSMaxLength = api . config . tcConfig . maxRSSLength ,
3166 positionsRemain = RSSMaxLength ,
3267 challengeType = connection . params . challengeType ,
68+ technologies = connection . params . technologies ,
69+ techId ,
70+ platforms = connection . params . platforms ,
71+ platformId ,
3372 listType = ( connection . params . listType || helper . ListType . OPEN ) . toUpperCase ( ) ,
3473 copyToResult = function ( queryResults ) {
3574 if ( positionsRemain > 0 ) {
@@ -68,51 +107,78 @@ function getChallengesRSS(api, connection, next) {
68107 error = helper . checkContains ( VALID_CHALLENGE_TYPE , challengeType . toLowerCase ( ) , 'challengeType' ) ;
69108 }
70109 error = error || helper . checkContains ( helper . VALID_LIST_TYPE , listType , 'listType' ) ;
71- if ( error ) {
72- cb ( error ) ;
73- return ;
74- }
75110
76111 challengeType = ( challengeType || 'all' ) . toLowerCase ( ) ;
77-
112+ cb ( error ) ;
113+ } ,
114+ function ( cb ) {
115+ if ( ! _ . isUndefined ( technologies ) ) {
116+ helper . getCatalogCachedValue ( technologies . split ( ',' ) , dbConnectionMap , 'technologies' , cb ) ;
117+ } else {
118+ cb ( null , null ) ;
119+ }
120+ } ,
121+ function ( id , cb ) {
122+ if ( _ . isDefined ( techId ) ) {
123+ techId = id ;
124+ }
125+ if ( ! _ . isUndefined ( platforms ) ) {
126+ helper . getCatalogCachedValue ( platforms . split ( ',' ) , dbConnectionMap , 'platforms' , cb ) ;
127+ } else {
128+ cb ( null , null ) ;
129+ }
130+ } ,
131+ function ( id , cb ) {
132+ if ( _ . isDefined ( id ) ) {
133+ platformId = id ;
134+ }
135+ helper . readQuery ( 'get_software_studio_challenges_rss' , cb ) ;
136+ } ,
137+ function ( q , cb ) {
138+ q = addFilter ( q , techId , platformId , helper ) ;
139+ // edit the sql
78140 async . parallel ( {
79141 design : function ( cbx ) {
80142 if ( challengeType === 'design' || challengeType === 'all' ) {
81- api . dataAccess . executeQuery ( 'get_software_studio_challenges_rss' ,
143+ api . dataAccess . executeSqlQuery ( q ,
82144 {
83145 page_size : RSSMaxLength ,
84146 project_status_id : helper . LIST_TYPE_PROJECT_STATUS_MAP [ listType ] ,
85147 project_type_id : helper . studio . category ,
86148 registration_phase_status : helper . LIST_TYPE_REGISTRATION_STATUS_MAP [ listType ]
87- } , dbConnectionMap , cbx ) ;
149+ } , 'tcs_catalog' , dbConnectionMap , cbx ) ;
88150 } else {
89151 cbx ( ) ;
90152 }
91153 } ,
92154 develop : function ( cbx ) {
93155 if ( challengeType === 'develop' || challengeType === 'all' ) {
94- api . dataAccess . executeQuery ( 'get_software_studio_challenges_rss' ,
156+ api . dataAccess . executeSqlQuery ( q ,
95157 {
96158 page_size : RSSMaxLength ,
97159 project_status_id : helper . LIST_TYPE_PROJECT_STATUS_MAP [ listType ] ,
98160 project_type_id : helper . software . category ,
99161 registration_phase_status : helper . LIST_TYPE_REGISTRATION_STATUS_MAP [ listType ]
100- } , dbConnectionMap , cbx ) ;
162+ } , 'tcs_catalog' , dbConnectionMap , cbx ) ;
101163 } else {
102164 cbx ( ) ;
103165 }
104166 } ,
105167 data : function ( cbx ) {
106- if ( challengeType === 'data' || challengeType === 'all' ) {
107- if ( listType === helper . ListType . PAST ) {
108- api . dataAccess . executeQuery ( 'get_past_data_challenges_rss' , { page_size : RSSMaxLength } , dbConnectionMap , cbx ) ;
109- } else if ( listType === helper . ListType . OPEN || listType === helper . ListType . ACTIVE ) {
110- api . dataAccess . executeQuery ( 'get_open_data_challenges_rss' , { page_size : RSSMaxLength } , dbConnectionMap , cbx ) ;
168+ if ( _ . isDefined ( techId ) || _ . isDefined ( platformId ) ) {
169+ cbx ( ) ;
170+ } else {
171+ if ( challengeType === 'data' || challengeType === 'all' ) {
172+ if ( listType === helper . ListType . PAST ) {
173+ api . dataAccess . executeQuery ( 'get_past_data_challenges_rss' , { page_size : RSSMaxLength } , dbConnectionMap , cbx ) ;
174+ } else if ( listType === helper . ListType . OPEN || listType === helper . ListType . ACTIVE ) {
175+ api . dataAccess . executeQuery ( 'get_open_data_challenges_rss' , { page_size : RSSMaxLength } , dbConnectionMap , cbx ) ;
176+ } else {
177+ cbx ( ) ;
178+ }
111179 } else {
112180 cbx ( ) ;
113181 }
114- } else {
115- cbx ( ) ;
116182 }
117183 }
118184 } , cb ) ;
0 commit comments