From 709fbdef42ce5dc7de56fd2f53d99259fb4ccec8 Mon Sep 17 00:00:00 2001 From: Victor Boctor Date: Sat, 22 Apr 2017 12:21:55 -0700 Subject: [PATCH] REST: Extend get issue to return 1 or many - Use consistent response payload when returning 1 or many. This changed the payload when getting a single issue. - Support pagination when returning multiple issues. - Support returning multiple issues based on a `project_id`. - Support returning multiple issues based on a filter id + `project_id`. --- api/rest/restcore/issues_rest.php | 40 +++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/api/rest/restcore/issues_rest.php b/api/rest/restcore/issues_rest.php index 2b4aa56714..9ebad34ea5 100644 --- a/api/rest/restcore/issues_rest.php +++ b/api/rest/restcore/issues_rest.php @@ -40,11 +40,43 @@ * @return \Slim\Http\Response The augmented response. */ function rest_issue_get( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) { - # Username and password below are ignored, since middleware already done the auth. - $t_result = mc_issue_get( /* username */ '', /* password */ '', $p_request->getParam( 'id' ) ); + $t_issue_id = $p_request->getParam( 'id' ); - if( ApiObjectFactory::isFault( $t_result ) ) { - return $p_response->withStatus( $t_result->status_code, $t_result->fault_string ); + if( !is_blank( $t_issue_id ) ) { + # Get Issue By Id + + # Username and password below are ignored, since middleware already done the auth. + $t_issue = mc_issue_get( /* username */ '', /* password */ '', $t_issue_id ); + + if( ApiObjectFactory::isFault( $t_issue ) ) { + return $p_response->withStatus( $t_result->status_code, $t_result->fault_string ); + } + + $t_result = array( 'issues' => array( $t_issue ) ); + } else { + $t_page_number = $p_request->getParam( 'page', 1 ); + $t_page_size = $p_request->getParam( 'page_size', 50 ); + + # Get a set of issues + $t_project_id = (int)$p_request->getParam( 'project_id', ALL_PROJECTS ); + if( $t_project_id != ALL_PROJECTS && !project_exists( $t_project_id ) ) { + # TODO: What's best was to escape $t_project? + $t_message = "Project '$t_project_id' doesn't exist"; + return $p_response->withStatus( HTTP_STATUS_NOT_FOUND, $t_message ); + } + + $t_filter_id = (int)$p_request->getParam( 'filter_id', 0 ); + if( $t_filter_id !== 0 ) { + # TODO: we should have a better way to do this. + global $g_project_override; + $g_project_override = $t_project_id; + + $t_issues = mc_filter_get_issues( '', '', $t_project_id, $t_filter_id, $t_page_number, $t_page_size ); + } else { + $t_issues = mc_project_get_issues( '', '', $t_project_id, $t_page_number, $t_page_size ); + } + + $t_result = array( 'issues' => $t_issues ); } return $p_response->withStatus( HTTP_STATUS_SUCCESS )->withJson( $t_result );