BigW Consortium Gitlab

milestones.rb 2.73 KB
Newer Older
1
module API
Robert Speicher committed
2 3 4 5 6 7 8 9
  # Milestones API
  class Milestones < Grape::API
    before { authenticate! }

    resource :projects do
      # Get a list of project milestones
      #
      # Parameters:
10
      #   id (required) - The ID of a project
Robert Speicher committed
11 12 13
      # Example Request:
      #   GET /projects/:id/milestones
      get ":id/milestones" do
14 15
        authorize! :read_milestone, user_project

Nihad Abbasov committed
16
        present paginate(user_project.milestones), with: Entities::Milestone
Robert Speicher committed
17 18 19 20 21
      end

      # Get a single project milestone
      #
      # Parameters:
22
      #   id (required) - The ID of a project
Robert Speicher committed
23 24 25 26
      #   milestone_id (required) - The ID of a project milestone
      # Example Request:
      #   GET /projects/:id/milestones/:milestone_id
      get ":id/milestones/:milestone_id" do
27 28
        authorize! :read_milestone, user_project

Robert Speicher committed
29 30 31 32 33 34 35
        @milestone = user_project.milestones.find(params[:milestone_id])
        present @milestone, with: Entities::Milestone
      end

      # Create a new project milestone
      #
      # Parameters:
36
      #   id (required) - The ID of the project
Robert Speicher committed
37 38 39 40 41 42
      #   title (required) - The title of the milestone
      #   description (optional) - The description of the milestone
      #   due_date (optional) - The due date of the milestone
      # Example Request:
      #   POST /projects/:id/milestones
      post ":id/milestones" do
43 44 45
        set_current_user_for_thread do
          authorize! :admin_milestone, user_project
          required_attributes! [:title]
46

47 48 49 50 51 52 53
          attrs = attributes_for_keys [:title, :description, :due_date]
          @milestone = user_project.milestones.new attrs
          if @milestone.save
            present @milestone, with: Entities::Milestone
          else
            not_found!
          end
Robert Speicher committed
54 55 56 57 58 59
        end
      end

      # Update an existing project milestone
      #
      # Parameters:
60
      #   id (required) - The ID of a project
61
      #   milestone_id (required) - The ID of a project milestone
Robert Speicher committed
62 63 64
      #   title (optional) - The title of a milestone
      #   description (optional) - The description of a milestone
      #   due_date (optional) - The due date of a milestone
65
      #   state_event (optional) - The state event of the milestone (close|activate)
Robert Speicher committed
66 67 68
      # Example Request:
      #   PUT /projects/:id/milestones/:milestone_id
      put ":id/milestones/:milestone_id" do
69 70
        set_current_user_for_thread do
          authorize! :admin_milestone, user_project
randx committed
71

72 73 74 75 76 77 78
          @milestone = user_project.milestones.find(params[:milestone_id])
          attrs = attributes_for_keys [:title, :description, :due_date, :state_event]
          if @milestone.update_attributes attrs
            present @milestone, with: Entities::Milestone
          else
            not_found!
          end
Robert Speicher committed
79 80 81 82 83
        end
      end
    end
  end
end