BigW Consortium Gitlab

boards.rb 3.91 KB
Newer Older
1 2 3 4 5
module API
  # Boards API
  class Boards < Grape::API
    before { authenticate! }

Robert Schilling committed
6 7 8
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
9
    resource :projects do
Robert Schilling committed
10 11 12 13
      desc 'Get all project boards' do
        detail 'This feature was introduced in 8.13'
        success Entities::Board
      end
14 15
      get ':id/boards' do
        authorize!(:read_board, user_project)
16
        present user_project.boards, with: Entities::Board
17 18
      end

Robert Schilling committed
19 20 21
      params do
        requires :board_id, type: Integer, desc: 'The ID of a board'
      end
22 23 24
      segment ':id/boards/:board_id' do
        helpers do
          def project_board
25 26
            board = user_project.boards.first

Robert Schilling committed
27
            if params[:board_id] == board.id
28 29 30 31 32 33 34 35 36 37 38
              board
            else
              not_found!('Board')
            end
          end

          def board_lists
            project_board.lists.destroyable
          end
        end

Robert Schilling committed
39 40 41 42
        desc 'Get the lists of a project board' do
          detail 'Does not include `backlog` and `done` lists. This feature was introduced in 8.13'
          success Entities::List
        end
43 44 45 46 47
        get '/lists' do
          authorize!(:read_board, user_project)
          present board_lists, with: Entities::List
        end

Robert Schilling committed
48 49 50 51 52 53 54
        desc 'Get a list of a project board' do
          detail 'This feature was introduced in 8.13'
          success Entities::List
        end
        params do
          requires :list_id, type: Integer, desc: 'The ID of a list'
        end
55 56 57 58 59
        get '/lists/:list_id' do
          authorize!(:read_board, user_project)
          present board_lists.find(params[:list_id]), with: Entities::List
        end

Robert Schilling committed
60 61 62 63 64 65 66
        desc 'Create a new board list' do
          detail 'This feature was introduced in 8.13'
          success Entities::List
        end
        params do
          requires :label_id, type: Integer, desc: 'The ID of an existing label'
        end
67
        post '/lists' do
68 69
          unless available_labels.exists?(params[:label_id])
            render_api_error!({ error: 'Label not found!' }, 400)
70 71 72 73
          end

          authorize!(:admin_list, user_project)

74 75 76 77
          service = ::Boards::Lists::CreateService.new(user_project, current_user,
            { label_id: params[:label_id] })

          list = service.execute(project_board)
78 79 80 81 82 83 84 85

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

Robert Schilling committed
86 87 88 89 90 91 92 93
        desc 'Moves a board list to a new position' do
          detail 'This feature was introduced in 8.13'
          success Entities::List
        end
        params do
          requires :list_id,  type: Integer, desc: 'The ID of a list'
          requires :position, type: Integer, desc: 'The position of the list'
        end
94 95 96 97 98
        put '/lists/:list_id' do
          list = project_board.lists.movable.find(params[:list_id])

          authorize!(:admin_list, user_project)

99
          service = ::Boards::Lists::MoveService.new(user_project, current_user,
Robert Schilling committed
100
              { position: params[:position] })
101

102
          if service.execute(list)
103 104 105 106 107 108
            present list, with: Entities::List
          else
            render_api_error!({ error: "List could not be moved!" }, 400)
          end
        end

Robert Schilling committed
109 110 111 112 113 114 115
        desc 'Delete a board list' do
          detail 'This feature was introduced in 8.13'
          success Entities::List
        end
        params do
          requires :list_id, type: Integer, desc: 'The ID of a board list'
        end
116 117 118
        delete "/lists/:list_id" do
          authorize!(:admin_list, user_project)

119 120 121 122 123 124
          list = board_lists.find(params[:list_id])

          service = ::Boards::Lists::DestroyService.new(user_project, current_user)

          if service.execute(list)
            present list, with: Entities::List
125
          else
126
            render_api_error!({ error: 'List could not be deleted!' }, 400)
127 128 129 130 131 132
          end
        end
      end
    end
  end
end