2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Boards
|
|
|
|
class ListsController < Boards::ApplicationController
|
|
|
|
include BoardsResponses
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
before_action :authorize_admin_list, only: [:create, :destroy, :generate]
|
2018-03-17 18:26:18 +05:30
|
|
|
before_action :authorize_read_list, only: [:index]
|
|
|
|
skip_before_action :authenticate_user!, only: [:index]
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
feature_category :boards
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def index
|
2019-12-21 20:55:43 +05:30
|
|
|
lists = Boards::Lists::ListService.new(board.resource_parent, current_user).execute(board)
|
|
|
|
|
|
|
|
List.preload_preferences_for_user(lists, current_user)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
render json: serialize_as_json(lists)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2021-02-22 17:27:13 +05:30
|
|
|
response = Boards::Lists::CreateService.new(board.resource_parent, current_user, create_list_params).execute(board)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
if response.success?
|
|
|
|
render json: serialize_as_json(response.payload[:list])
|
2018-03-17 18:26:18 +05:30
|
|
|
else
|
2021-02-22 17:27:13 +05:30
|
|
|
render json: { errors: response.errors }, status: :unprocessable_entity
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2019-12-04 20:38:33 +05:30
|
|
|
list = board.lists.find(params[:id])
|
|
|
|
service = Boards::Lists::UpdateService.new(board_parent, current_user, update_list_params)
|
|
|
|
result = service.execute(list)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
if result.success?
|
2018-03-17 18:26:18 +05:30
|
|
|
head :ok
|
|
|
|
else
|
2021-06-08 01:23:25 +05:30
|
|
|
head result.http_status
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
list = board.lists.destroyable.find(params[:id])
|
|
|
|
service = Boards::Lists::DestroyService.new(board_parent, current_user)
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
if service.execute(list).success?
|
2018-03-17 18:26:18 +05:30
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate
|
|
|
|
service = Boards::Lists::GenerateService.new(board_parent, current_user)
|
|
|
|
|
|
|
|
if service.execute(board)
|
2020-01-01 13:55:28 +05:30
|
|
|
lists = board.lists.movable.preload_associated_models
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
List.preload_preferences_for_user(lists, current_user)
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
render json: serialize_as_json(lists)
|
2018-03-17 18:26:18 +05:30
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def list_creation_attrs
|
|
|
|
%i[label_id]
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def list_update_attrs
|
|
|
|
%i[collapsed position]
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def create_list_params
|
2018-11-08 19:23:39 +05:30
|
|
|
params.require(:list).permit(list_creation_attrs)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def update_list_params
|
2019-12-21 20:55:43 +05:30
|
|
|
params.require(:list).permit(list_update_attrs)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_as_json(resource)
|
2018-11-08 19:23:39 +05:30
|
|
|
resource.as_json(serialization_attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialization_attrs
|
|
|
|
{
|
2018-03-17 18:26:18 +05:30
|
|
|
only: [:id, :list_type, :position],
|
|
|
|
methods: [:title],
|
2019-12-04 20:38:33 +05:30
|
|
|
label: true,
|
|
|
|
collapsed: true,
|
|
|
|
current_user: current_user
|
2018-11-08 19:23:39 +05:30
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Boards::ListsController.prepend_mod_with('Boards::ListsController')
|