module Safrano::EntityClassNonMedia

normal handling for non-media entity

Public Instance Methods

odata_create_entity_and_relation(req, assoc = nil, parent = nil) click to toggle source

POST for non-media entity collection –>

  1. Create and add entity from payload

  2. Create relationship if needed

# File lib/odata/model_ext.rb, line 678
def odata_create_entity_and_relation(req, assoc = nil, parent = nil)
  # TODO: this is for v2 only...
  req.with_parsed_data do |data|
    data.delete('__metadata')

    # validate payload column names
    if (invalid = invalid_hash_data?(data))
      ::Safrano::Request::ON_CGST_ERROR.call(req)
      return [422, EMPTY_HASH, ['Invalid attribute name: ', invalid.to_s]]
    end

    if req.accept?(APPJSON)
      new_entity = new_from_hson_h(data)
      if parent
        odata_create_save_entity_and_rel(req, new_entity, assoc, parent)
      else
        # in-changeset requests get their own transaction
        new_entity.save(transaction: !req.in_changeset)
      end
      req.register_content_id_ref(new_entity)
      new_entity.copy_request_infos(req)
      # json is default content type so we dont need to specify it here again
      # TODO quirks array mode !
      # [201, EMPTY_HASH, new_entity.to_odata_post_json(service: req.service)]
      [201, EMPTY_HASH, new_entity.to_odata_create_json(request: req)]
    else # TODO: other formats
      415
    end
  end
end