This commit is contained in:
CheeSiong
2020-12-04 09:58:07 +08:00
12 changed files with 341 additions and 0 deletions
@@ -0,0 +1,65 @@
<?php
namespace App\Classes\Modules\Currencies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Currencies\Services\ListsCurrency;
use App\Classes\Modules\Currencies\Standards\Rules\CanListCurrency;
use App\Http\Resources\CurrencyResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ListCurrencyLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Retrieved Currency',
'message' => 'You have successfully retrieved a list of Currency'
];
}
/** @var CanListCurrency */
private $canListCurrency;
/** @var ListsCurrency */
private $listsCurrency;
/**
* ListCurrencyLogic constructor.
* @param CanListCurrency $canListCurrency
* @param ListsCurrency $listsCurrency
*/
public function __construct(CanListCurrency $canListCurrency, ListsCurrency $listsCurrency)
{
$this->canListCurrency = $canListCurrency;
$this->listsCurrency = $listsCurrency;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
try {
$this->canListCurrency->passes();
$query = $this->listsCurrency->execute();
return $this->collectionResponse(CurrencyResource::collection($query));
} catch (\Exception $exception){
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}
@@ -0,0 +1,66 @@
<?php
namespace App\Classes\Modules\Currencies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Currencies\Services\FetchesCurrency;
use App\Classes\Modules\Currencies\Services\RateCalculatesCurrency;
use App\Http\Resources\RateCalculateCurrencyResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class RateCalculateCurrencyLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Rate Calculated Currency',
'message' => 'You have successfully rate calculated a Currency'
];
}
/** @var FetchesCurrency */
private $fetchesCurrency;
/** @var RateCalculatesCurrency */
private $rateCalculatesCurrency;
/**
* @param CanListStandardSegmentConstants $canListStandardSegmentConstants
* @param FetchesCurrency $fetchesCurrency
* @param RateCalculatesCurrency $rateCalculatesCurrency
*/
public function __construct(FetchesCurrency $fetchesCurrency, RateCalculatesCurrency $rateCalculatesCurrency)
{
$this->fetchesCurrency = $fetchesCurrency;
$this->rateCalculatesCurrency = $rateCalculatesCurrency;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
try {
$amount = $request->input('amount');
$conversion_currency = $this->fetchesCurrency->execute(['id' => $request->input('conversion_currency_id')]);
$convertable_currency = $this->fetchesCurrency->execute(['id' => $request->input('convertable_currency_id')]);
$convert_amount = $this->rateCalculatesCurrency->execute($conversion_currency, $convertable_currency, $amount);
return $this->resourceResponse(new RateCalculateCurrencyResource($conversion_currency, $convertable_currency, $amount));
} catch (\Exception $exception){
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}
@@ -0,0 +1,33 @@
<?php
namespace App\Classes\Modules\Currencies\Services;
use App\Classes\General\Eloquent\AbstractListRecord;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Currency;
class ListsCurrency extends AbstractListRecord
{
/** @var Currency */
private $repository;
/**
* ListsCurrency constructor.
* @param Currency $repository
*/
public function __construct(Currency $repository)
{
$this->repository = $repository;
}
/**
* @return Builder
*/
function getRepository(): Builder
{
return $this->repository->newQuery();
}
}
@@ -0,0 +1,15 @@
<?php
namespace App\Classes\Modules\Currencies\Services;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Models\Currency;
class RateCalculatesCurrency extends AbstractUpdateRecord
{
public function execute(Currency $conversion_currency, Currency $convertable_currency, $amount)
{
return $convert_amount = ($conversion_currency->selling / $convertable_currency->selling) * $amount;
}
}
@@ -0,0 +1,45 @@
<?php
namespace App\Classes\Modules\Currencies\Standards\Rules;
use App\Classes\General\Abstracts\AbstractRule;
use App\Classes\Modules\Segments\DataTransferObjects\CurrencyObject;
class CanListCurrency extends AbstractRule
{
/**
* @return bool
*/
protected function authorized(): bool
{
// TODO Set Authorization rules
if (!\Auth::user()->can('view currency')) {
return false;
}
return true;
}
/**
* @param CurrencyObject $object
* @return bool
*/
protected function validators($object): bool
{
return true;
}
/**
* @param CurrencyObject $object
* @return bool
*/
protected function criteria($object): bool
{
return true;
}
}