Skip to content

Commit

Permalink
feat: ✨ associate task with user_id
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmaji committed Jun 23, 2024
1 parent 42ca371 commit e7d2079
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 22 deletions.
8 changes: 5 additions & 3 deletions app/adapters/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

# Tasks controller
class TasksController < ActionController::API
before_action :doorkeeper_authorize!

def create
create_task = CreateTask.new(TaskRepository.new)
task = create_task.execute(task_params)
render json: task
task = create_task.execute(task_params, doorkeeper_token.resource_owner_id)
render json: task, status: :created
end

private

def task_params
params.require(:task).permit(:title, :description, :author_id)
params.require(:task).permit(:title, :description, :user_id)
end
end
4 changes: 2 additions & 2 deletions app/core/entities/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
module Entities
# Task entity
class Task
attr_accessor :id, :title, :description, :author_id, :status
attr_accessor :id, :title, :description, :user_id, :status

def initialize(attributes = {})
@id = attributes[:id]
@title = attributes[:title]
@description = attributes[:description]
@author_id = attributes[:author_id]
@user_id = attributes[:user_id]
@status = attributes[:status]
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/core/use_cases/tasks/create_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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)
def execute(task_params, user_id)
task = Entities::Task.new(task_params.merge(user_id:))
@repository.save(title: task.title, description: task.description, status: 0, user_id: task.user_id)
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240623190417_add_user_id_to_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserIdToTask < ActiveRecord::Migration[7.0]
def change
add_reference :tasks, :user, null: false, foreign_key: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions spec/requests/task_spec.rb

This file was deleted.

46 changes: 42 additions & 4 deletions spec/requests/tasks_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
require 'rails_helper'
# frozen_string_literal: true

RSpec.describe "Tasks", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
require 'swagger_helper'
RSpec.describe 'Tasks', type: :request do
path '/task' do
post 'Create a task' do
tags 'Tasks'
consumes 'application/json'
produces 'application/json'
security [bearer_auth: []]

parameter name: :register_params, in: :body, schema: {
type: :object,
properties: {
title: { type: :string },
description: { type: :string }
},
required: %w[title]
},
required: true

let(:token) { token_scopes('public manage') }

let(:Authorization) { "Bearer #{token.token}" }

request_body_example value: {
title: 'Some title',
description: 'password'
}, name: '1', summary: 'Success 201'

response '201', 'task created' do
let(:register_params) do
{
title: 'user@example.com',
description: 'password'
}
end

run_test! do |response|
expect(response).to have_http_status(:created)
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/use_cases/tasks/create_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
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)
title: task[:title], user_id: user.id).and_return(model_task)

result = create_task.execute(task_params)
result = create_task.execute(task_params, user.id)
expect(result).to be_a(Task)
end
end

0 comments on commit e7d2079

Please sign in to comment.