Method: Anoubis::Data::Actions#create

Defined in:
app/controllers/anoubis/data/actions.rb

#createObject

Create action of Anoubis::DataController. Procedure inserts data into database. Authorization bearer is required.

API request:

POST /api/<version>/<controller>/new

Request Header:

{
  "Authorization": "Bearer <Session token>"
}

Parameters:

  • locale (String) — the output language locale (optional value)

  • tab (String) — the tab, is used for action (optional value, default: first defined tab)

  • data (String) — inserted data (required value)

Request example:

curl --request POST --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' --data='{"title": "Sailor Mars", "name": "Rey Hino", "state": "inner"}' http://<server>:<port>/api/<api-version>/<controller>/?tab=<tab>

Results:

Resulting data returns in JSON format.

Examples:

Success: HTTP response code 200

{
  "result": 0,
  "message": "Successful",
  "timestamp": 1563271417,
  "tab": "inner",
  "values": {
      "id": 3,
      "sys_title": "Sailor Mars",
      "actions": {
        "edit": "Edit: Sailor Mars",
        "delete": "Delete: Sailor Mars"
      },
      "title": "Sailor Mars",
      "name": "Rey Hino",
      "state": "Inner Senshi",
      "raw_state": "inner"
  },
  "action": ""
}

Error (data presents): HTTP response code 200

{
  "result": -3,
  "message": "Error update data",
  "timestamp": 1563271417,
  "tab": "inner",
  "errors": [
    "Title already presents"
  ]
}

Error (session expired): HTTP response code 422

{
  "result": -1,
  "message": "Session expired",
  "timestamp": 1563271417,
  "tab": "inner"
}


481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'app/controllers/anoubis/data/actions.rb', line 481

def create
  self.output = Anoubis::Output::Update.new
  self.set_parent_model 'create'
  self.output.tab = self.etc.tab.tab
  if params.key? :data
    if self.etc.tab.buttons.key? :new
      self.load_new_data
      if self.etc.data.data
        self.setup_fields
        data = get_permited_params

        data = self.before_create_data data

        if data
          data.each_key do |key|
            self.convert_view_to_db_value key, data[key]
          end

          if self.etc.data.data.respond_to? :tenant_id
            if self.current_user.respond_to? :tenant_id
              self.etc.data.data.tenant_id = self.current_user.tenant_id if !self.etc.data.data.tenant_id
            end
          end

          if self.etc.data.data.save
          else
            self.output.errors.concat self.etc.data.data.errors.full_messages
          end
        else
          self.output.result = -4
        end

        if self.output.errors.length == 0
          self.etc.data.fields = nil
          self.set_new_action 'index'
          self.output.values = self.get_data_row self.etc.data.data
          self.set_new_action 'create'
          self.after_create_data
        else
          self.output.result = -3
        end
      else
        self.output.result = -2
      end
    else
      self.output.result = -1
    end
  else
    self.output.result = -2
  end
  self.before_output

  render json: around_output(output.to_h)
end