Files
exchange-2.0/app/Classes/Modules/Currencies/ControllerLogic/CreateCurrencyLogic.php
T
CheeSiong dec78bf8d7 Create Currency Logic Class
Update Currency Selling Logic Class
Delete Currency Selling Logic Class
Create Currency Log Service Class
2020-11-29 12:39:36 +08:00

87 lines
2.6 KiB
PHP

<?php
namespace App\Classes\Modules\Currencies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Currencies\Standards\Rules\CanCreateCurrency;
use App\Classes\Modules\Currencies\Services\CreatesCurrency;
use App\Classes\Modules\Currencies\Services\CreatesCurrencyLog;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyObject;
use App\Http\Resources\CurrencyResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CreateCurrencyLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Created Currency',
'message' => 'You have successfully created a new Currency'
];
}
/** @var CanCreateCurrency */
private $canCreateCurrency;
/** @var CreatesCurrency */
private $createsCurrency;
/** @var CreatesCurrencyLog */
private $createsCurrencyLog;
/**
* CreateSegmentLogic constructor.
* @param CanCreateCurrency $canCreateCurrency
* @param CreatesCurrency $createsCurrency
* @param CreatesCurrencyLog $createsCurrencyLog
*/
public function __construct(
CanCreateCurrency $canCreateCurrency,
CreatesCurrency $createsCurrency,
CreatesCurrencyLog $createsCurrencyLog
)
{
$this->canCreateCurrency = $canCreateCurrency;
$this->createsCurrency = $createsCurrency;
$this->createsCurrencyLog = $createsCurrencyLog;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
try {
DB::beginTransaction();
$currency_object = new CurrencyObject(
$request->input('country_id'),
$request->input('name'),
$request->input('short_code'),
$request->input('symbol'),
number_format( (float) $request->input('selling'), 5, '.', '')
);
$this->canCreateCurrency->passes($currency_object);
$currency_query = $this->createsCurrency->execute($currency_object);
$currency_log_query = $this->createsCurrencyLog->execute($currency_query);
DB::commit();
return $this->resourceResponse(new CurrencyResource($currency_query));
} catch (\Exception $exception) {
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}