summaryrefslogtreecommitdiff
path: root/lib/api/boards_responses.rb
blob: 2ae82f78e0117bf537a506d4553900a1b0f28c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true

module API
  module BoardsResponses
    extend ActiveSupport::Concern

    included do
      helpers do
        def board
          board_parent.boards.find(params[:board_id])
        end

        def create_board
          forbidden! unless board_parent.multiple_issue_boards_available?

          response =
            ::Boards::CreateService.new(board_parent, current_user, { name: params[:name] }).execute

          present response.payload, with: Entities::Board
        end

        def update_board
          service = ::Boards::UpdateService.new(board_parent, current_user, declared_params(include_missing: false))
          service.execute(board)

          if board.valid?
            present board, with: Entities::Board
          else
            bad_request!("Failed to save board #{board.errors.messages}")
          end
        end

        def delete_board
          forbidden! unless board_parent.multiple_issue_boards_available?

          destroy_conditionally!(board) do |board|
            service = ::Boards::DestroyService.new(board_parent, current_user)
            service.execute(board)
          end
        end

        def board_lists
          board.destroyable_lists
        end

        def create_list
          create_list_service =
            ::Boards::Lists::CreateService.new(board_parent, current_user, create_list_params)

          list = create_list_service.execute(board)

          if list.valid?
            present list, with: Entities::List
          else
            render_validation_error!(list)
          end
        end

        def create_list_params
          params.slice(:label_id)
        end

        def move_list(list)
          move_list_service =
            ::Boards::Lists::MoveService.new(board_parent, current_user, { position: params[:position].to_i })

          if move_list_service.execute(list)
            present list, with: Entities::List
          else
            render_api_error!({ error: "List could not be moved!" }, 400)
          end
        end

        def destroy_list(list)
          destroy_conditionally!(list) do |list|
            service = ::Boards::Lists::DestroyService.new(board_parent, current_user)
            if service.execute(list).error?
              render_api_error!({ error: 'List could not be deleted!' }, 400)
            end
          end
        end

        # rubocop: disable CodeReuse/ActiveRecord
        def authorize_list_type_resource!
          unless available_labels_for(board_parent).exists?(params[:label_id])
            render_api_error!({ error: 'Label not found!' }, 400)
          end
        end
        # rubocop: enable CodeReuse/ActiveRecord

        params :list_creation_params do
          requires :label_id, type: Integer, desc: 'The ID of an existing label'
        end

        params :update_params do
          # Configurable issue boards are not available in CE/EE Core.
          # https://docs.gitlab.com/ee/user/project/issue_board.html#configurable-issue-boards
          optional :name, type: String, desc: 'The board name'
        end
      end
    end
  end
end