Skip to content

Commit

Permalink
HW11 gernichenko (fs#128)
Browse files Browse the repository at this point in the history
* added lesson practice & homework

* rubocop fix

* minor changes

---------

Co-authored-by: Denis Zakharov <[email protected]>
  • Loading branch information
elenagernichenko and DenisZackharov authored Nov 28, 2023
1 parent 11cd4e5 commit ff6ec56
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
29 changes: 29 additions & 0 deletions app/controllers/api/v1/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Api
module V1
class ProjectsController < Api::ApplicationController
before_action :set_project, only: %i[update destroy]

def index
@projects = Project.includes(:tasks)
.order(params[:sort])
Expand All @@ -22,13 +24,40 @@ def create
end
end

def update
if update_project.success?
render json: { project: @project, message: "Update Successful" }
else
render json: { message: "Error" }
end
end

def destroy
return unless destroy_project.success?

render json: { message: "Project Destroyed" }
end

private

def set_project
@project = Project.find_by(id: params[:id])
end

def create_project
@create_project ||=
Projects::Create.call(project_params: project_params, user: current_user)
end

def update_project
::Projects::Update.call(project: @project,
project_params: project_params)
end

def destroy_project
::Projects::Destroy.call(project: @project)
end

def project_params
params.require(:project).permit(:name, :description)
end
Expand Down
73 changes: 73 additions & 0 deletions app/controllers/api/v1/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Api
module V1
class TasksController < Api::ApplicationController
before_action :set_project
before_action :set_task, only: %i[update destroy]
def index
@tasks = Task.order(params[:sort])
.page(params[:page])
.per(3)

serializable_tasks = ActiveModelSerializers::SerializableResource.new(
@tasks, each_serializer: TaskSerializer
)
render json: { tasks: serializable_tasks }
end

def create
@task = create_task.task
serializable_task = ActiveModelSerializers::SerializableResource.new(
@task, serializer: TaskSerializer
)
if create_task.success?
render json: { task: serializable_task, message: "Task Created" }
else
render json: { task: {}, errors: @task.errors }
end
end

def update
@task = update_task.task
serializable_task = ActiveModelSerializers::SerializableResource.new(
@task, serializer: TaskSerializer
)
if update_task.success?
render json: { task: serializable_task, message: "Task Updated" }
else
render json: { task: {}, errors: @task.errors }
end
end

def destroy
destroy_task
render json: { task: {}, errors: @task.id, message: "Task Destroyed" }
end

private

def create_task
@create_task ||= ::Tasks::Create.call(task_params: task_params, project: @project)
end

def update_task
::Tasks::Update.call(task_params: task_params, task: @task)
end

def destroy_task
::Tasks::Destroy.call(task: @task, user: current_user)
end

def task_params
params.require(:task).permit(:name, :description, :status, :deadline_at)
end

def set_project
@project = Project.find_by(id: params[:project_id])
end

def set_task
@task = @project.tasks.find(params[:id])
end
end
end
end
2 changes: 1 addition & 1 deletion app/serializers/api/v1/project_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ProjectSerializer < ActiveModel::Serializer
object.name.capitalize
end

has_many :tasks
has_many :tasks, serializer: TaskSerializer
end
end
end
11 changes: 11 additions & 0 deletions app/serializers/api/v1/task_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Api
module V1
class TaskSerializer < ActiveModel::Serializer
attributes :id, :name, :description, :status, :deadline_at

attribute :deadline_at do
object.deadline_at.strftime("%Y-%m-%d")
end
end
end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :projects, only: %i[index create]
resources :projects, only: %i[index create update destroy] do
resources :tasks, only: %i[index create update destroy]
end
end
end

Expand Down

0 comments on commit ff6ec56

Please sign in to comment.