Skip to content

Commit

Permalink
Merge pull request #221 from enpitut2018/feature/update_contribution_…
Browse files Browse the repository at this point in the history
…mutations

Feature/update contribution mutations
  • Loading branch information
Ryoma-Suizu authored Dec 14, 2018
2 parents 0845ab0 + a799590 commit e61d37d
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 84 deletions.
36 changes: 36 additions & 0 deletions app/graphql/mutations/finish_contribution.rb
Original file line number Diff line number Diff line change
@@ -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
55 changes: 0 additions & 55 deletions app/graphql/mutations/finish_task_mutation.rb

This file was deleted.

23 changes: 23 additions & 0 deletions app/graphql/mutations/start_contribution.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 0 additions & 24 deletions app/graphql/mutations/start_task.rb

This file was deleted.

13 changes: 11 additions & 2 deletions app/graphql/types/contribution_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/models/contribution.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusColumnToContribution < ActiveRecord::Migration[5.1]
def change
add_column :contributions, :status, :boolean, default: true, null: false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameIsFinalColumnInContribution < ActiveRecord::Migration[5.1]
def change
rename_column :contributions, :is_finish?, :finality
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameFinishTimeColumnInContribution < ActiveRecord::Migration[5.1]
def change
rename_column :contributions, :finish_time, :finished_at
end
end
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SetDefaultOfFinalityInContribution < ActiveRecord::Migration[5.1]
def change
change_column_default :contributions, :finality, false
end
end
16 changes: 14 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e61d37d

Please sign in to comment.