From ab543c7dbe2223e0e98bed036c5628535418eb59 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:23:52 +0900 Subject: [PATCH 01/14] add status column to contribution table --- .../20181213200431_add_status_column_to_contribution.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20181213200431_add_status_column_to_contribution.rb diff --git a/db/migrate/20181213200431_add_status_column_to_contribution.rb b/db/migrate/20181213200431_add_status_column_to_contribution.rb new file mode 100644 index 0000000..86f6a89 --- /dev/null +++ b/db/migrate/20181213200431_add_status_column_to_contribution.rb @@ -0,0 +1,5 @@ +class AddStatusColumnToContribution < ActiveRecord::Migration[5.1] + def change + add_column :contributions, :status, :boolean, default: true, null: false + end +end From 13aed8ec8f986e17614e5ad24298b56037a5b272 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:24:48 +0900 Subject: [PATCH 02/14] rename is_final? column in contribution table into finality --- .../20181213201322_rename_is_final_column_in_contribution.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20181213201322_rename_is_final_column_in_contribution.rb diff --git a/db/migrate/20181213201322_rename_is_final_column_in_contribution.rb b/db/migrate/20181213201322_rename_is_final_column_in_contribution.rb new file mode 100644 index 0000000..ef150f0 --- /dev/null +++ b/db/migrate/20181213201322_rename_is_final_column_in_contribution.rb @@ -0,0 +1,5 @@ +class RenameIsFinalColumnInContribution < ActiveRecord::Migration[5.1] + def change + rename_column :contributions, :is_finish?, :finality + end +end From 3b096d57d5dd7fa765eb56bd559d7e33a22c0ba0 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:25:30 +0900 Subject: [PATCH 03/14] rename finish_time column in contribution table into finished_at --- ...181213201644_rename_finish_time_column_in_contribution.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20181213201644_rename_finish_time_column_in_contribution.rb diff --git a/db/migrate/20181213201644_rename_finish_time_column_in_contribution.rb b/db/migrate/20181213201644_rename_finish_time_column_in_contribution.rb new file mode 100644 index 0000000..c3bfad8 --- /dev/null +++ b/db/migrate/20181213201644_rename_finish_time_column_in_contribution.rb @@ -0,0 +1,5 @@ +class RenameFinishTimeColumnInContribution < ActiveRecord::Migration[5.1] + def change + rename_column :contributions, :finish_time, :finished_at + end +end From f209fa1224bdc4324ecd4b8d7c6c0afe8285da0a Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:26:05 +0900 Subject: [PATCH 04/14] add nullability to columns in contribution table --- ...213202158_add_nullability_to_columns_of_contribution.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 db/migrate/20181213202158_add_nullability_to_columns_of_contribution.rb diff --git a/db/migrate/20181213202158_add_nullability_to_columns_of_contribution.rb b/db/migrate/20181213202158_add_nullability_to_columns_of_contribution.rb new file mode 100644 index 0000000..9e35326 --- /dev/null +++ b/db/migrate/20181213202158_add_nullability_to_columns_of_contribution.rb @@ -0,0 +1,7 @@ +class AddNullabilityToColumnsOfContribution < ActiveRecord::Migration[5.1] + def change + change_column_null :contributions, :user_id, false + change_column_null :contributions, :task_id, false + change_column_null :contributions, :finality, false + end +end From 6ae3e2a1b6e1e65617b5f89e3a98b426a35c5819 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:27:15 +0900 Subject: [PATCH 05/14] set default of finality in contribution table as false --- ...20181213203003_set_default_of_finality_in_contribution.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20181213203003_set_default_of_finality_in_contribution.rb diff --git a/db/migrate/20181213203003_set_default_of_finality_in_contribution.rb b/db/migrate/20181213203003_set_default_of_finality_in_contribution.rb new file mode 100644 index 0000000..fd7ff60 --- /dev/null +++ b/db/migrate/20181213203003_set_default_of_finality_in_contribution.rb @@ -0,0 +1,5 @@ +class SetDefaultOfFinalityInContribution < ActiveRecord::Migration[5.1] + def change + change_column_default :contributions, :finality, false + end +end From 4e227f0e672b146e49d23ae59003eb1c7098900b Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:28:05 +0900 Subject: [PATCH 06/14] update schema.db --- db/schema.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 2235be8..cc8e21f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,25 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20181209235432) do - +ActiveRecord::Schema.define(version: 20181213203003) do + create_table "audio_tests", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end + create_table "contributions", force: :cascade do |t| + t.integer "user_id", null: false + t.integer "task_id", null: false + t.datetime "finished_at" + t.boolean "finality", default: false, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "status", default: true, null: false + t.index ["task_id"], name: "index_contributions_on_task_id" + t.index ["user_id"], name: "index_contributions_on_user_id" + end + create_table "groups", force: :cascade do |t| t.integer "parent_id" t.string "name", null: false From 11793739e81a14304d10098d4744867bbade2320 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:30:58 +0900 Subject: [PATCH 07/14] set finality and status as enum and add uniqueness validation to set of user_id, task_id and status when creating new contribution record --- app/models/contribution.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/contribution.rb b/app/models/contribution.rb index 979397f..a3056c7 100644 --- a/app/models/contribution.rb +++ b/app/models/contribution.rb @@ -1,4 +1,9 @@ class Contribution < ApplicationRecord belongs_to :user belongs_to :task + + enum finality: { final: true, not_final: false } + enum status: { active: true, inactive: false } + + validates :status, uniqueness: { scope: [:user_id, :task_id], on: :create } end From abc29364ea44b15bb2ecd6b0613dcda7ff084f25 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:40:49 +0900 Subject: [PATCH 08/14] update contribution query to following model's changes --- app/graphql/types/contribution_type.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/graphql/types/contribution_type.rb b/app/graphql/types/contribution_type.rb index 8cef291..c618c08 100644 --- a/app/graphql/types/contribution_type.rb +++ b/app/graphql/types/contribution_type.rb @@ -3,6 +3,15 @@ class Types::ContributionType < Types::BaseObject field :user_id, ID, 'ユーザーID', null: false field :task_id, ID, 'タスクID', null: false field :created_at, Types::MomentType, '開始時刻', null: false - field :finish_time, Types::MomentType, '終了時刻', null: true - field :is_finish?, Boolean, 'このコントリビューションでタスクを終了したか否か', null: true + field :finished_at, Types::MomentType, '終了時刻', null: true + field :status, Boolean, null: false + field :finality, Boolean, 'このコントリビューションでタスクを終了したか否か', null: false + + def status + Contribution.statuses[object.status] + end + + def finality + Contribution.finality[object.finality] + end end \ No newline at end of file From dd3d5bbd8eccbef7e83f84232ceb5401225c12b3 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 06:58:52 +0900 Subject: [PATCH 09/14] update fields' descriptions --- app/graphql/types/contribution_type.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/graphql/types/contribution_type.rb b/app/graphql/types/contribution_type.rb index c618c08..6aa6713 100644 --- a/app/graphql/types/contribution_type.rb +++ b/app/graphql/types/contribution_type.rb @@ -4,14 +4,14 @@ class Types::ContributionType < Types::BaseObject field :task_id, ID, 'タスクID', null: false field :created_at, Types::MomentType, '開始時刻', null: false field :finished_at, Types::MomentType, '終了時刻', null: true - field :status, Boolean, null: false - field :finality, Boolean, 'このコントリビューションでタスクを終了したか否か', null: false + field :status, Boolean, 'コントリビューションの状態(true: active, false: inactive)', null: false + field :finality, Boolean, 'このコントリビューションでタスクを終了したか否か(true: final, false: not_final)', null: false def status Contribution.statuses[object.status] end def finality - Contribution.finality[object.finality] + Contribution.finalities[object.finality] end end \ No newline at end of file From b74b00e529e906524641aeadc722cd94666e00a4 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 07:07:34 +0900 Subject: [PATCH 10/14] change validation trigger from create to save --- app/models/contribution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/contribution.rb b/app/models/contribution.rb index a3056c7..2c9f341 100644 --- a/app/models/contribution.rb +++ b/app/models/contribution.rb @@ -5,5 +5,5 @@ class Contribution < ApplicationRecord enum finality: { final: true, not_final: false } enum status: { active: true, inactive: false } - validates :status, uniqueness: { scope: [:user_id, :task_id], on: :create } + validates :status, uniqueness: { scope: [:user_id, :task_id], on: :save } end From 8404c46abda43a386d71e5b25a22cc17033f2297 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 07:33:36 +0900 Subject: [PATCH 11/14] make validation triggerred only when new contribution is active --- app/models/contribution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/contribution.rb b/app/models/contribution.rb index 2c9f341..3e1b230 100644 --- a/app/models/contribution.rb +++ b/app/models/contribution.rb @@ -5,5 +5,5 @@ class Contribution < ApplicationRecord enum finality: { final: true, not_final: false } enum status: { active: true, inactive: false } - validates :status, uniqueness: { scope: [:user_id, :task_id], on: :save } + validates :status, uniqueness: { scope: [:user_id, :task_id], if: 'active?' } end From b15220d0ac27f1424eb8bf45e21bc2c8f43d4502 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 08:39:01 +0900 Subject: [PATCH 12/14] rewrite start contribution mutation to match strict validation --- app/graphql/mutations/start_contribution.rb | 23 ++++++++++++++++++++ app/graphql/mutations/start_task.rb | 24 --------------------- 2 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 app/graphql/mutations/start_contribution.rb delete mode 100644 app/graphql/mutations/start_task.rb diff --git a/app/graphql/mutations/start_contribution.rb b/app/graphql/mutations/start_contribution.rb new file mode 100644 index 0000000..3c85213 --- /dev/null +++ b/app/graphql/mutations/start_contribution.rb @@ -0,0 +1,23 @@ +class Mutations::StartContribution < GraphQL::Schema::Mutation + graphql_name 'StartContributionMutation' + null false + + argument :task_id, ID, required: true + + field :task, Types::TaskType, null: true + field :contribution, Types::ContributionType, null: true + field :errors, [String], null: false + + def resolve(task_id:) + task = Task.find(task_id) + contribution = Contribution.new(:user_id => 2, :task_id => task.id) + if contribution.save && task.doing! + { contribution: contribution, task: task, errors: [] } + else + { + contribution: contribution, + errors: contribution.errors.full_messages + } + end + end +end \ No newline at end of file diff --git a/app/graphql/mutations/start_task.rb b/app/graphql/mutations/start_task.rb deleted file mode 100644 index 6e98c4d..0000000 --- a/app/graphql/mutations/start_task.rb +++ /dev/null @@ -1,24 +0,0 @@ -class Mutations::StartTask < GraphQL::Schema::Mutation - graphql_name 'StartTaskMutation' - null false - - argument :task_id, ID, required: true - - field :contribution, Types::ContributionType, null: false - field :errors, [String], null: false - - # 引数にargumentが入ってくるのはqueryと同じ挙動 - def resolve(task_id:) - task = Task.find(task_id) - task.status = 2 - contribution = Contribution.create(:user_id => context[:current_user].id) - if contribution.save - { contribution: contribution, errors: [] } - else - { - contribution: contribution, - errors: contribution.errors.full_messages - } - end - end -end \ No newline at end of file From b7fe809da5e0fd9b3c803cec3d136ac4e8f751de Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 08:40:47 +0900 Subject: [PATCH 13/14] rewrite finish contribution mutation --- app/graphql/mutations/finish_contribution.rb | 36 ++++++++++++ app/graphql/mutations/finish_task_mutation.rb | 55 ------------------- app/graphql/types/mutation_type.rb | 3 +- 3 files changed, 38 insertions(+), 56 deletions(-) create mode 100644 app/graphql/mutations/finish_contribution.rb delete mode 100644 app/graphql/mutations/finish_task_mutation.rb diff --git a/app/graphql/mutations/finish_contribution.rb b/app/graphql/mutations/finish_contribution.rb new file mode 100644 index 0000000..8750a62 --- /dev/null +++ b/app/graphql/mutations/finish_contribution.rb @@ -0,0 +1,36 @@ +class Mutations::FinishContribution < GraphQL::Schema::Mutation + graphql_name 'FinishContributionMutation' + null false + + argument :contribution_id, ID, required: true + argument :finality, Boolean, required: true + + field :task, Types::TaskType, null: true + field :contribution, Types::ContributionType, null: true + field :errors, [String], null: false + + def resolve(contribution_id:, finality:) + contribution = Contribution.find(contribution_id) + task = Task.find(contribution.task_id) + + contribution.status = 'inactive' + contribution.finished_at = DateTime.now + + if finality + contribution.finality = 'final' + task.status = 'done' + else + contribution.finality = 'not_final' + task.status = 'todo' + end + + if contribution.save && task.save + { contribution: contribution, task: task, errors: [] } + else + { + contribution: contribution, + errors: contribution.errors.full_messages + } + end + end +end \ No newline at end of file diff --git a/app/graphql/mutations/finish_task_mutation.rb b/app/graphql/mutations/finish_task_mutation.rb deleted file mode 100644 index e4e47a0..0000000 --- a/app/graphql/mutations/finish_task_mutation.rb +++ /dev/null @@ -1,55 +0,0 @@ -Mutations::FinishTaskMutation = GraphQL::Relay::Mutation.define do - name "FinishTaskMutation" - - input_field :task_id, !types.ID - input_field :is_finish?, !types.Boolean - input_field :contribution_id, !types.ID - - return_field :task, !Types::TaskType - - resolve ->(obj, args, ctx) { - begin - task = Task.find(inputs.task_id) - contribution = Contribution.find(inputs.contribution_id) - - if inputs.is_finish? - task.status = 1 - contribution.finish_time = DateTime.now - contribution.is_finish? = true - else - task.status = 3 - contribution.finish_time = DateTime.now - contribution.is_finish? = false - end - - rescue => e - return GraphQL::ExecutionError.new(e.message) - end - - { task: task } - - } -end - - -Mutations::StartTaskMutation = GraphQL::Relay::Mutation.define do - name "StartTaskMutation" - - input_field :task_id, !types.ID - - return_field :contribution, !Types::ContributionType - - resolve ->(obj, args, ctx) { - begin - task = Task.find(inputs.task_id) - task.status = 2 - contribution = Contribution.create(:user_id => ctx[:current_user_id], :task_id => inputs.task_id) - contribution.save - rescue => e - return GraphQL::ExecutionError.new(e.message) - end - - { contribution: contribution } - - } -end \ No newline at end of file diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 0fa2f70..9be77a0 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -1,5 +1,6 @@ class Types::MutationType < GraphQL::Schema::Object - field :startTask, mutation: Mutations::StartTask + field :startContribution, mutation: Mutations::StartContribution + field :finishContribution, mutation: Mutations::FinishContribution field :createClap, mutation: Mutations::CreateClap # TODO: Remove me From a799590329b4c4bc3ef05a805ffb3cc3a67bec73 Mon Sep 17 00:00:00 2001 From: kunado Date: Fri, 14 Dec 2018 09:06:28 +0900 Subject: [PATCH 14/14] make user_id assigned to current_user's id --- app/graphql/mutations/start_contribution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/graphql/mutations/start_contribution.rb b/app/graphql/mutations/start_contribution.rb index 3c85213..7a7b036 100644 --- a/app/graphql/mutations/start_contribution.rb +++ b/app/graphql/mutations/start_contribution.rb @@ -10,7 +10,7 @@ class Mutations::StartContribution < GraphQL::Schema::Mutation def resolve(task_id:) task = Task.find(task_id) - contribution = Contribution.new(:user_id => 2, :task_id => task.id) + contribution = Contribution.new(:user_id => context[:current_user].id, :task_id => task.id) if contribution.save && task.doing! { contribution: contribution, task: task, errors: [] } else