Skip to content

Commit

Permalink
feat: ✨ List tasks (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmaji committed Jun 26, 2024
1 parent a2381b9 commit bcecf95
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/adapters/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def create
end
end

def index
posts = ListTasks.new(TaskRepository.new).execute
render json: posts
end

private

def task_params
Expand Down
12 changes: 12 additions & 0 deletions app/core/use_cases/tasks/list_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# List Tasks use case
class ListTasks
def initialize(repository)
@repository = repository
end

def execute
@repository.all
end
end
1 change: 1 addition & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
# Task model
class Task < ApplicationRecord
validates :title, presence: true
belongs_to :user
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

get 'me', to: 'auth#current_user'
post 'task', to: 'tasks#create'
get 'task', to: 'tasks#index'
end
7 changes: 4 additions & 3 deletions spec/factories/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

FactoryBot.define do
factory :task do
title { 'My Task' }
sequence(:title) { |n| "Task #{n}" }
description { 'Task description' }
status { 1 }
status { 0 }
association :user, factory: :user
end
end
end
4 changes: 2 additions & 2 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

FactoryBot.define do
factory :user do
email { 'test@example.com' }
sequence(:email) { |n| Faker::Internet.email.gsub('@', "-#{n}@") }
password { 'password' }
password_confirmation { 'password' }
first_name { 'L' }
last_name { 'M' }
end
end
end
60 changes: 59 additions & 1 deletion spec/requests/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,63 @@
end
end
end

get 'Get tasks' do
tags 'Tasks'
consumes 'application/json'
produces 'application/json'
security [bearer_auth: []]

response '200', 'list tasks' do
let(:token) { token_scopes('public manage') }

let(:Authorization) { "Bearer #{token.token}" }
before do
create_list(:task, 5)
end

schema type: :array,
items: {
type: :object,
properties: {
id: { type: :integer },
title: { type: :string },
description: { type: :string },
user_id: { type: :integer },
created_at: { type: :string, format: :datetime },
updated_at: { type: :string, format: :datetime },
status: { type: :integer }
}
}

run_test! do
expect(JSON.parse(response.body).size).to eq(5)
end
end

response '200', 'zero records' do
let(:token) { token_scopes('public manage') }
let(:Authorization) { "Bearer #{token.token}" }

schema type: :array,
items: {
type: :object,
properties: {
id: { type: :integer },
title: { type: :string },
description: { type: :string },
user_id: { type: :integer },
created_at: { type: :string, format: :datetime },
updated_at: { type: :string, format: :datetime },
status: { type: :integer }
}
}

let(:posts) { create_list(:task, 5) }
run_test! do
expect(JSON.parse(response.body).size).to eq(0)
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/use_cases/tasks/list_tasks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ListTasks do
let(:list_tasks) { described_class.new(repository) }

let(:repository) { TaskRepository.new }

describe '#execute' do
it 'returns tasks from the repository' do
tasks = create_list(:task, 3)

expect(list_tasks.execute).to match_array(tasks)
end

it 'returns all tasks from the repository' do
create_list(:task, 3)

expect(list_tasks.execute.size).to eq(3)
end
end
end

0 comments on commit bcecf95

Please sign in to comment.