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/mutations/start_contribution.rb b/app/graphql/mutations/start_contribution.rb new file mode 100644 index 0000000..7a7b036 --- /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 => context[:current_user].id, :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 diff --git a/app/graphql/types/contribution_type.rb b/app/graphql/types/contribution_type.rb index 8cef291..6aa6713 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, 'コントリビューションの状態(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.finalities[object.finality] + end 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 diff --git a/app/models/contribution.rb b/app/models/contribution.rb index 979397f..3e1b230 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], if: 'active?' } end 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 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 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 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 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 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