Last active
September 18, 2018 00:36
-
-
Save ojulianos/0fabbea675e678a43104a90a4a408cc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Essa função deverá identificar quando a altura vem no formato double com ".", | |
caso venha retornará o mesmo formatado, caso não, deverá retornar o substr para que adicione o ponto | |
e depois converter o valor em float | |
*/ | |
public function postPeso($request, $response) | |
{ | |
$response = $response->withHeader('Content-Type', 'application/json'); | |
$request_data = $request->getParsedBody(); | |
$peso = new Peso; | |
$peso->set_id_aluno($this->token->id_aluno); | |
// check if peso and altura fields are not empty | |
if(empty($peso_aluno)){ | |
$this->return['message'] = 'Por favor preencha o seu peso!'; | |
return $response->write(json_encode($this->return, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); | |
} | |
if(empty($altura)){ | |
$this->return['message'] = 'Por favor preencha a sua altura!'; | |
return $response->write(json_encode($this->return, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); | |
} | |
// convert $altura like 160 to 1.6 | |
$convert_height = function($floatString){ | |
if(strpos($floatString, '.')){ | |
return sprintf("%.2f", $floatString); | |
} | |
$str = substr($floatString, 0, 1) . '.' . substr($floatString, 1); | |
return (float) $str; | |
}; | |
// insert new peso | |
$peso->insert([ | |
'id_aluno' => $this->token->id_aluno, | |
'peso' => $request_data['peso'], | |
'altura' => $convert_height($request_data['altura']), | |
'observacoes' => $request_data['observacoes'], | |
]); | |
// return message | |
$this->return['status'] = 'success'; | |
$this->return['message'] = 'Nova pesagem cadastrada!'; | |
return $response->write(json_encode($this->return, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment