Commit 8192635b by Florian Shllaku

Retriving data from API - live data finished

parent 3aa40a09
...@@ -4,6 +4,7 @@ namespace App\Http\Controllers; ...@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\CurrencyListing; use App\CurrencyListing;
use function GuzzleHttp\json_encode;
class API extends Controller class API extends Controller
{ {
...@@ -11,6 +12,10 @@ class API extends Controller ...@@ -11,6 +12,10 @@ class API extends Controller
{ {
$data = CurrencyListing::all('iso', 'rates'); $data = CurrencyListing::all('iso', 'rates');
return $data; foreach($data as $i => $item) {
$data[$i] = array($item["iso"] => $item["rates"]);
}
$ex = str_replace( array( '[', ']'), array( '{', '}'),json_encode($data));
return '{'.str_replace( array( '{', '}'), '', $ex).'}';
} }
} }
{ {
"USD": [ "USD":1.24,
{
"iso": "USD", "BGN": 1.96,
"rates": 1.12
}], "CZK": 25.7,
"JPY": [
{ "DKK": 7.47,
"iso": "JPY",
"rates": 124.4 "GBP": 0.86,
}],
"BGN": [ "HUF": 323,
{
"iso": "BGN", "PLN": 4.29,
"rates": 1.96
}], "RON": 4.76,
"CZK": [
{ "SEK": 10.7,
"iso": "CZK",
"rates": 25.71 "CHF": 1.1400000000000001,
}],
"DKK": [ "ISK": 136,
{
"iso": "DKK", "NOK": 9.78,
"rates": 7.47
}], "HRK": 7.41,
"GBP": [
{ "RUB": 72.8,
"iso": "GBP",
"rates": 0.86 "TRY": 6.66,
}],
"HUF": [ "AUD": 1.5899999999999999,
{
"iso": "HUF", "BRL": 4.42,
"rates": 323.38
}], "CAD": 1.5,
"PLN": [
{ "CNY": 7.51,
"iso": "PLN",
"rates": 4.29 "HDK": 8.75,
}],
"RON": [ "IDR": 1590,
{
"iso": "RON", "ILS": 4.01,
"rates": 4.76
}], "INR": 77.2,
"SEK": [
{ "KRW": 1305,
"iso": "SEK",
"rates": 10.7 "MXN": 21.3,
}],
"CHF": [ "MYR": 4.62,
{
"iso": "CHF", "NZD": 1.69,
"rates": 1.1400000000000001
}], "PHP": 57.8,
"ISK": [
{ "SGD": 1.52,
"iso": "ISK",
"rates": 136.6 "THB": 35.7,
}],
"NOK": [ "ZAR": 16.2,
{
"iso": "NOK", "EUR": 1
"rates": 9.78
}],
"HRK": [
{
"iso": "HRK",
"rates": 7.41
}],
"RUB": [
{
"iso": "RUB",
"rates": 72.87
}],
"TRY": [
{
"iso": "TRY",
"rates": 6.66
}],
"AUD": [
{
"iso": "AUD",
"rates": 1.5899999999999999
}],
"BRL": [
{
"iso": "BRL",
"rates": 4.42
}],
"CAD": [
{
"iso": "CAD",
"rates": 1.5
}],
"CNY": [
{
"iso": "CNY",
"rates": 7.51
}],
"HDK": [
{
"iso": "HKD",
"rates": 8.75
}],
"IDR": [
{
"iso": "IDR",
"rates": 15901.45
}],
"ILS": [
{
"iso": "ILS",
"rates": 4.01
}],
"INR": [
{
"iso": "INR",
"rates": 77.26
}],
"KRW": [
{
"iso": "KRW",
"rates": 1305.08
}],
"MXN": [
{
"iso": "MXN",
"rates": 21.34
}],
"MYR": [
{
"iso": "MYR",
"rates": 4.62
}],
"NZD": [
{
"iso": "NZD",
"rates": 1.69
}],
"PHP": [
{
"iso": "PHP",
"rates": 57.86
}],
"SGD": [
{
"iso": "SGD",
"rates": 1.52
}],
"THB": [
{
"iso": "THB",
"rates": 35.72
}],
"ZAR": [
{
"iso": "ZAR",
"rates": 16.21
}],
"EUR": [
{
"iso": "EUR",
"rates": 1
}]
} }
$.getJSON( "/currency.json", function( data ) { $("#go").click( function()
{
$('#From').attr('id', 'To');
$('#To').attr('id', 'From');
}
);
$.ajax({
url: "/api",
dataType: 'json',
success: function(data){
var From; var From;
var To; var To;
var Amount; var Amount;
var From_iso_value; var From_iso_value;
var To_iso_value; var To_iso_value;
// $('#From').on("change",function(){
// From = $("#From option:selected").attr('value');
// return data[From][0]['rates'];
// });
$('#Form').on("change",function(){ $('#Form').on("change",function(){
Amount = document.getElementById("Amount").value; Amount = document.getElementById("Amount").value;
From_iso_value = $("#From option:selected").attr('value'); From_iso_value = $("#From option:selected").attr('value');
From = data[From_iso_value][0]['rates']; From = data[From_iso_value];
To_iso_value = $("#To option:selected").attr('value'); To_iso_value = $("#To option:selected").attr('value');
To = data[To_iso_value][0]['rates']; To = data[To_iso_value];
console.log((Amount/From) * To); console.log((Amount/From) * To);
}); });
}
}); });
\ No newline at end of file
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
@endforeach @endforeach
</select> </select>
<br> <br>
<input type="button" id="go" onclick="swapValues()" value="Swap"> <input type="button" id="go" value="Swap">
<br> <br>
<select class="form-control" id="To" name="To"> <select class="form-control" id="To" name="To">
@foreach ($currencies as $item) @foreach ($currencies as $item)
...@@ -54,6 +54,14 @@ ...@@ -54,6 +54,14 @@
document.getElementById("From").value = document.getElementById("To").value; document.getElementById("From").value = document.getElementById("To").value;
document.getElementById("To").value = tmp; document.getElementById("To").value = tmp;
} }
</script> --}}
{{-- <script>
function swapValues(){
var tmp = document.getElementById("From").value;
document.getElementById("From").value = document.getElementById("To").value;
document.getElementById("To").value = tmp;
}
var request = new XMLHttpRequest() var request = new XMLHttpRequest()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment