OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
implement GET /api/v3/days/week
Christophe Bliardcc8fa1f7be36
diff --git a/app/models/queries/week_days/week_day_query.rb b/app/models/queries/week_days/week_day_query.rbnew file mode 100644index 00000000000..4523c0cfdef--- /dev/null+++ b/app/models/queries/week_days/week_day_query.rb@@ -0,0 +1,30 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++class Queries::WeekDays::WeekDayQuery < Queries::BaseQuery+enddiff --git a/app/models/week_day.rb b/app/models/week_day.rbnew file mode 100644index 00000000000..ebc0b43a602--- /dev/null+++ b/app/models/week_day.rb@@ -0,0 +1,6 @@+class WeekDay < ApplicationRecord+ def name+ day_names = I18n.t('date.day_names')+ day_names[day % 7]+ end+enddiff --git a/db/migrate/20220511124930_create_week_days.rb b/db/migrate/20220511124930_create_week_days.rbnew file mode 100644index 00000000000..d2f20ac40aa--- /dev/null+++ b/db/migrate/20220511124930_create_week_days.rb@@ -0,0 +1,21 @@+class CreateWeekDays < ActiveRecord::Migration[6.1]+ def up+ create_table :week_days do |t|+ t.integer :day, null: false+ t.boolean :working, null: false, default: true++ t.timestamps+ end++ execute <<-SQL.squish+ ALTER TABLE week_days+ ADD CONSTRAINT unique_day_number UNIQUE (day);+ ALTER TABLE week_days+ ADD CHECK (day >= 1 AND day <=7);+ SQL+ end++ def down+ drop_table :week_days+ end+enddiff --git a/lib/api/decorators/single.rb b/lib/api/decorators/single.rbindex e782cc9ba45..2d1dcb2c684 100644--- a/lib/api/decorators/single.rb+++ b/lib/api/decorators/single.rb@@ -30,8 +30,6 @@ require 'roar/decorator'require 'roar/hypermedia'require 'roar/json/hal'-require 'api/v3/utilities/path_helper'-module APImodule Decoratorsclass Single < ::Roar::Decoratordiff --git a/lib/api/v3/days/days_api.rb b/lib/api/v3/days/days_api.rbnew file mode 100644index 00000000000..efc828d06ea--- /dev/null+++ b/lib/api/v3/days/days_api.rb@@ -0,0 +1,37 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++module API::V3::Days+ class DaysAPI < ::API::OpenProjectAPI+ helpers ::API::Utilities::UrlPropsParsingHelper++ resources :days do+ mount WeekAPI+ end+ end+enddiff --git a/lib/api/v3/days/week_api.rb b/lib/api/v3/days/week_api.rbnew file mode 100644index 00000000000..41e6aa23fe3--- /dev/null+++ b/lib/api/v3/days/week_api.rb@@ -0,0 +1,40 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++module API::V3::Days+ class WeekAPI < ::API::OpenProjectAPI+ helpers ::API::Utilities::UrlPropsParsingHelper++ resources :week do+ get &::API::V3::Utilities::Endpoints::Index.new(model: WeekDay,+ render_representer: WeekDayCollectionRepresenter,+ self_path: -> { api_v3_paths.days_week })+ .mount+ end+ end+enddiff --git a/lib/api/v3/days/week_day_collection_representer.rb b/lib/api/v3/days/week_day_collection_representer.rbnew file mode 100644index 00000000000..eade97b5b45--- /dev/null+++ b/lib/api/v3/days/week_day_collection_representer.rb@@ -0,0 +1,32 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++module API::V3::Days+ class WeekDayCollectionRepresenter < ::API::Decorators::UnpaginatedCollection+ end+enddiff --git a/lib/api/v3/days/week_day_representer.rb b/lib/api/v3/days/week_day_representer.rbnew file mode 100644index 00000000000..45f2f90a2d3--- /dev/null+++ b/lib/api/v3/days/week_day_representer.rb@@ -0,0 +1,48 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++module API::V3::Days+ class WeekDayRepresenter < ::API::Decorators::Single+ include ::API::Caching::CachedRepresenter++ property :day, render_nil: true+ property :name, render_nil: true+ property :working++ link :self do+ {+ href: api_v3_paths.days_week_day(represented.day),+ title: represented.name+ }+ end++ def _type+ 'WeekDay'+ end+ end+enddiff --git a/lib/api/v3/root.rb b/lib/api/v3/root.rbindex dddbe27eded..db5199e7807 100644--- a/lib/api/v3/root.rb+++ b/lib/api/v3/root.rb@@ -52,6 +52,7 @@ module APImount ::API::V3::Configuration::ConfigurationAPImount ::API::V3::CustomActions::CustomActionsAPImount ::API::V3::CustomOptions::CustomOptionsAPI+ mount ::API::V3::Days::DaysAPImount ::API::V3::Notifications::NotificationsAPImount ::API::V3::HelpTexts::HelpTextsAPImount ::API::V3::Memberships::MembershipsAPI@@ -80,7 +81,7 @@ module APImount ::API::V3::Grids::GridsAPIget '/' do- RootRepresenter.new({}, current_user: current_user)+ RootRepresenter.new({}, current_user:)endget '/spec.json' dodiff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex c8e0e27b050..f998da95d6d 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -192,6 +192,14 @@ module API"#{root}/custom_options/#{id}"end+ def self.days_week+ "#{root}/days/week"+ end++ def self.days_week_day(day)+ "#{days_week}/#{day}"+ end+index :help_textshow :help_text@@ -473,7 +481,7 @@ module API"#{project_id}-#{type_id}"end- filter = [{ id: { operator: '=', values: values } }]+ filter = [{ id: { operator: '=', values: } }]path + "?filters=#{CGI.escape(filter.to_s)}"end@@ -497,8 +505,8 @@ module APIsortBy: sort_by&.to_json,groupBy: group_by,pageSize: page_size,- offset: offset,- select: select+ offset:,+ select:}.compact_blankif query_params.any?diff --git a/spec/factories/week_day_factory.rb b/spec/factories/week_day_factory.rbnew file mode 100644index 00000000000..e8c7e45015c--- /dev/null+++ b/spec/factories/week_day_factory.rb@@ -0,0 +1,24 @@+FactoryBot.define do+ factory :week_day do+ sequence :day, [1, 2, 3, 4, 5, 6, 7].cycle+ working { day < 6 }++ # hack to reuse the day if it already exists in database+ to_create do |instance|+ instance.attributes = WeekDay.find_or_create_by(instance.attributes.slice("day", "working")).attributes+ instance.instance_variable_set('@new_record', false)+ end+ end++ # Factory to create all 7 week days at once+ factory :week_days, class: 'Array' do+ # Skip the create callback to be able to use non-AR models. Otherwise FactoryBot will+ # try to call #save! on any created object.+ skip_create++ initialize_with do+ days = 1.upto(7).map { |day| create(:week_day, day:) }+ new(days)+ end+ end+enddiff --git a/spec/lib/api/v3/days/week_day_collection_representer_spec.rb b/spec/lib/api/v3/days/week_day_collection_representer_spec.rbnew file mode 100644index 00000000000..af716f91db6--- /dev/null+++ b/spec/lib/api/v3/days/week_day_collection_representer_spec.rb@@ -0,0 +1,44 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++describe ::API::V3::Days::WeekDayCollectionRepresenter do+ let(:week_days) { build(:week_days) }+ let(:representer) do+ described_class.new(week_days,+ self_link: '/api/v3/days/week',+ current_user: instance_double(User, name: 'current_user'))+ end++ describe '#to_json' do+ subject(:collection) { representer.to_json }++ it_behaves_like 'unpaginated APIv3 collection', 7, 'days/week', 'WeekDay'+ end+enddiff --git a/spec/lib/api/v3/days/week_day_representer_spec.rb b/spec/lib/api/v3/days/week_day_representer_spec.rbnew file mode 100644index 00000000000..82e955f6067--- /dev/null+++ b/spec/lib/api/v3/days/week_day_representer_spec.rb@@ -0,0 +1,110 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++describe ::API::V3::Days::WeekDayRepresenter do+ let(:week_day) { build_stubbed(:week_day, day: 1) }+ let(:representer) { described_class.new(week_day, current_user: instance_double(User, name: 'current_user')) }++ describe '#to_json' do+ subject(:generated) { representer.to_json }++ it 'has _type: WeekDay' do+ expect(subject).to be_json_eql('WeekDay'.to_json).at_path('_type')+ end++ it 'has day integer property' do+ expect(subject).to have_json_type(Integer).at_path('day')+ expect(subject).to be_json_eql(week_day.day.to_json).at_path('day')+ end++ it 'has name string property' do+ expect(subject).to have_json_type(String).at_path('name')+ expect(subject).to be_json_eql(week_day.name.to_json).at_path('name')+ end++ it 'has working boolean property' do+ expect(subject).to have_json_type(TrueClass).at_path('working')+ expect(subject).to be_json_eql(week_day.working.to_json).at_path('working')+ end++ describe '_links' do+ it 'is present' do+ expect(subject).to have_json_type(Object).at_path('_links')+ end++ describe 'self' do+ it 'links to this resource' do+ expected_json = {+ href: "/api/v3/days/week/#{week_day.day}",+ title: week_day.name+ }.to_json+ expect(subject).to be_json_eql(expected_json).at_path('_links/self')+ end+ end+ end+ end++ describe 'caching' do+ it 'is based on the representer\'s json_cache_key' do+ allow(OpenProject::Cache)+ .to receive(:fetch)+ .and_call_original++ representer.to_json++ expect(OpenProject::Cache)+ .to have_received(:fetch)+ .with(representer.json_cache_key)+ end++ describe '#json_cache_key' do+ let!(:former_cache_key) { representer.json_cache_key }++ it 'includes the name of the representer class' do+ expect(representer.json_cache_key)+ .to include('API', 'V3', 'Days', 'WeekDayRepresenter')+ end++ it 'changes when the locale changes' do+ I18n.with_locale(:fr) do+ expect(representer.json_cache_key)+ .not_to eql former_cache_key+ end+ end++ it 'changes when the week_day is updated' do+ week_day.updated_at = 20.seconds.from_now++ expect(representer.json_cache_key)+ .not_to eql former_cache_key+ end+ end+ end+enddiff --git a/spec/models/week_day_spec.rb b/spec/models/week_day_spec.rbnew file mode 100644index 00000000000..a239501265c--- /dev/null+++ b/spec/models/week_day_spec.rb@@ -0,0 +1,14 @@+require 'rails_helper'++RSpec.describe WeekDay, type: :model do+ describe '#name' do+ it 'returns the translated week day name' do+ expect(described_class.create(day: 1).name).to eq('Monday')+ expect(described_class.create(day: 7).name).to eq('Sunday')+ I18n.with_locale(:de) do+ expect(described_class.create(day: 3).name).to eq('Mittwoch')+ expect(described_class.create(day: 4).name).to eq('Donnerstag')+ end+ end+ end+enddiff --git a/spec/requests/api/v3/days/week_spec.rb b/spec/requests/api/v3/days/week_spec.rbnew file mode 100644index 00000000000..82c6336922a--- /dev/null+++ b/spec/requests/api/v3/days/week_spec.rb@@ -0,0 +1,50 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.++require 'spec_helper'++describe ::API::V3::Days::WeekAPI,+ 'index',+ content_type: :json,+ type: :request do+ include API::V3::Utilities::PathHelper++ let(:parsed_response) { JSON.parse(last_response.body) }++ current_user { user }++ before do+ create(:week_days)+ get api_v3_paths.days_week+ end++ context 'for an admin user' do+ let(:user) { build(:admin) }++ it_behaves_like 'API V3 collection response', 7, 7, 'WeekDay'+ end+end