summaryrefslogtreecommitdiff
path: root/lib/api/broadcast_messages.rb
blob: fb2a41480113d71390b2ca2d5f18a192e541ec4d (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
module API
  class BroadcastMessages < Grape::API
    before { authenticate! }
    before { authenticated_as_admin! }

    resource :broadcast_messages do
      helpers do
        def find_message
          BroadcastMessage.find(params[:id])
        end
      end

      desc 'Get all broadcast messages' do
        detail 'This feature was introduced in GitLab 8.12.'
        success Entities::BroadcastMessage
      end
      params do
        optional :page,     type: Integer, desc: 'Current page number'
        optional :per_page, type: Integer, desc: 'Number of messages per page'
      end
      get do
        messages = BroadcastMessage.all

        present paginate(messages), with: Entities::BroadcastMessage
      end

      desc 'Create a broadcast message' do
        detail 'This feature was introduced in GitLab 8.12.'
        success Entities::BroadcastMessage
      end
      params do
        requires :message,   type: String,   desc: 'Message to display'
        optional :starts_at, type: DateTime, desc: 'Starting time', default: -> { Time.zone.now }
        optional :ends_at,   type: DateTime, desc: 'Ending time',   default: -> { 1.hour.from_now }
        optional :color,     type: String,   desc: 'Background color'
        optional :font,      type: String,   desc: 'Foreground color'
      end
      post do
        create_params = declared(params, include_missing: false).to_h
        message = BroadcastMessage.create(create_params)

        if message.persisted?
          present message, with: Entities::BroadcastMessage
        else
          render_validation_error!(message)
        end
      end

      desc 'Get a specific broadcast message' do
        detail 'This feature was introduced in GitLab 8.12.'
        success Entities::BroadcastMessage
      end
      params do
        requires :id, type: Integer, desc: 'Broadcast message ID'
      end
      get ':id' do
        message = find_message

        present message, with: Entities::BroadcastMessage
      end

      desc 'Update a broadcast message' do
        detail 'This feature was introduced in GitLab 8.12.'
        success Entities::BroadcastMessage
      end
      params do
        requires :id,        type: Integer,  desc: 'Broadcast message ID'
        optional :message,   type: String,   desc: 'Message to display'
        optional :starts_at, type: DateTime, desc: 'Starting time'
        optional :ends_at,   type: DateTime, desc: 'Ending time'
        optional :color,     type: String,   desc: 'Background color'
        optional :font,      type: String,   desc: 'Foreground color'
      end
      put ':id' do
        message = find_message
        update_params = declared(params, include_missing: false).to_h

        if message.update(update_params)
          present message, with: Entities::BroadcastMessage
        else
          render_validation_error!(message)
        end
      end

      desc 'Delete a broadcast message' do
        detail 'This feature was introduced in GitLab 8.12.'
        success Entities::BroadcastMessage
      end
      params do
        requires :id, type: Integer, desc: 'Broadcast message ID'
      end
      delete ':id' do
        message = find_message

        present message.destroy, with: Entities::BroadcastMessage
      end
    end
  end
end