module Coronavirus
  class LiveStreamController < ApplicationController
    before_action :require_livestream_editor_permissions!
    layout "admin_layout"

    def index
      @live_stream = updater.object
    end

    def update
      @live_stream = LiveStream.last
      if @live_stream.update(url: url_params, formatted_stream_date: formatted_date)
        if updater.update
          flash[:notice] = I18n.t("coronavirus.live_stream.update.success")
        else
          flash[:alert] = I18n.t("coronavirus.live_stream.update.failed")
        end
      else
        flash[:alert] = @live_stream.errors.full_messages.join(", ")
      end
      redirect_to coronavirus_live_stream_index_path
    end

    def publish
      if updater.publish
        flash[:notice] = I18n.t("coronavirus.live_stream.publish.success")
      else
        flash[:alert] = I18n.t("coronavirus.live_stream.publish.failed")
      end
      redirect_to coronavirus_live_stream_index_path
    end

  private

    def updater
      LiveStreamUpdater.new
    end

    def url_params
      params[:url]
    end

    def formatted_date
      Time.zone.now.strftime("%-d %B %Y")
    end
  end
end

module Coronavirus
  class LiveStreamController < ApplicationController
    before_action :require_livestream_editor_permissions!
    layout "admin_layout"

    def index
      @live_stream = updater.object
    end

    def update
      @live_stream = LiveStream.last
      if @live_stream.update(url: url_params, formatted_stream_date: formatted_date)
        if updater.update
          flash[:notice] = helpers.t("coronavirus.live_stream.update.success")
        else
          flash[:alert] = helpers.t("coronavirus.live_stream.update.failed")
        end
      else
        flash[:alert] = @live_stream.errors.full_messages.join(", ")
      end
      redirect_to coronavirus_live_stream_index_path
    end

    def publish
      if updater.publish
        flash[:notice] = helpers.t("coronavirus.live_stream.publish.success")
      else
        flash[:alert] = helpers.t("coronavirus.live_stream.publish.failed")
      end
      redirect_to coronavirus_live_stream_index_path
    end

  private

    def updater
      LiveStreamUpdater.new
    end

    def url_params
      params[:url]
    end

    def formatted_date
      Time.zone.now.strftime("%-d %B %Y")
    end
  end
end

Raise exception for missing livestream translation
Raise exception for missing livestream translation

We already have `config.action_view.raise_on_missing_translations = true` set
so an error will be raised if a view tries to call a missing translation.
However, to get the same effect with content called from a controller, the method needs
to be called with a `!`. i.e. `I18n.t!`.
Rather than call translations without a bang in the views, but with one in the controllers,
we can instead use the helper method from the view.
