This commit is contained in:
71
api/app/Services/Service.php
Normal file
71
api/app/Services/Service.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
||||
|
||||
abstract class Service
|
||||
{
|
||||
protected static function get($model, $id = null)
|
||||
{
|
||||
$model = $model::query();
|
||||
$query = request()->query();
|
||||
|
||||
if (isset($query['with'])) {
|
||||
$with = is_array($query['with']) ? $query['with'] : [$query['with']];
|
||||
|
||||
foreach ($with as $value) {
|
||||
$model->with($value);
|
||||
}
|
||||
}
|
||||
|
||||
return is_null($id) ? $model : $model->findOrFail($id);
|
||||
}
|
||||
|
||||
protected static function getWhereFromQueryString($fields)
|
||||
{
|
||||
$where = [];
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$operator = '';
|
||||
$fld = $field;
|
||||
|
||||
if (is_array($field)) {
|
||||
$fld = $field[0];
|
||||
$operator = $field[1];
|
||||
}
|
||||
|
||||
$query = request()->query();
|
||||
|
||||
if (!array_key_exists($fld, $query)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$vlr = $query[$fld];
|
||||
|
||||
if ($vlr === 'true') $vlr = 1;
|
||||
if ($vlr === 'false') $vlr = 0;
|
||||
if ($vlr === 'null') $vlr = null;
|
||||
|
||||
$fld = $fld === 'not_id' ? 'id' : $fld;
|
||||
|
||||
if ($operator === "")
|
||||
$where[] = [$field, $vlr];
|
||||
else
|
||||
$where[] = [$fld, $operator, $vlr];
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
protected static function getCurrentLotacao()
|
||||
{
|
||||
return auth()->check() ? Lotacao::findOrFail(auth()->payload()->get('lotacao_id')) : null;
|
||||
}
|
||||
|
||||
protected static function getCurrentCentroCusto()
|
||||
{
|
||||
return auth()->check() ? CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id')) : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user