fixed convert currency response message

This commit is contained in:
vedatakd 2021-01-04 17:10:03 +03:00
parent 4d64d281ea
commit fdb162fdc7
3 changed files with 36 additions and 20 deletions

View File

@ -1,9 +1,9 @@
<?php namespace Visiosoft\AdvsModule\Adv;
use Anomaly\Streams\Platform\Image\Command\MakeImageInstance;
use Anomaly\Streams\Platform\Message\MessageBag;
use Anomaly\Streams\Platform\Model\Advs\AdvsCustomFieldsEntryModel;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Visiosoft\AdvsModule\Adv\Contract\AdvInterface;
@ -112,10 +112,9 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
public function foreignCurrency($currency, $price, $isUpdate, $settings)
{
$currencies = setting_value('visiosoft.module.advs::enabled_currencies');
$messages = app(MessageBag::class);
$foreign_currency = array();
$client = new Client();
foreach ($currencies as $currencyIn) {
if ($currencyIn == $currency) {
$foreign_currency[$currency] = (int)$price;
@ -123,8 +122,14 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
try {
$url = $currency . "_" . $currencyIn;
$freeCurrencyKey = $settings->value('visiosoft.module.advs::free_currencyconverterapi_key');
$response = $client->get('http://free.currencyconverterapi.com/api/v6/convert?q='
. $url . '&compact=y&apiKey=' . $freeCurrencyKey);
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://free.currencyconverterapi.com/api/v6/convert', ['query' => [
'q' => $url,
'compact' => 'y',
'apiKey' => $freeCurrencyKey
]]);
if ($response->getStatusCode() == '200') {
$response = (array)\GuzzleHttp\json_decode($response->getBody()->getContents());
if (!empty($response)) {
@ -132,8 +137,11 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
$foreign_currency[$currencyIn] = $price * $rate;
}
}
} catch (\Exception $e) {
$this->messages->error((!is_null($e->getMessage())) ? $e->getMessage() : trans('streams::error.500.message'));
} catch (\GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$response = json_decode($responseBodyAsString, true);
$messages->error($response['error']);
}
}
}

View File

@ -102,20 +102,26 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
if (!empty($param['user'])) {
$query = $query->where('advs_advs.created_by_id', $param['user']);
}
$currency = setting_value('streams::currency');
if (!empty($param['currency'])) {
if (!empty($param['min_price'])) {
$num = $param['min_price'];
$int = (int)$num;
$column = "JSON_EXTRACT(foreign_currencies, '$." . $param['currency'] . "') >=" . $int;
$query = $query->whereRaw($column);
}
if (!empty($param['max_price'])) {
$num = $param['max_price'];
$int = (int)$num;
$column = "JSON_EXTRACT(foreign_currencies, '$." . $param['currency'] . "') <=" . $int;
$query = $query->whereRaw($column);
}
$currency = $param['currency'];
}
if (!empty($param['min_price'])) {
$num = $param['min_price'];
$int = (int)$num;
$column = "JSON_EXTRACT(foreign_currencies, '$." . $currency . "') >= " . $int;
$query = $query->whereRaw($column);
}
if (!empty($param['max_price'])) {
$num = $param['max_price'];
$int = (int)$num;
$column = "JSON_EXTRACT(foreign_currencies, '$." . $currency . "') <= " . $int;
$query = $query->whereRaw($column);
}
if (!empty($param['date'])) {
if ($param['date'] === 'day') {
$query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subDay());

View File

@ -13,7 +13,9 @@ class ConvertCurrency extends ActionHandler
foreach ($selected as $id) {
$entry = $model->newQuery()->find($id);
$model->foreignCurrency($entry->currency, $entry->price, $id, $settingRepository);
if ($entry) {
$model->foreignCurrency($entry->currency, $entry->price, $id, $settingRepository);
}
}
if ($selected) {
$this->messages->success(trans('visiosoft.module.advs::message.currency_converted'));