From dbc72feb8731b66fcaf4b25247a2e408cf51f2da Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Sat, 8 Apr 2017 22:30:35 +0100 Subject: [PATCH] Move the parameter parsing into a service for reuse This commit moves the code for parsing the parameters representing search criteria in the URL into a separate service rather than the StoryListController so that it can be reused elsewhere. This commit also completes the implementation such that all useful parameters can be parsed. Change-Id: If2addfcab054a4e869d391dbda5f3845bbd701f6 --- src/app/search/service/search_helper.js | 120 ++++++++++++++++++ .../controller/story_list_controller.js | 54 +------- 2 files changed, 124 insertions(+), 50 deletions(-) create mode 100644 src/app/search/service/search_helper.js diff --git a/src/app/search/service/search_helper.js b/src/app/search/service/search_helper.js new file mode 100644 index 00000000..5610bdc0 --- /dev/null +++ b/src/app/search/service/search_helper.js @@ -0,0 +1,120 @@ +/** + * Copyright (c) 2016 Codethink Ltd + * Copyright (c) 2017 Adam Coldrick + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * A service providing helper functions for search views. + */ +angular.module('sb.search').factory('SearchHelper', + function(User, Project, ProjectGroup, Story, Task, Criteria, $filter) { + 'use strict'; + + /** + * Create search criteria based on some given parameters. + */ + function parseParameters(params, criteria) { + if (params.q) { + criteria.push( + Criteria.create('Text', params.q) + ); + } + if (params.title) { + criteria.push( + Criteria.create('Text', params.title) + ); + } + if (params.status) { + criteria.push( + Criteria.create('StoryStatus', params.status, + $filter('capitalize')(params.status)) + ); + } + if (params.tags) { + if (params.tags.constructor === Array) { + angular.forEach(params.tags, function(tag) { + criteria.push( + Criteria.create('Tags', tag, tag) + ); + }); + } else { + criteria.push( + Criteria.create('Tags', params.tags, params.tags) + ); + } + } + if (params.assignee_id || params.creator_id) { + var id = params.assignee_id || params.creator_id; + User.get({'id': id}).$promise + .then(function(result) { + criteria.push( + Criteria.create('User', + params.assignee_id, + result.full_name) + ); + } + ); + } + if (params.project_id) { + Project.get({'id': params.project_id}).$promise + .then(function(result) { + criteria.push( + Criteria.create('Project', + params.project_id, + result.name) + ); + } + ); + } + if (params.project_group_id) { + ProjectGroup.get({'id': params.project_group_id}).$promise + .then(function(result) { + criteria.push( + Criteria.create('ProjectGroup', + params.project_group_id, + result.title) + ); + } + ); + } + if (params.story_id) { + Story.get({'id': params.story_id}).$promise + .then(function(result) { + criteria.push( + Criteria.create('Story', + params.story_id, + result.title) + ); + } + ); + } + if (params.task_id) { + Task.get({'id': params.task_id}).$promise + .then(function(result) { + criteria.push( + Criteria.create('Task', + params.task_id, + result.title) + ); + } + ); + } + } + + return { + parseParameters: parseParameters + }; + } +); diff --git a/src/app/stories/controller/story_list_controller.js b/src/app/stories/controller/story_list_controller.js index cd7a8ba5..90258db5 100644 --- a/src/app/stories/controller/story_list_controller.js +++ b/src/app/stories/controller/story_list_controller.js @@ -1,5 +1,6 @@ /* * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * Copyright (c) 2016 Codethink Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain @@ -18,9 +19,8 @@ * Controller for our story list. */ angular.module('sb.story').controller('StoryListController', - function ($scope, $state, Criteria, NewStoryService, - SubscriptionList, CurrentUser, $stateParams, $filter, $q, - Tags, User, Project, ProjectGroup) { + function ($scope, $state, Criteria, NewStoryService, SubscriptionList, + CurrentUser, $stateParams, SearchHelper) { 'use strict'; // search results must be of type "story" @@ -29,53 +29,7 @@ angular.module('sb.story').controller('StoryListController', // Search result criteria default must be "active" $scope.defaultCriteria = []; - if ($stateParams.q) { - $scope.defaultCriteria.push( - Criteria.create('Text', $stateParams.q) - ); - } - if ($stateParams.status) { - $scope.defaultCriteria.push( - Criteria.create('StoryStatus', $stateParams.status, - $filter('capitalize')($stateParams.status)) - ); - } - if ($stateParams.tags) { - $scope.defaultCriteria.push( - Criteria.create('Tags', $stateParams.tags, $stateParams.tags) - ); - } - if ($stateParams.assignee_id) { - User.get({'id': $stateParams.assignee_id}).$promise - .then(function(result) { - $scope.defaultCriteria.push( - Criteria.create('User', $stateParams.assignee_id, - result.full_name) - ); - } - ); - } - if ($stateParams.project_id) { - Project.get({'id': $stateParams.project_id}).$promise - .then(function(result) { - $scope.defaultCriteria.push( - Criteria.create('Project', $stateParams.project_id, - result.name) - ); - } - ); - } - if ($stateParams.project_group_id) { - ProjectGroup.get({'id': $stateParams.project_group_id}).$promise - .then(function(result) { - $scope.defaultCriteria.push( - Criteria.create('ProjectGroup', - $stateParams.project_group_id, - result.title) - ); - } - ); - } + SearchHelper.parseParameters($stateParams, $scope.defaultCriteria); /** * Creates a new story.