|
1 | 1 | /* |
2 | 2 | * Copyright (C) 2013-2014 TopCoder Inc., All Rights Reserved. |
3 | 3 | * |
4 | | - * @version 1.8 |
| 4 | + * @version 1.9 |
5 | 5 | * @author Sky_, freegod, panoptimum, Ghost_141 |
6 | 6 | * changes in 1.1: |
7 | 7 | * - implement srm API |
|
26 | 26 | * - Update search srm challenges api. Add challengeName filter. |
27 | 27 | * Changes in 1.8: |
28 | 28 | * - Implement get srm practice problems api. |
| 29 | + * Changes in 1.9: |
| 30 | + * - Implement Rounds For Problem API |
29 | 31 | */ |
30 | 32 | /*jslint node: true, nomen: true */ |
31 | 33 | "use strict"; |
@@ -2137,3 +2139,112 @@ exports.getPracticeProblems = { |
2137 | 2139 | } |
2138 | 2140 | } |
2139 | 2141 | }; |
| 2142 | + |
| 2143 | +/** |
| 2144 | + * getSrmRoundsForProblem implements the rounds for problem api. |
| 2145 | + * |
| 2146 | + * @param {Object} api - The api object. |
| 2147 | + * @param {Object} connection - The connection object. |
| 2148 | + * @param {Function} next - The callback function. |
| 2149 | + */ |
| 2150 | +function getSrmRoundsForProblem(api, connection, next) { |
| 2151 | + // control flow designators |
| 2152 | + var problemId = 'problemId', |
| 2153 | + checkProblem = 'checkProblem', |
| 2154 | + // shortcuts |
| 2155 | + helper = api.helper, |
| 2156 | + dbConnectionMap = connection.dbConnectionMap, |
| 2157 | + dataAccess = _.bind(api.dataAccess.executeQuery, api.dataAccess); |
| 2158 | + return async.waterfall( |
| 2159 | + [ |
| 2160 | + function (cb) { |
| 2161 | + var id = parseInt(connection.params.problemId, 10), |
| 2162 | + error = helper.checkIdParameter(id, problemId), |
| 2163 | + results = {}; |
| 2164 | + if (error) { |
| 2165 | + return cb(error); |
| 2166 | + } |
| 2167 | + results[problemId] = id; |
| 2168 | + return cb(null, results); |
| 2169 | + }, |
| 2170 | + function (results, cb) { |
| 2171 | + var id = results[problemId]; |
| 2172 | + dataAccess( |
| 2173 | + 'check_problem_exists', |
| 2174 | + {problem_id: id}, |
| 2175 | + dbConnectionMap, |
| 2176 | + function (error, result) { |
| 2177 | + if (error) { |
| 2178 | + return cb(error); |
| 2179 | + } |
| 2180 | + results[checkProblem] = result; |
| 2181 | + return cb(null, results); |
| 2182 | + } |
| 2183 | + ); |
| 2184 | + }, |
| 2185 | + function (results, cb) { |
| 2186 | + if (results[checkProblem][0].is_there) { |
| 2187 | + return cb(null, results); |
| 2188 | + } |
| 2189 | + return cb(new NotFoundError("The problem doesn't exist.")); |
| 2190 | + }, |
| 2191 | + function (results, cb) { |
| 2192 | + var id = results[problemId]; |
| 2193 | + return dataAccess( |
| 2194 | + 'get_rounds_for_problem', |
| 2195 | + {problem_id: id}, |
| 2196 | + dbConnectionMap, |
| 2197 | + function (error, result) { |
| 2198 | + if (error) { |
| 2199 | + return cb(error); |
| 2200 | + } |
| 2201 | + return cb( |
| 2202 | + null, |
| 2203 | + { |
| 2204 | + rounds: helper.transferDBResults2Response(result) |
| 2205 | + } |
| 2206 | + ); |
| 2207 | + } |
| 2208 | + ); |
| 2209 | + } |
| 2210 | + ], |
| 2211 | + function (error, results) { |
| 2212 | + if (error) { |
| 2213 | + helper.handleError(api, connection, error); |
| 2214 | + return next(connection, true); |
| 2215 | + } |
| 2216 | + connection.response = results; |
| 2217 | + return next(connection, true); |
| 2218 | + } |
| 2219 | + ); |
| 2220 | +} |
| 2221 | + |
| 2222 | +/** |
| 2223 | + * Rounds For Problem API |
| 2224 | + * |
| 2225 | + * This api returns the rounds that used the given problem (identified by problem id). |
| 2226 | + * This api will exclude the practice rounds. |
| 2227 | + * This api includes only finished rounds |
| 2228 | + * |
| 2229 | + * @since 1.9 |
| 2230 | + */ |
| 2231 | +exports.getSrmRoundsForProblem = { |
| 2232 | + name: "getSrmRoundsForProblem", |
| 2233 | + description: "SRM Rounds For Problem API", |
| 2234 | + inputs: { |
| 2235 | + required: ['problemId'], |
| 2236 | + optional: [] |
| 2237 | + }, |
| 2238 | + blockedConnectionTypes: [], |
| 2239 | + outputExample: {}, |
| 2240 | + version: 'v2', |
| 2241 | + transaction: 'read', // this action is read-only |
| 2242 | + databases: ["topcoder_dw"], |
| 2243 | + run: function (api, connection, next) { |
| 2244 | + if (connection.dbConnectionMap) { |
| 2245 | + api.log("Execute getSrmRoundsForProblem", 'debug'); |
| 2246 | + return getSrmRoundsForProblem(api, connection, next); |
| 2247 | + } |
| 2248 | + return api.helper.handleNoConnection(api, connection, next); |
| 2249 | + } |
| 2250 | +}; |
0 commit comments