gitlab-org--gitlab-foss/app/controllers/boards/lists_controller.rb

103 lines
2.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-08-28 17:56:49 -04:00
module Boards
class ListsController < Boards::ApplicationController
include BoardsResponses
before_action :authorize_admin_list, only: [:create, :destroy, :generate]
2017-08-28 17:56:49 -04:00
before_action :authorize_read_list, only: [:index]
2017-08-31 13:48:57 -04:00
skip_before_action :authenticate_user!, only: [:index]
2017-08-28 17:56:49 -04:00
feature_category :boards
2017-08-28 17:56:49 -04:00
def index
lists = Boards::Lists::ListService.new(board.resource_parent, current_user).execute(board)
2017-08-28 17:56:49 -04:00
List.preload_preferences_for_user(lists, current_user)
2017-08-28 17:56:49 -04:00
render json: serialize_as_json(lists)
end
def create
response = Boards::Lists::CreateService.new(board.resource_parent, current_user, create_list_params).execute(board)
2017-08-28 17:56:49 -04:00
if response.success?
render json: serialize_as_json(response.payload[:list])
2017-08-28 17:56:49 -04:00
else
render json: { errors: response.errors }, status: :unprocessable_entity
2017-08-28 17:56:49 -04:00
end
end
def update
list = board.lists.find(params[:id])
service = Boards::Lists::UpdateService.new(board_parent, current_user, update_list_params)
result = service.execute(list)
2017-08-28 17:56:49 -04:00
if result.success?
2017-08-28 17:56:49 -04:00
head :ok
else
head result.http_status
2017-08-28 17:56:49 -04:00
end
end
def destroy
list = board.lists.destroyable.find(params[:id])
service = Boards::Lists::DestroyService.new(board_parent, current_user)
if service.execute(list).success?
2017-08-28 17:56:49 -04:00
head :ok
else
head :unprocessable_entity
end
end
def generate
service = Boards::Lists::GenerateService.new(board_parent, current_user)
if service.execute(board)
lists = board.lists.movable.preload_associated_models
List.preload_preferences_for_user(lists, current_user)
render json: serialize_as_json(lists)
2017-08-28 17:56:49 -04:00
else
head :unprocessable_entity
end
end
private
def list_creation_attrs
%i[label_id]
end
def list_update_attrs
%i[collapsed position]
end
def create_list_params
params.require(:list).permit(list_creation_attrs)
2017-08-28 17:56:49 -04:00
end
def update_list_params
params.require(:list).permit(list_update_attrs)
2017-08-28 17:56:49 -04:00
end
def serialize_as_json(resource)
resource.as_json(serialization_attrs)
end
def serialization_attrs
{
2017-08-28 17:56:49 -04:00
only: [:id, :list_type, :position],
methods: [:title],
label: true,
collapsed: true,
current_user: current_user
}
2017-08-28 17:56:49 -04:00
end
end
end
Boards::ListsController.prepend_mod_with('Boards::ListsController')