Class: Collavre::CreativeImportsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/collavre/creative_imports_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/collavre/creative_imports_controller.rb', line 7

def create
  unless authenticated?
    render json: { error: "Unauthorized" }, status: :unauthorized and return
  end

  parent = params[:parent_id].present? ? Creative.find_by(id: params[:parent_id]) : nil
  if parent && !parent.has_permission?(Current.user, :write)
    render json: { error: I18n.t("collavre.creatives.errors.no_permission") }, status: :forbidden and return
  end

  created = ::Creatives::Importer.new(file: params[:markdown], user: Current.user, parent: parent).call

  if created.any?
    render json: { success: true, created: created.map(&:id) }
  else
    render json: { error: "No creatives created" }, status: :unprocessable_entity
  end
rescue ::Creatives::Importer::UnsupportedFile
  render json: { error: "Invalid file type" }, status: :unprocessable_entity
rescue ::Creatives::Importer::Error => e
  render json: { error: e.message }, status: :unprocessable_entity
end