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