Skip to content

Commit

Permalink
Merge branch 'fs:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
stepantishhen authored Feb 12, 2024
2 parents 23030e1 + 5b8e28b commit 64f5ea7
Show file tree
Hide file tree
Showing 33 changed files with 262 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Ignore bundler config.
/.bundle
/vendor

# Ignore all logfiles and tempfiles.
/log/*
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "2.7.4"

gem "action_policy"
gem "action_policy-graphql"
gem "active_model_serializers"
gem "enumerize"
gem "graphql"
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ GEM
specs:
action_policy (0.6.7)
ruby-next-core (>= 0.14.0)
action_policy-graphql (0.5.3)
action_policy (>= 0.5.0)
graphql (>= 1.9.3)
ruby-next-core (>= 0.10.0)
actioncable (6.1.7.6)
actionpack (= 6.1.7.6)
activesupport (= 6.1.7.6)
Expand Down Expand Up @@ -360,6 +364,7 @@ PLATFORMS

DEPENDENCIES
action_policy
action_policy-graphql
active_model_serializers
bcrypt (~> 3.1.7)
bootsnap (>= 1.4.4)
Expand Down
1 change: 1 addition & 0 deletions app/graphql/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Mutations
class BaseMutation < GraphQL::Schema::Mutation
include GraphqlErrors
include ActionPolicy::GraphQL::Behaviour

argument_class Types::BaseArgument
field_class Types::BaseField
Expand Down
18 changes: 18 additions & 0 deletions app/graphql/mutations/create_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Mutations
class CreateTask < BaseMutation
argument :input, Types::Inputs::CreateTaskInput, required: true

type Types::Payloads::TaskPayload

def resolve(input:)
input_params = input.to_h

result = Tasks::Create.call(
project: ::Project.find_by(id: input_params.delete(:project_id)),
task_params: input_params
)

result.to_h.merge(errors: formatted_errors(result.task))
end
end
end
15 changes: 15 additions & 0 deletions app/graphql/mutations/destroy_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Mutations
class DestroyComment < BaseMutation
argument :id, ID, required: true

type Types::Payloads::CommentPayload

def resolve(id:)
comment = Comment.find(id)

result = Comments::Destroy.call(comment: comment)

result.to_h.merge(errors: formatted_errors(result.comment))
end
end
end
14 changes: 14 additions & 0 deletions app/graphql/mutations/destroy_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Mutations
class DestroyProject < BaseMutation
argument :id, ID, required: true

type Types::Payloads::ProjectPayload

def resolve(id:)
project = Project.find(id)

result = ::Projects::Destroy.call(project: project)
result.to_h.merge(errors: formatted_errors(result.project))
end
end
end
14 changes: 14 additions & 0 deletions app/graphql/mutations/destroy_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Mutations
class DestroyTask < BaseMutation
argument :id, ID, required: true

type Types::Payloads::TaskPayload

def resolve(id:)
task = Task.find(id)

result = ::Tasks::Destroy.call(task: task, user: current_user)
result.to_h.merge(errors: formatted_errors(result.task))
end
end
end
16 changes: 16 additions & 0 deletions app/graphql/mutations/destroy_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Mutations
class DestroyUser < BaseMutation
argument :id, ID, required: true

type Types::Payloads::UserPayload

def resolve(**options)
user = ::User.find(options[:id])
authorize! user, to: :destroy?

result = Users::Destroy.call(user: user)

result.to_h.merge(errors: formatted_errors(result.user))
end
end
end
18 changes: 18 additions & 0 deletions app/graphql/mutations/update_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Mutations
class UpdateComment < BaseMutation
argument :input, Types::Inputs::UpdateCommentInput, required: true

type Types::Payloads::CommentPayload

def resolve(input:)
input_params = input.to_h

result = Comments::Update.call(
comment: Comment.find(input_params.delete(:id)),
comment_params: input_params
)

result.to_h.merge(errors: formatted_errors(result.comment))
end
end
end
18 changes: 18 additions & 0 deletions app/graphql/mutations/update_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Mutations
class UpdateTask < BaseMutation
argument :input, Types::Inputs::UpdateTaskInput, required: true

type Types::Payloads::TaskPayload

def resolve(input:)
input_hash = input.to_h

result = ::Tasks::Update.call(
task: Task.find_by(id: input_hash.delete(:id)),
task_params: input_hash, user: current_user
)

result.to_h.merge(errors: formatted_errors(result.task))
end
end
end
13 changes: 13 additions & 0 deletions app/graphql/mutations/update_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Mutations
class UpdateUser < BaseMutation
argument :input, Types::Inputs::UpdateUserInput

type Types::Payloads::UpdateUserPayload

def resolve(input:)
result = Users::Update.call(user_params: input.to_h, user: current_user)

result.to_h.merge(errors: formatted_errors(result.user))
end
end
end
10 changes: 10 additions & 0 deletions app/graphql/resolvers/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Resolvers
class Comment < Resolvers::Base
argument :id, ID, required: true
type Types::CommentType, null: true

def resolve(**args)
::Comment.find_by(id: args[:id])
end
end
end
9 changes: 9 additions & 0 deletions app/graphql/resolvers/comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Resolvers
class Comments < Resolvers::Base
type [Types::CommentType], null: true

def resolve(**_args)
::Comment.all
end
end
end
3 changes: 2 additions & 1 deletion app/graphql/types/comment_type.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Types
class CommentType < Types::BaseObject
field :id, ID, null: false
field :content, String
field :content, String, null: false
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :task, TaskType, null: false
field :user_id, Integer, null: false
field :user, UserType, null: false
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/inputs/create_task_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Types
module Inputs
class CreateTaskInput < Types::BaseInputObject
argument :project_id, ID, required: true
argument :name, String, required: true
argument :description, String, required: true
argument :status, TaskStatusType, required: true
argument :deadline_at, GraphQL::Types::ISO8601DateTime, required: true
end
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/inputs/update_comment_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Types
module Inputs
class UpdateCommentInput < Types::BaseInputObject
argument :id, ID, required: true
argument :content, String, required: false
end
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/inputs/update_task_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Types
module Inputs
class UpdateTaskInput < Types::BaseInputObject
argument :id, ID, required: true
argument :name, String, required: true
argument :description, String, required: false
argument :status, TaskStatusType, required: false
argument :deadline_at, GraphQL::Types::ISO8601DateTime, required: false
end
end
end
10 changes: 10 additions & 0 deletions app/graphql/types/inputs/update_user_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Types
module Inputs
class UpdateUserInput < Types::BaseInputObject
argument :first_name, String, required: false
argument :last_name, String, required: false
argument :email, String, required: false
argument :role, UserRoleType, required: false
end
end
end
13 changes: 11 additions & 2 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# frozen_string_literal: true

module Types
class MutationType < Types::BaseObject
field :sign_up, mutation: Mutations::SignUp
field :sign_in, mutation: Mutations::SignIn

field :create_project, mutation: Mutations::CreateProject
field :update_project, mutation: Mutations::UpdateProject
field :destroy_project, mutation: Mutations::DestroyProject

field :create_task, mutation: Mutations::CreateTask
field :update_task, mutation: Mutations::UpdateTask
field :destroy_task, mutation: Mutations::DestroyTask

field :create_comment, mutation: Mutations::CreateComment
field :update_comment, mutation: Mutations::UpdateComment
field :destroy_comment, mutation: Mutations::DestroyComment

field :update_user, mutation: Mutations::UpdateUser
field :destroy_user, mutation: Mutations::DestroyUser
end
end
9 changes: 0 additions & 9 deletions app/graphql/types/payloads/current_user_type.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/graphql/types/payloads/sign_up_payload.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Types
module Payloads
class SignUpPayload < Types::BaseObject
field :current_user, CurrentUserType, null: false, method: :user
field :user, UserType, null: false
field :errors, [Types::UserError], null: true
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/graphql/types/payloads/task_payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Types
module Payloads
class TaskPayload < Types::BaseObject
field :task, TaskType, null: true
field :errors, [Types::UserError], null: true
end
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/payloads/update_user_payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Types
module Payloads
class UpdateUserPayload < Types::BaseObject
field :user, UserType, null: false
field :errors, [Types::UserError], null: false
end
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/payloads/user_payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Types
module Payloads
class UserPayload < Types::BaseObject
field :user, UserType, null: true
field :errors, [Types::UserError], null: true
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ class QueryType < Types::BaseObject
field :projects, resolver: Resolvers::Projects
field :task, resolver: Resolvers::Task
field :tasks, resolver: Resolvers::Tasks
field :comment, resolver: Resolvers::Comment
field :comments, resolver: Resolvers::Comments
end
end
1 change: 0 additions & 1 deletion app/graphql/types/user_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class UserType < Types::BaseObject
field :first_name, String
field :last_name, String
field :email, String, null: false
field :password_digest, String
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :role, UserRoleType, null: false
Expand Down
11 changes: 11 additions & 0 deletions app/interactors/users/destroy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Users
class Destroy
include Interactor

delegate :user, to: :context

def call
user.destroy
end
end
end
14 changes: 5 additions & 9 deletions app/interactors/users/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ module Users
class Save
include Interactor

delegate :user_params, to: :context
delegate :user, :user_params, to: :context

def call
context.user = user

context.fail! unless user.update(user_params)
before do
context.user ||= User.new
end

private

def user
@user ||= context.user || User.new
def call
context.fail!(error: "Invalid data") unless user.update(user_params)
end
end
end
7 changes: 7 additions & 0 deletions app/interactors/users/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Users
class Update
include Interactor::Organizer

organize Users::Save
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class User < ApplicationRecord
has_many :projects, through: :project_memberships
has_many :comments

validates :password, length: { minimum: 6 }
validates :password, length: { minimum: 6 }, allow_blank: true
validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX }
validates :first_name, :last_name, presence: true

Expand Down
Loading

0 comments on commit 64f5ea7

Please sign in to comment.