Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

igarakatidus/exchange-rate

0 stars
2 code files
View igarakatidus/exchange-rate on GitHub

app/Http/Livewire/Conversion.php

Open in GitHub
use App\Exceptions\ExchangeRateException;
use App\Models\Conversion as ConversionModel;
use App\Services\Contract\ExchangeRate as ExchangeRateContract;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Component;
 
class Conversion extends Component
{
private const ALLOWED_SYMBOLS = ['INR', 'EUR'];
 
public string $from;
 
public string $to;
 
public ?float $result;
 
public array $currencies;
 
protected array $rules = [
'from' => 'required|string|size:3',
'to' => 'required|string|size:3',
'result' => 'nullable|numeric',
];
 
public Collection $conversions;
 
public function mount(ExchangeRateContract $service)
{
try {
$symbols = $service->getAllowedCurrencies();
$this->currencies = array_filter($symbols, fn ($symbol) => in_array($symbol, self::ALLOWED_SYMBOLS));
$this->conversions = $this->getConversions();
} catch (ExchangeRateException $e) {
session()->flash('api_error', $e->getMessage());
}
}
 
public function render()
{
return view('livewire.conversion')
->extends('layouts.app')
->section('content');
}
 
public function convert(ExchangeRateContract $service)
{
$this->result = null;
$this->validate();
try {
$this->result = $service->convert($this->from, $this->to);
} catch (ExchangeRateException $e) {
session()->flash('api_error', $e->getMessage());
}
 
if($this->result){
ConversionModel::updateOrCreate([
'from' => $this->from,
'to' => $this->to,
'conversion_date' => now()->format('Y-m-d'),
], [
'result' => $this->result,
]);
}
}
 
private function getConversions($limit = null)
{
$limit ??= (int) config('app.pagination_limit', 10);
return ConversionModel::take($limit)->orderBy('conversion_date', 'desc')->get();
}
}

resources/views/livewire/conversion.blade.php

Open in GitHub
<div>
<form class="form pb-3" wire:submit.prevent="convert">
<div class="mb-3">
<label class="form-label">From</label>
<select class="form-control" wire:model="from" type="text" name="from">
<option selected="selected">(Select)</option>
@foreach($currencies as $currency)
<option value="{{$currency}}">{{$currency}}</option>
@endforeach
</select>
@error('from') <div class="form-text text-danger">{{ $message }}</div> @enderror
</div>
<div class="mb-3">
<label class="form-label">To</label>
<select class="form-control" wire:model="to" type="text" name="to" value="">
<option selected="selected">(Select)</option>
@foreach($currencies as $currency)
<option value="{{$currency}}">{{$currency}}</option>
@endforeach
</select>
@error('to') <div class="form-text text-danger">{{ $message }}</div> @enderror
</div>
<button class="btn btn-primary" type="submit">Convert</button>
</form>
 
@if ($result)
<div class="alert alert-success">{{ $result }}</div>
@endif
 
@if (session()->has('api_error'))
<div class="alert alert-danger">
{{ session('api_error') }}
</div>
@endif
 
<div>
<table class="table">
<thead>
<tr>
<th scope="col">From</th>
<th scope="col">To</th>
<th scope="col">Rate</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
@foreach($conversions as $conversion)
<tr>
<td scope="row">{{ $conversion->from }}</th>
<td>{{ $conversion->to }}</td>
<td>{{ $conversion->result }}</td>
<td>{{ $conversion->conversion_date->format('Y-m-d') }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.