-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
235 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,9 @@ | ||
class Users::RegistrationsController < ApplicationController | ||
def create | ||
user_params = User::Params.to_register(params) | ||
|
||
user = User::Registration.new(user_params).create | ||
|
||
if user.persisted? | ||
render_json(201, user: user.as_json(only: [:id, :name, :token])) | ||
else | ||
render_json(422, user: user.errors.as_json) | ||
end | ||
rescue ActionController::ParameterMissing => e | ||
render_json(400, error: e.message) | ||
User::Register::Flow | ||
.call(params: params) | ||
.on_failure(:parameter_missing) { |error| render_json(400, error: error[:message]) } | ||
.on_failure(:invalid_user_params) { |user| render_json(422, user: user[:errors]) } | ||
.on_success { |result| render_json(201, user: result[:user]) } | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Todo::List::ActivateItem < Micro::Case | ||
attributes :user, :todo_id | ||
|
||
validates :user, type: User | ||
validates :todo_id, numericality: { only_integer: true } | ||
|
||
def call! | ||
todo = user.todos.find_by(id: todo_id&.strip) | ||
|
||
return Failure(:todo_not_found) unless todo | ||
|
||
todo.completed_at = nil unless todo.active? | ||
|
||
todo.save if todo.completed_at_changed? | ||
|
||
Success { { todo: Todo::Serialization.as_json(todo) } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Todo::List::AddItem < Micro::Case | ||
attributes :user, :params | ||
|
||
validates :user, type: User | ||
validates :params, type: ActionController::Parameters | ||
|
||
def call! | ||
todo_params = Todo::Params.to_save(params) | ||
|
||
todo = user.todos.create(todo_params) | ||
|
||
todo_as_json = Todo::Serialization.as_json(todo) | ||
|
||
return Success { { todo: todo_as_json } } if todo.persisted? | ||
|
||
Failure(:invalid_todo) { { errors: todo_as_json } } | ||
rescue ActionController::ParameterMissing => e | ||
Failure(:parameter_missing) { { message: e.message } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Todo::List::CompleteItem < Micro::Case | ||
attributes :user, :todo_id | ||
|
||
validates :user, type: User | ||
validates :todo_id, numericality: { only_integer: true } | ||
|
||
def call! | ||
todo = user.todos.find_by(id: todo_id&.strip) | ||
|
||
return Failure(:todo_not_found) unless todo | ||
|
||
todo.completed_at = Time.current unless todo.completed? | ||
|
||
todo.save if todo.completed_at_changed? | ||
|
||
Success { { todo: Todo::Serialization.as_json(todo) } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Todo::List::DeleteItem < Micro::Case | ||
attributes :user, :todo_id | ||
|
||
validates :user, type: User | ||
validates :todo_id, numericality: { only_integer: true } | ||
|
||
def call! | ||
todo = user.todos.find_by(id: todo_id&.strip) | ||
|
||
return Failure(:todo_not_found) unless todo | ||
|
||
todo.destroy | ||
|
||
Success { { todo: Todo::Serialization.as_json(todo) } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Todo::List::FetchItems < Micro::Case | ||
attributes :user, :params | ||
|
||
validates :user, type: User | ||
validates :params, type: ActionController::Parameters | ||
|
||
def call! | ||
todos = Todo.where_status(params[:status]).where(user_id: user.id) | ||
|
||
Success { { todos: Todo::Serialization.map_as_json(todos) } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Todo::List::UpdateItem < Micro::Case | ||
attributes :user, :todo_id, :params | ||
|
||
validates :user, type: User | ||
validates :params, type: ActionController::Parameters | ||
validates :todo_id, numericality: { only_integer: true } | ||
|
||
def call! | ||
todo_params = Todo::Params.to_save(params) | ||
|
||
todo = user.todos.find_by(id: todo_id&.strip) | ||
|
||
return Failure(:todo_not_found) unless todo | ||
|
||
todo.update(todo_params) | ||
|
||
todo_as_json = Todo::Serialization.as_json(todo) | ||
|
||
return Success { { todo: todo_as_json } } if todo.valid? | ||
|
||
Failure(:invalid_todo) { { errors: todo_as_json } } | ||
rescue ActionController::ParameterMissing => e | ||
Failure(:parameter_missing) { { message: e.message } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class User | ||
module Register | ||
class Flow < Micro::Case | ||
flow Step::NormalizeParams, | ||
Step::ValidatePassword, | ||
Step::CreateRecord, | ||
Step::SerializeAsJson | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class User::Register::Step::CreateRecord < Micro::Case | ||
attributes :name, :password, :password_confirmation | ||
|
||
def call! | ||
user = User.new(user_attributes).tap(&:save) | ||
|
||
return Success { { user: user } } if user.persisted? | ||
|
||
Failure(:invalid_user_params) { { errors: user.errors.as_json } } | ||
end | ||
|
||
private | ||
|
||
def user_attributes | ||
{ name: name, password_digest: password.digest, token: SecureRandom.uuid } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class User::Register::Step::NormalizeParams < Micro::Case | ||
attribute :params | ||
|
||
validates! :params, type: ActionController::Parameters | ||
|
||
def call! | ||
user_params = User::Params.to_register(params) | ||
|
||
Success do | ||
{ | ||
name: user_params[:name].try(:squish), | ||
password: User::Password.new(user_params[:password]), | ||
password_confirmation: User::Password.new(user_params[:password_confirmation]) | ||
} | ||
end | ||
rescue ActionController::ParameterMissing => e | ||
Failure(:parameter_missing) { { message: e.message } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class User::Register::Step::SerializeAsJson < Micro::Case | ||
attribute :user | ||
|
||
validates! :user, type: User | ||
|
||
def call! | ||
Success do | ||
{ user: user.as_json(only: [:id, :name, :token]) } | ||
end | ||
end | ||
end |
Oops, something went wrong.