This commit is contained in:
2021-03-20 16:51:17 -03:00
parent 845077d2a0
commit 53a519ffbd
7 changed files with 175 additions and 8 deletions

View File

@ -130,4 +130,47 @@ class Currencies {
}
return $this->withJson($response, $output);
}
public function getSources(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
$currency = $factory->find(Currency::class)->one($currency_id);
$output = [
'get_data' => compact('currency_id'),
'currency' => null,
'sources' => []
];
if ($currency) {
$output['currency'] = $currency->asArray();
if ($currency->sources()) {
$output['sources'] = array_map(function($item) {
return $item->asArray();
}, $currency->sources());
}
}
return $this->withJson($response, $output);
}
public function addSources(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
$currency = $factory->find(Currency::class)->one($currency_id);
$post = json_decode($request->getBody()->getContents());
$output = [
'get_data' => compact('currency_id'),
'post_data' => $post,
'currency' => null,
'sources' => []
];
if ($currency) {
$output['currency'] = $currency->asArray();
$sources = [];
if (is_array($post)) {
foreach ($post as $obj) {
if (!is_object($obj)) {
continue;
}
$sources []= $currency->addSource($obj);
}
} else {
$sources []= $currency->addSource($post);
}
$output['sources'] = $sources;
}
return $this->withJson($response, $output);
}
}