Class: TelegramBotEngine::Admin::EventsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/telegram_bot_engine/admin/events_controller.rb

Constant Summary collapse

PER_PAGE =
50

Instance Method Summary collapse

Instance Method Details

#indexObject



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
# File 'app/controllers/telegram_bot_engine/admin/events_controller.rb', line 8

def index
  unless Event.table_exists?
    respond_to do |format|
      format.html do
        @events = []
        @total_count = 0
        @page = 1
        @total_pages = 0
        flash.now[:alert] = "Events table not found. Run: rails telegram_bot_engine:install:migrations && rails db:migrate"
      end
      format.json { render json: { error: "Events table not found" }, status: :service_unavailable }
    end
    return
  end

  @events = Event.recent
  @events = @events.by_type(params[:type]) if params[:type].present?
  @events = @events.by_action(params[:action_name]) if params[:action_name].present?
  @events = @events.by_chat_id(params[:chat_id]) if params[:chat_id].present?

  @total_count = @events.count
  @page = [params[:page].to_i, 1].max
  @events = @events.offset((@page - 1) * PER_PAGE).limit(PER_PAGE)
  @total_pages = (@total_count.to_f / PER_PAGE).ceil

  respond_to do |format|
    format.html
    format.json do
      render json: {
        events: @events.map { |e|
          {
            id: e.id,
            event_type: e.event_type,
            action: e.action,
            chat_id: e.chat_id,
            username: e.username,
            details: e.details,
            created_at: e.created_at.iso8601
          }
        },
        meta: {
          total_count: @total_count,
          page: @page,
          total_pages: @total_pages,
          per_page: PER_PAGE
        }
      }
    end
  end
end