Skip to content

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

add first implementation of /api/v3/days

Christophe Bliard084df4233471
diff --git a/app/models/day.rb b/app/models/day.rb
index 78807171cb6..5c98e020806 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -26,8 +26,43 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Day
- include ActiveModel::Model
+class Day < ApplicationRecord
+ include Tableless
- attr_accessor :date, :name, :working
+ belongs_to :week_day,
+ inverse_of: false,
+ class_name: 'WeekDay',
+ foreign_key: :day_of_week,
+ primary_key: :day
+
+ attribute :date, :date, default: nil
+ attribute :day_of_week, :integer, default: nil
+ attribute :working, :boolean, default: 't'
+
+ delegate :name, to: :week_day
+
+ def self.default
+ today = Time.zone.today
+ from = today.at_beginning_of_month
+ to = today.next_month.at_end_of_month
+
+ days_sql = <<~SQL.squish
+ (
+ SELECT
+ date_trunc('day', dd)::date date,
+ extract(isodow from dd) day_of_week,
+ week_days.working
+ FROM generate_series
+ ( '#{from}'::timestamp,
+ '#{to}'::timestamp,
+ '1 day'::interval) dd
+ LEFT JOIN week_days
+ ON extract(isodow from dd) = week_days.day
+ ORDER BY date
+ ) days
+ SQL
+
+ select('days.*')
+ .from(days_sql)
+ end
end
diff --git a/app/models/queries/days/day_query.rb b/app/models/queries/days/day_query.rb
new file mode 100644
index 00000000000..2b8eba8227a
--- /dev/null
+++ b/app/models/queries/days/day_query.rb
@@ -0,0 +1,41 @@
+#-- 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::Days::DayQuery < Queries::BaseQuery
+ def self.model
+ Day
+ end
+
+ def default_scope
+ Day.default
+ end
+
+ def results
+ super.reorder('date ASC')
+ end
+end
diff --git a/lib/api/v3/days/days_api.rb b/lib/api/v3/days/days_api.rb
index cfbcf40ca7c..81fcef3fdf3 100644
--- a/lib/api/v3/days/days_api.rb
+++ b/lib/api/v3/days/days_api.rb
@@ -31,8 +31,14 @@ module API::V3::Days
helpers ::API::Utilities::UrlPropsParsingHelper
resources :days do
- mount WeekAPI
mount NonWorkingDaysAPI
+ mount WeekAPI
+
+ get &::API::V3::Utilities::Endpoints::Index.new(
+ model: Day,
+ render_representer: DayCollectionRepresenter,
+ self_path: -> { api_v3_paths.days }
+ ).mount
end
end
end
diff --git a/spec/factories/day_factory.rb b/spec/factories/day_factory.rb
index 706056ba731..38bb2fe93a4 100644
--- a/spec/factories/day_factory.rb
+++ b/spec/factories/day_factory.rb
@@ -31,7 +31,7 @@ FactoryBot.define do
sequence :date do |n|
(1.year.ago + n.days).to_date
end
- name { WeekDay.new(day: date.wday).name }
+ day_of_week { date.wday }
working { date.wday < 6 }
end
end
diff --git a/spec/factories/week_day_factory.rb b/spec/factories/week_day_factory.rb
index 10974857429..cd2229e5b70 100644
--- a/spec/factories/week_day_factory.rb
+++ b/spec/factories/week_day_factory.rb
@@ -36,6 +36,10 @@ FactoryBot.define do
instance.attributes = WeekDay.find_or_create_by(instance.attributes.slice("day", "working")).attributes
instance.instance_variable_set('@new_record', false)
end
+
+ trait :tuesday do
+ day { 2 }
+ end
end
# Factory to create all 7 week days at once
diff --git a/spec/lib/api/v3/days/day_collection_representer_spec.rb b/spec/lib/api/v3/days/day_collection_representer_spec.rb
index 22773be52c4..8c3e5cf7095 100644
--- a/spec/lib/api/v3/days/day_collection_representer_spec.rb
+++ b/spec/lib/api/v3/days/day_collection_representer_spec.rb
@@ -29,6 +29,7 @@
require 'spec_helper'
describe ::API::V3::Days::DayCollectionRepresenter do
+ let!(:week_days) { create(:week_days) }
let(:days) do
[
build(:day, date: Date.new(2022, 12, 27)),
diff --git a/spec/lib/api/v3/days/day_representer_spec.rb b/spec/lib/api/v3/days/day_representer_spec.rb
index cdce648808e..689254913cc 100644
--- a/spec/lib/api/v3/days/day_representer_spec.rb
+++ b/spec/lib/api/v3/days/day_representer_spec.rb
@@ -30,7 +30,7 @@ require 'spec_helper'
describe ::API::V3::Days::DayRepresenter do
let(:day) do
- build(:day, date: Date.new(2022, 12, 27), name: 'Tuesday', working: true)
+ build(:day, date: Date.new(2022, 12, 27), week_day: create(:week_day, :tuesday), working: true)
end
let(:current_user) { instance_double(User, name: 'current_user') }
let(:representer) { described_class.new(day, current_user:) }
diff --git a/spec/requests/api/v3/days/day_spec.rb b/spec/requests/api/v3/days/day_spec.rb
new file mode 100644
index 00000000000..869c962ee6e
--- /dev/null
+++ b/spec/requests/api/v3/days/day_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::DaysAPI,
+ 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
+ end
+
+ context 'for an admin user' do
+ let(:user) { build(:admin) }
+
+ nb_days = Time.zone.today.end_of_month.day + Time.zone.today.next_month.end_of_month.day
+ it_behaves_like 'API V3 collection response', nb_days, nb_days, 'Day'
+ end
+end