-
Notifications
You must be signed in to change notification settings - Fork 0
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
10 changed files
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
# Tasks controller | ||
class TasksController < ActionController::API | ||
def create | ||
create_task = CreateTask.new(TaskRepository.new) | ||
task = create_task.execute(task_params) | ||
render json: task | ||
end | ||
|
||
private | ||
|
||
def task_params | ||
params.require(:task).permit(:title, :description, :author_id) | ||
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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# Outside clean arch, rails specific imple | ||
class TaskRepository | ||
def save(task_params) | ||
Task.create(task_params) | ||
end | ||
|
||
def all | ||
Task.all | ||
end | ||
|
||
def find(id) | ||
Task.find_by(id:) | ||
end | ||
|
||
def update(id, task_params) | ||
task = find(id) | ||
task&.update(task_params) | ||
task | ||
end | ||
|
||
def destroy(id) | ||
task = find(id) | ||
task&.destroy | ||
task | ||
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 @@ | ||
# frozen_string_literal: true | ||
|
||
module Entities | ||
# Task entity | ||
class Task | ||
attr_accessor :id, :title, :description, :author_id, :status | ||
|
||
def initialize(attributes = {}) | ||
@id = attributes[:id] | ||
@title = attributes[:title] | ||
@description = attributes[:description] | ||
@author_id = attributes[:author_id] | ||
@status = attributes[:status] | ||
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
# Create Task use case | ||
class CreateTask | ||
def initialize(repository) | ||
@repository = repository | ||
end | ||
|
||
def execute(task_params) | ||
task = Entities::Task.new(task_params) | ||
@repository.save(title: task.title, description: task.description, status: 0) | ||
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
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 @@ | ||
# frozen_string_literal: true | ||
|
||
# Rails.application.config.eager_load = true | ||
|
||
# shapes = "#{Rails.root.join('app/core')}" | ||
# Rails.autoloaders.main.collapse(shapes) # Not a namespace. | ||
|
||
# unless Rails.application.config.eager_load | ||
# Rails.application.config.to_prepare do | ||
# Rails.autoloaders.main.eager_load_dir(shapes) | ||
# 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 |
---|---|---|
|
@@ -15,4 +15,5 @@ | |
end | ||
|
||
get 'me', to: 'auth#current_user' | ||
post 'task', to: 'tasks#create' | ||
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,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Tasks", type: :request do | ||
describe "GET /index" do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
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,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Tasks", type: :request do | ||
describe "GET /index" do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
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 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe CreateTask, type: :use_case do | ||
let(:task_repository) { instance_double(TaskRepository) } | ||
let(:user) { create(:user) } | ||
let(:create_task) { described_class.new(task_repository) } | ||
let(:task_params) { { title: 'Sample Todo', description: 'This is a sample content.' } } | ||
let(:model_task) { Task.new(task_params) } | ||
|
||
it 'creates a task' do | ||
task = Task.new(task_params) | ||
allow(task_repository).to receive(:save).with(description: task[:description], status: 0, | ||
title: task[:title]).and_return(model_task) | ||
|
||
result = create_task.execute(task_params) | ||
expect(result).to be_a(Task) | ||
end | ||
end |