forked from fs/task_tracker_itis_2023
-
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.
* added lesson practice & homework * rubocop fix * minor changes --------- Co-authored-by: Denis Zakharov <[email protected]>
- Loading branch information
1 parent
11cd4e5
commit ff6ec56
Showing
5 changed files
with
117 additions
and
2 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
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 |
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,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 |
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