summaryrefslogtreecommitdiff
path: root/lib/api/boards.rb
blob: 4d5d144a02e77fa4224cba93d1c3ecc1f4002d38 (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
104
105
106
107
108
109
110
111
112
113
114
115
module API
  # Boards API
  class Boards < Grape::API
    before { authenticate! }

    resource :projects do
      # Get the project board
      get ':id/boards' do
        authorize!(:read_board, user_project)
        present [user_project.board], with: Entities::Board
      end

      segment ':id/boards/:board_id' do
        helpers do
          def project_board
            board = user_project.board
            if params[:board_id].to_i == board.id
              board
            else
              not_found!('Board')
            end
          end

          def board_lists
            project_board.lists.destroyable
          end
        end

        # Get the lists of a project board
        # Does not include `backlog` and `done` lists
        get '/lists' do
          authorize!(:read_board, user_project)
          present board_lists, with: Entities::List
        end

        # Get a list of a project board
        get '/lists/:list_id' do
          authorize!(:read_board, user_project)
          present board_lists.find(params[:list_id]), with: Entities::List
        end

        # Create a new board list
        #
        # Parameters:
        #   id (required)           - The ID of a project
        #   label_id (required)     - The ID of an existing label
        # Example Request:
        #   POST /projects/:id/boards/:board_id/lists
        post '/lists' do
          required_attributes! [:label_id]

          unless user_project.labels.exists?(params[:label_id])
            render_api_error!({ error: "Label not found!" }, 400)
          end

          authorize!(:admin_list, user_project)

          list = ::Boards::Lists::CreateService.new(user_project, current_user,
              { label_id: params[:label_id] }).execute

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

        # Moves a board list to a new position
        #
        # Parameters:
        #   id (required) - The ID of a project
        #   board_id (required) - The ID of a board
        #   position (required) - The position of the list
        # Example Request:
        #   PUT /projects/:id/boards/:board_id/lists/:list_id
        put '/lists/:list_id' do
          list = project_board.lists.movable.find(params[:list_id])

          authorize!(:admin_list, user_project)

          moved = ::Boards::Lists::MoveService.new(user_project, current_user,
              { position: params[:position].to_i }).execute(list)

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

        # Delete a board list
        #
        # Parameters:
        #   id (required) - The ID of a project
        #   board_id (required) - The ID of a board
        #   list_id (required) - The ID of a board list
        # Example Request:
        #   DELETE /projects/:id/boards/:board_id/lists/:list_id
        delete "/lists/:list_id" do
          list = board_lists.find_by(id: params[:list_id])

          authorize!(:admin_list, user_project)

          if list
            destroyed_list = ::Boards::Lists::DestroyService.new(
              user_project, current_user).execute(list)
            present destroyed_list, with: Entities::List
          else
            not_found!('List')
          end
        end
      end
    end
  end
end