Class: Helios::Press::Api::PostsController
- Inherits:
-
BaseController
- Object
- ActionController::API
- BaseController
- Helios::Press::Api::PostsController
- Defined in:
- app/controllers/helios/press/api/posts_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
POST /api/v1/posts Upsert a post by external_id.
Instance Method Details
#create ⇒ Object
POST /api/v1/posts Upsert a post by external_id. Payload shape:
"external_id": "helios-blog-post-draft-4821",
"title": "...",
"slug": "...",
"keywords": "...",
"description": "...",
"body_html": "...",
"published": true
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 |
# File 'app/controllers/helios/press/api/posts_controller.rb', line 16 def create external_id = params[:external_id].to_s return render json: { error: "external_id is required" }, status: :bad_request if external_id.blank? post = Post.find_or_initialize_by(external_id: external_id) post.assign_attributes( name: params[:title], slug: params[:slug].presence, keywords: params[:keywords], description: params[:description], published: ActiveModel::Type::Boolean.new.cast(params[:published]) ) # If body_html is provided, create/update a single text block with the content if params[:body_html].present? if post.persisted? # Update or create the first text block text_block = post.blocks.find_by(block_type: "text") || post.blocks.build(block_type: "text", position: 0) text_block.content = params[:body_html] else post.blocks.build(block_type: "text", position: 0, content: params[:body_html]) end end post.save! render json: { ok: true, id: post.id, slug: post.slug }, status: :ok rescue ActiveRecord::RecordInvalid => e render json: { errors: e.record.errors. }, status: :unprocessable_entity end |