diff --git a/doc/apiv3-documentation.apib b/doc/apiv3-documentation.apib index 5c5fb37a73..6a793b0282 100644 --- a/doc/apiv3-documentation.apib +++ b/doc/apiv3-documentation.apib @@ -1880,18 +1880,25 @@ but are also limited to the projects that the current user is allowed to see. # Group Queries +| Link | Description | Type | Constraints | Supported operations | Condition | +| :----------------: | ------------------------------------------------------------------------ | ------------ | ------------ | --------------------- | ----------------------------------------- | +| self | This query | Query | not null | READ | | +| user | The user that owns this query | User | not null | READ | | +| project | The project on which this query operates | Project | | READ | | + ## Local Properties -| Property | Description | Type | Constraints | Supported operations | -| :---------: | ------------- | ---- | ----------- | -------------------- | -| id | Query id | Integer | x > 0 | READ | -| name | Query name | String | | READ | -| filters | | String | | READ | -| isPublic | | Boolean | | READ | -| columnNames | | String | | READ | -| sortCriteria | | String | | READ | -| groupBy | | String | | READ | -| displaySums | | Boolean | | READ | -| isStarred | | Boolean | | READ | + +| Property | Description | Type | Constraints | Supported operations | +| :--------------: | ------------------------------------------------------ | ----------- | -------------------------------- | -------------------- | +| id | Query id | Integer | x > 0 | READ | +| name | Query name | String | | READ | +| filters | An object describing the queries filter conditions | Object | | READ | +| columnNames | Ordered list of properties to be shown in this query | String[] | | READ | +| sortCriteria | An object describing the sorting rules of this query | Object | | READ | +| groupBy | The property to group results of this query by | String | | READ | +| displaySums | Should sums (of supported properties) be shown? | Boolean | | READ | +| isPublic | Can users besides the owner see the query? | Boolean | | READ | +| isStarred | Should the query be highlighted to the user? | Boolean | | READ | ## Query [/api/v3/queries/{id}] @@ -1907,11 +1914,11 @@ but are also limited to the projects that the current user is allowed to see. }, "project": { "href": "/api/v3/projects/1", - "title": "Lorem ipsum" + "title": "A project" }, "user": { "href": "/api/v3/users/1", - "title": "John Sheppard - admin" + "title": "John Sheppard" } }, "id": 2, @@ -1932,7 +1939,7 @@ but are also limited to the projects that the current user is allowed to see. } } ], - "isPublic": "false", + "isPublic": false, "columnNames": [ "type", "status", @@ -1947,7 +1954,7 @@ but are also limited to the projects that the current user is allowed to see. ] ], "groupBy": null, - "displaySums": "false", + "displaySums": false, "isStarred": true } diff --git a/lib/api/v3/queries/query_representer.rb b/lib/api/v3/queries/query_representer.rb index d44b6f5ceb..22f5533ee8 100644 --- a/lib/api/v3/queries/query_representer.rb +++ b/lib/api/v3/queries/query_representer.rb @@ -34,28 +34,21 @@ module API module V3 module Queries class QueryRepresenter < ::API::Decorators::Single - link :self do - { - href: api_v3_paths.query(represented.id), - title: "#{represented.name}" - } - end - property :id, render_nil: true - property :name, render_nil: true - property :project_id, getter: -> (*) { project.id } - property :project_name, getter: -> (*) { project.try(:name) } - property :user_id, getter: -> (*) { user.try(:id) }, render_nil: true - property :user_name, getter: -> (*) { user.try(:name) }, render_nil: true - property :user_login, getter: -> (*) { user.try(:login) }, render_nil: true - property :user_mail, getter: -> (*) { user.try(:mail) }, render_nil: true + self_link + + linked_property :user + linked_property :project + + property :id + property :name property :filters, render_nil: true - property :is_public, getter: -> (*) { is_public.to_s }, render_nil: true + property :is_public, getter: -> (*) { is_public } property :column_names, render_nil: true property :sort_criteria, render_nil: true property :group_by, render_nil: true - property :display_sums, getter: -> (*) { display_sums.to_s }, render_nil: true - property :is_starred, getter: -> (*) { (!query_menu_item.nil?).to_s } + property :display_sums, getter: -> (*) { display_sums } + property :is_starred, getter: -> (*) { starred } def _type 'Query' diff --git a/spec/lib/api/v3/queries/query_representer_spec.rb b/spec/lib/api/v3/queries/query_representer_spec.rb new file mode 100644 index 0000000000..93fe61db55 --- /dev/null +++ b/spec/lib/api/v3/queries/query_representer_spec.rb @@ -0,0 +1,131 @@ +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +require 'spec_helper' + +describe ::API::V3::Queries::QueryRepresenter do + include ::API::V3::Utilities::PathHelper + + let(:query) { + FactoryGirl.build_stubbed(:query) + } + let(:representer) { described_class.new(query) } + + subject { representer.to_json } + + describe 'generation' do + describe '_links' do + it_behaves_like 'has a titled link' do + let(:link) { 'self' } + let(:href) { api_v3_paths.query query.id } + let(:title) { query.name } + end + + it_behaves_like 'has a titled link' do + let(:link) { 'user' } + let(:href) { api_v3_paths.user query.user_id } + let(:title) { query.user.name } + end + + it_behaves_like 'has a titled link' do + let(:link) { 'project' } + let(:href) { api_v3_paths.project query.project_id } + let(:title) { query.project.name } + end + + context 'has no project' do + let(:query) { FactoryGirl.build_stubbed(:query, project: nil) } + + it_behaves_like 'has an empty link' do + let(:link) { 'project' } + end + end + end + + it 'should show an id' do + is_expected.to be_json_eql(query.id).at_path('id') + end + + it 'should show the query name' do + is_expected.to be_json_eql(query.name.to_json).at_path('name') + end + + it 'should show the grouping column' do + is_expected.to be_json_eql(query.group_by.to_json).at_path('groupBy') + end + + it 'should indicate whether sums are shown' do + is_expected.to be_json_eql(query.display_sums.to_json).at_path('displaySums') + end + + it 'should indicate whether the query is publicly visible' do + is_expected.to be_json_eql(query.is_public.to_json).at_path('isPublic') + end + + describe 'with filters' do + let(:query) { + FactoryGirl.build_stubbed(:query, + filters: [ + Queries::WorkPackages::Filter.new('subject', + operator: '=', + values: ['foo']) + ]) + } + + it 'should render the filters' do + expected = [ + { + subject: { + operator: '=', + values: ['foo'] + } + } + ] + is_expected.to be_json_eql(expected.to_json).at_path('filters') + end + end + + describe 'with sort criteria' do + let(:sorting) { [['subject', 'asc'], ['type', 'desc']] } + let(:query) { FactoryGirl.build_stubbed(:query, sort_criteria: sorting) } + + it 'should render the filters' do + is_expected.to be_json_eql(sorting.to_json).at_path('sortCriteria') + end + end + + describe 'with columns' do + let(:columns) { ['subject', 'type'] } + let(:query) { FactoryGirl.build_stubbed(:query, column_names: columns) } + + it 'should render the filters' do + is_expected.to be_json_eql(columns.to_json).at_path('columnNames') + end + end + end +end