/** * API Method inserts a new Servico record and render response as JSON */ public function Create() { try { $json = json_decode(RequestUtil::GetBody()); if (!$json) { throw new Exception('The request body does not contain valid JSON'); } $servico = new Servico($this->Phreezer); // TODO: any fields that should not be inserted by the user should be commented out $servico->Id = $this->SafeGetVal($json, 'id'); $servico->Placa = $this->SafeGetVal($json, 'placa'); $servico->Servico = $this->SafeGetVal($json, 'servico'); $servico->Descricao = $this->SafeGetVal($json, 'descricao'); $servico->Valor = $this->SafeGetVal($json, 'valor'); $servico->Validate(); $errors = $servico->GetValidationErrors(); if (count($errors) > 0) { $this->RenderErrorJSON('Please check the form for errors', $errors); } else { // since the primary key is not auto-increment we must force the insert here $servico->Save(true); $this->RenderJSON($servico, $this->JSONPCallback(), true, $this->SimpleObjectParams()); } } catch (Exception $ex) { $this->RenderExceptionJSON($ex); } }