Expand currency endpoint.

This commit is contained in:
James Cole 2018-12-09 06:51:53 +01:00
parent 2a68ce6c90
commit 82e4055fa6
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 21 additions and 7 deletions

View File

@ -92,6 +92,7 @@ class CurrencyExchangeRateController extends Controller
$this->parameters->set('from', $fromCurrency->code);
$this->parameters->set('to', $toCurrency->code);
$this->parameters->set('date', $dateObj->format('Y-m-d'));
$this->parameters->set('amount', $request->get('amount'));
$rate = $this->repository->getExchangeRate($fromCurrency, $toCurrency, $dateObj);
if (null === $rate) {

View File

@ -55,13 +55,26 @@ class CurrencyExchangeRateTransformer extends TransformerAbstract
*/
public function transform(CurrencyExchangeRate $rate): array
{
$data = [
'id' => (int)$rate->id,
'updated_at' => $rate->updated_at->toAtomString(),
'created_at' => $rate->created_at->toAtomString(),
'date' => $rate->date->format('Y-m-d'),
'rate' => (float)$rate->rate,
'links' => [
$result = round((float)$rate->rate * (float)$this->parameters->get('amount'), $rate->toCurrency->decimal_places);
$result = 0.0 === $result ? null : $result;
$data = [
'id' => (int)$rate->id,
'updated_at' => $rate->updated_at->toAtomString(),
'created_at' => $rate->created_at->toAtomString(),
'from_currency_id' => $rate->fromCurrency->id,
'from_currency_name' => $rate->fromCurrency->name,
'from_currency_code' => $rate->fromCurrency->code,
'from_currency_symbol' => $rate->fromCurrency->symbol,
'from_currency_dp' => $rate->fromCurrency->decimal_places,
'to_currency_id' => $rate->toCurrency->id,
'to_currency_name' => $rate->toCurrency->name,
'to_currency_code' => $rate->toCurrency->code,
'to_currency_symbol' => $rate->toCurrency->symbol,
'to_currency_dp' => $rate->toCurrency->decimal_places,
'date' => $rate->date->format('Y-m-d'),
'rate' => (float)$rate->rate,
'amount' => $result,
'links' => [
[
'rel' => 'self',
'uri' => '/currency_exchange_rates/' . $rate->id,