add profile
This commit is contained in:
parent
65045c8656
commit
2fd0c7bf70
@ -2,29 +2,60 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\OverviewResource;
|
||||
use App\Repositories\Pld\PldContractInterface;
|
||||
use App\Traits\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class PldController extends Controller
|
||||
{
|
||||
|
||||
use ApiResponse;
|
||||
|
||||
public function __construct(
|
||||
protected PldContractInterface $pldContract
|
||||
){}
|
||||
|
||||
public function overviewByRegion()
|
||||
{
|
||||
|
||||
dd('oii');
|
||||
try {
|
||||
$response = $this->pldContract->getOverviewByRegion();
|
||||
return (new OverviewResource($response))
|
||||
->response()
|
||||
->setStatusCode(Response::HTTP_OK);
|
||||
}catch (\Exception $ex){
|
||||
return $this->errorResponse(false, $ex->getMessage(), 404);
|
||||
}
|
||||
}
|
||||
|
||||
public function listConsumption()
|
||||
public function listConsumption(Request $request)
|
||||
{
|
||||
dd('oii');
|
||||
try {
|
||||
$response = $this->pldContract->getListConsumption($request->all());
|
||||
return response()->json($response, 200);
|
||||
}catch (\Exception $ex){
|
||||
return $this->errorResponse(false, $ex->getMessage(), 404);
|
||||
}
|
||||
}
|
||||
|
||||
public function consumptionByDaily()
|
||||
public function consumptionByDaily(Request $request)
|
||||
{
|
||||
dd('oii');
|
||||
try {
|
||||
$response = $this->pldContract->getConsumptionByDaily($request->all());
|
||||
return response()->json($response, 200);
|
||||
}catch (\Exception $ex){
|
||||
return $this->errorResponse(false, $ex->getMessage(), 404);
|
||||
}
|
||||
}
|
||||
|
||||
public function consumptionBySchedule()
|
||||
public function consumptionBySchedule(Request $request)
|
||||
{
|
||||
dd('oii');
|
||||
try {
|
||||
$response = $this->pldContract->getConsumptionBySchedule($request->all());
|
||||
return response()->json($response, 200);
|
||||
}catch (\Exception $ex){
|
||||
return $this->errorResponse(false, $ex->getMessage(), 404);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
app/Http/Resources/OverviewResource.php
Normal file
19
app/Http/Resources/OverviewResource.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OverviewResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
@ -11,4 +12,22 @@ use OwenIt\Auditing\Auditable;
|
||||
class Faq extends Model implements Auditing
|
||||
{
|
||||
use HasFactory, SoftDeletes, Auditable;
|
||||
|
||||
protected $table = 'faqs';
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $fillable = [
|
||||
'question',
|
||||
'answer',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
protected function serializeDate(DateTimeInterface $date): string
|
||||
{
|
||||
return $date->format('d/m/Y H:i:s');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -12,4 +12,8 @@ use OwenIt\Auditing\Auditable;
|
||||
class Pld extends Model implements Auditing
|
||||
{
|
||||
use HasFactory, SoftDeletes, Auditable;
|
||||
|
||||
protected $table = "pld";
|
||||
|
||||
protected $guarded = ['id'];
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Support\FilterBuilder\FiltersQuery;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@ -11,7 +12,7 @@ use Illuminate\Support\Traits\ForwardsCalls;
|
||||
|
||||
abstract class AbstractRepository
|
||||
{
|
||||
use MethodsTrait, ForwardsCalls;
|
||||
use MethodsTrait, ForwardsCalls, FiltersQuery;
|
||||
|
||||
protected AbstractRepository|Model $model;
|
||||
|
||||
@ -55,5 +56,4 @@ abstract class AbstractRepository
|
||||
|
||||
return (new static)->$model;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -4,10 +4,7 @@ namespace App\Repositories\Economy;
|
||||
|
||||
use App\Models\Economy;
|
||||
use App\Repositories\AbstractRepository;
|
||||
use App\Repository\Repository;
|
||||
use App\Support\FilterQueryBuilder;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Support\FilterBuilder\FilterQueryBuilder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EconomyRepository extends AbstractRepository implements EconomyContractInterface
|
||||
|
||||
@ -6,5 +6,8 @@ use App\Repositories\ContractInterface;
|
||||
|
||||
interface PldContractInterface extends ContractInterface
|
||||
{
|
||||
|
||||
public function getOverviewByRegion();
|
||||
public function getConsumptionByDaily($params);
|
||||
public function getListConsumption($params);
|
||||
public function getConsumptionBySchedule($params);
|
||||
}
|
||||
@ -6,6 +6,11 @@ namespace App\Repositories\Pld;
|
||||
|
||||
use App\Models\Pld;
|
||||
use App\Repositories\AbstractRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PldRepository extends AbstractRepository implements PldContractInterface
|
||||
{
|
||||
@ -14,4 +19,77 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
||||
parent::__construct($pld);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
private function execute($fields, $params): Builder
|
||||
{
|
||||
$query = $this->model->select($fields);
|
||||
|
||||
$query = static::getFilterBuilder($params)->applyFilter($query);
|
||||
|
||||
if (!empty($params)) {
|
||||
if ($params['field'] === 'mes_ref' && $params['value']) {
|
||||
$query = $query->where(
|
||||
DB::raw("TO_CHAR(TO_DATE(pld.{$params['field']}, 'YYMM'), 'MM/YYYY')"),
|
||||
$params['type'],
|
||||
$params['value']);
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function getOverviewByRegion(): Collection|array
|
||||
{
|
||||
$fields = [
|
||||
'pld.submercado as submarket',
|
||||
'pld.mes_ref as year_month',
|
||||
DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY') as year_month_formatted"),
|
||||
DB::raw("SUM(pld.valor) as value")
|
||||
];
|
||||
|
||||
$params = ["type" => "=", "field" => 'mes_ref', "value" => Carbon::now()->format('m/Y')];
|
||||
|
||||
return $this->execute($fields, $params)
|
||||
->groupBy(['submarket', 'year_month', 'year_month_formatted'])
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getListConsumption($params)
|
||||
{
|
||||
// TODO: Implement getListConsumption() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function getConsumptionByDaily($params): Collection|array
|
||||
{
|
||||
$fields = static::getRowField();
|
||||
|
||||
return $this->execute($fields, $params)->get();
|
||||
|
||||
}
|
||||
|
||||
public function getConsumptionBySchedule($params)
|
||||
{
|
||||
$fields = static::getRowField();
|
||||
|
||||
return $this->execute($fields, $params)->toSql();
|
||||
}
|
||||
|
||||
protected static function getRowField(): array
|
||||
{
|
||||
return [
|
||||
DB::raw("TO_CHAR((date('1899-12-30') + interval '1' day * pld.dia_num), 'DD') as day_formatted"),
|
||||
DB::raw("(date('1899-12-30') + interval '1' day * pld.dia_num) as day_calc"),
|
||||
'pld.submercado as submarket',
|
||||
'pld.mes_ref as year_month',
|
||||
DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY') as year_month_formatted"),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
76
app/Support/FilterBuilder/Entity/FilterItem.php
Normal file
76
app/Support/FilterBuilder/Entity/FilterItem.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\FilterBuilder\Entity;
|
||||
|
||||
use App\Support\FilterBuilder\EntityJson;
|
||||
use Illuminate\Database\Query\Expression;
|
||||
|
||||
class FilterItem extends EntityJson
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $type = "=";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $field;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $value;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $row = false;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getRow(): bool
|
||||
{
|
||||
return $this->row;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $row
|
||||
*/
|
||||
public function setRow(bool $row): void
|
||||
{
|
||||
$this->row = $row;
|
||||
}
|
||||
|
||||
public function getType() : string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function getField() :Expression|string
|
||||
{
|
||||
return $this->field;
|
||||
}
|
||||
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function setType(string $type)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function setField($field): void
|
||||
{
|
||||
$this->field = $field;
|
||||
}
|
||||
|
||||
public function setValue($value): void
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
42
app/Support/FilterBuilder/Entity/OrderItem.php
Normal file
42
app/Support/FilterBuilder/Entity/OrderItem.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\FilterBuilder\Entity;
|
||||
|
||||
use App\Support\FilterBuilder\EntityJson;
|
||||
|
||||
/**
|
||||
* OrderItem
|
||||
*
|
||||
* Objeto utilizado para base dos filtros, por padrão ele possui um estrutura json nesse formato
|
||||
* { field: "id_produto", direction: "asc" }
|
||||
* @author renan
|
||||
*/
|
||||
|
||||
class OrderItem extends EntityJson
|
||||
{
|
||||
|
||||
protected string $field;
|
||||
|
||||
protected string $direction = "asc";
|
||||
|
||||
|
||||
public function getField() : string
|
||||
{
|
||||
return $this->field;
|
||||
}
|
||||
|
||||
public function setField(string $field)
|
||||
{
|
||||
$this->field = $field;
|
||||
}
|
||||
|
||||
public function getDirection() : string
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public function setDirection(string $direction = "asc")
|
||||
{
|
||||
$this->direction = $direction;
|
||||
}
|
||||
}
|
||||
94
app/Support/FilterBuilder/EntityJson.php
Normal file
94
app/Support/FilterBuilder/EntityJson.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\FilterBuilder;
|
||||
|
||||
|
||||
abstract class EntityJson implements \JsonSerializable
|
||||
{
|
||||
|
||||
public function jsonToObject(\stdClass $jsonData)
|
||||
{
|
||||
$vars = get_object_vars($jsonData);
|
||||
foreach ($vars as $key => $value) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function jsonSerialize(): \stdClass
|
||||
{
|
||||
$vars = get_object_vars($this);
|
||||
$obj = new \stdClass();
|
||||
foreach ($vars as $key => $value) {
|
||||
$obj->$key = $value;
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function jsonSetObject(\stdClass $jsonData)
|
||||
{
|
||||
$vars = get_object_vars($jsonData);
|
||||
foreach ($vars as $key => $value) {
|
||||
if (is_array($value)){
|
||||
$campo = strtolower($key);
|
||||
$method = "set" . ucfirst($campo);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
}
|
||||
} else{
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
$vars = get_object_vars($this);
|
||||
$result = array();
|
||||
foreach ($vars as $key => $value) {
|
||||
$method = "get" . ucfirst($key);
|
||||
$key = strtoupper($key);
|
||||
$valor = $value;
|
||||
if (method_exists($this, $method)) {
|
||||
$valor = $this->$method();
|
||||
}
|
||||
$result[$key] = $valor;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function arrayObjectCast(array $arrayOfObject, $className)
|
||||
{
|
||||
$arr = [];
|
||||
if (!empty($arrayOfObject)) {
|
||||
$obj = $arrayOfObject[0];
|
||||
if (is_array($obj) && count(array_filter(array_keys($obj), 'is_string')) > 0) {
|
||||
$obj = (object) $obj;
|
||||
}
|
||||
if (get_class($obj) == "stdClass") {
|
||||
foreach ($arrayOfObject as $item) {
|
||||
$e = new $className();
|
||||
if (method_exists($e, 'jsonSetObject')) {
|
||||
$e->jsonSetObject($item);
|
||||
$arr[] = $e;
|
||||
}
|
||||
}
|
||||
} elseif (get_class($obj) == $className) {
|
||||
return $arrayOfObject;
|
||||
}
|
||||
}
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
public function __get(string $name)
|
||||
{
|
||||
return $this->{$name};
|
||||
}
|
||||
|
||||
public function __set(string $name, $value): void
|
||||
{
|
||||
$this->{$name} = $value;
|
||||
}
|
||||
}
|
||||
71
app/Support/FilterBuilder/FilterQueryBuilder.php
Normal file
71
app/Support/FilterBuilder/FilterQueryBuilder.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\FilterBuilder;
|
||||
|
||||
use App\Support\FilterBuilder\Entity\FilterItem;
|
||||
use App\Support\FilterBuilder\Entity\OrderItem;
|
||||
use App\Support\FilterBuilder\Interfaces\IFilterBuilder;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class FilterQueryBuilder extends EntityJson implements IFilterBuilder
|
||||
{
|
||||
|
||||
protected array $filters = [];
|
||||
|
||||
protected array $order = [];
|
||||
|
||||
public function applyFilter(Builder $builder): Builder
|
||||
{
|
||||
if (!empty($this->getFilters())) {
|
||||
|
||||
foreach ($this->getFilters() as $filter) {
|
||||
|
||||
$builder = FilterType::filter($builder, $filter);
|
||||
}
|
||||
}
|
||||
|
||||
$builder->limit($this->limit);
|
||||
$builder->offset($this->offset);
|
||||
|
||||
foreach ($this->getOrder() as $order) {
|
||||
$builder->orderBy($order->getField(), $order->getDirection());
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFilters(): array
|
||||
{
|
||||
return $this->filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $filters
|
||||
*/
|
||||
public function setFilters(array $filters): void
|
||||
{
|
||||
$this->filters = $this->arrayObjectCast($filters, FilterItem::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getOrder(): array
|
||||
{
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $order
|
||||
*/
|
||||
public function setOrder(array $order): void
|
||||
{
|
||||
$this->order = $this->arrayObjectCast($order,OrderItem::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
63
app/Support/FilterBuilder/FilterType.php
Normal file
63
app/Support/FilterBuilder/FilterType.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\FilterBuilder;
|
||||
|
||||
use App\Support\FilterBuilder\Entity\FilterItem;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FilterType
|
||||
{
|
||||
const WHERE_FILTER = [
|
||||
"=",
|
||||
"<>",
|
||||
">",
|
||||
">=",
|
||||
"<",
|
||||
"<=",
|
||||
"like",
|
||||
"not_like"
|
||||
];
|
||||
|
||||
const IN_FILTER = [
|
||||
"in",
|
||||
"not_in"
|
||||
];
|
||||
|
||||
const BETWEEN_FILTER = [
|
||||
"between",
|
||||
"not_between"
|
||||
];
|
||||
|
||||
const NULL_FILTER = [
|
||||
"null",
|
||||
"not_null"
|
||||
];
|
||||
|
||||
public static function filter(Builder $builder, FilterItem $filter) : Builder
|
||||
{
|
||||
|
||||
if (in_array($filter->getType(), self::WHERE_FILTER))
|
||||
{
|
||||
return static::makeWhereFilter($builder, $filter);
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public static function makeWhereFilter(Builder $builder, FilterItem $filter): Builder
|
||||
{
|
||||
$fType = $filter->getType();
|
||||
|
||||
if ($fType === 'not_like') {
|
||||
$fType = 'not like';
|
||||
}
|
||||
|
||||
$field = ($filter->getRow()) ? DB::raw($filter->getField()) : $filter->getField();
|
||||
|
||||
return $builder->where($field, $fType, $filter->getValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
36
app/Support/FilterBuilder/FiltersQuery.php
Normal file
36
app/Support/FilterBuilder/FiltersQuery.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\FilterBuilder;
|
||||
|
||||
use Exception;
|
||||
|
||||
trait FiltersQuery
|
||||
{
|
||||
|
||||
public function getJsonObject($json, $className)
|
||||
{
|
||||
try {
|
||||
$jsonData = json_decode(json_encode($json), false);
|
||||
|
||||
$obj = new $className;
|
||||
|
||||
if (!isset($jsonData) && method_exists($obj, 'jsonToObject'))
|
||||
{
|
||||
throw new Exception("Request inválido");
|
||||
}
|
||||
|
||||
$obj->jsonSetObject($jsonData);
|
||||
|
||||
return $obj;
|
||||
|
||||
} catch (Exception $ex) {
|
||||
return $ex->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function getFilterBuilder($json): ?FilterQueryBuilder
|
||||
{
|
||||
return $this->getJsonObject($json, FilterQueryBuilder::class);
|
||||
}
|
||||
|
||||
}
|
||||
10
app/Support/FilterBuilder/Interfaces/IFilterBuilder.php
Normal file
10
app/Support/FilterBuilder/Interfaces/IFilterBuilder.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\FilterBuilder\Interfaces;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
interface IFilterBuilder
|
||||
{
|
||||
public function applyFilter(Builder $builder) : Builder;
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FilterQueryBuilder
|
||||
{
|
||||
protected $request;
|
||||
|
||||
|
||||
public function __construct($request)
|
||||
{
|
||||
|
||||
$this->initializeRequest($request);
|
||||
|
||||
}
|
||||
|
||||
// public function __construct($class)
|
||||
// {
|
||||
// $this->class = $class;
|
||||
//
|
||||
// return $this;
|
||||
// }
|
||||
|
||||
public static function for($request)
|
||||
{
|
||||
|
||||
return (new static($request));
|
||||
}
|
||||
|
||||
|
||||
protected function initializeRequest(?Request $request = null): static
|
||||
{
|
||||
|
||||
$this->request = $request
|
||||
? QueryBuilderRequest::fromRequest($request)
|
||||
: app(QueryBuilderRequest::class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
return $this->class->{$name};
|
||||
}
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->class->{$name} = $value;
|
||||
}
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
trait FiltersQuery
|
||||
{
|
||||
|
||||
}
|
||||
@ -1,201 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class QueryBuilderRequest extends Request
|
||||
{
|
||||
private static $includesArrayValueDelimiter = ',';
|
||||
|
||||
private static $appendsArrayValueDelimiter = ',';
|
||||
|
||||
private static $fieldsArrayValueDelimiter = ',';
|
||||
|
||||
private static $sortsArrayValueDelimiter = ',';
|
||||
|
||||
private static $filterArrayValueDelimiter = ',';
|
||||
|
||||
public static function setArrayValueDelimiter(string $delimiter): void
|
||||
{
|
||||
static::$filterArrayValueDelimiter = $delimiter;
|
||||
static::$includesArrayValueDelimiter = $delimiter;
|
||||
static::$appendsArrayValueDelimiter = $delimiter;
|
||||
static::$fieldsArrayValueDelimiter = $delimiter;
|
||||
static::$sortsArrayValueDelimiter = $delimiter;
|
||||
}
|
||||
|
||||
public static function fromRequest(Request $request): self
|
||||
{
|
||||
return static::createFrom($request, new self());
|
||||
}
|
||||
|
||||
public function includes(): Collection
|
||||
{
|
||||
$includeParameterName = config('query-builder.parameters.include');
|
||||
|
||||
$includeParts = $this->getRequestData($includeParameterName);
|
||||
|
||||
if (is_string($includeParts)) {
|
||||
$includeParts = explode(static::getIncludesArrayValueDelimiter(), $includeParts);
|
||||
}
|
||||
|
||||
return collect($includeParts)->filter();
|
||||
}
|
||||
|
||||
public function appends(): Collection
|
||||
{
|
||||
$appendParameterName = config('query-builder.parameters.append');
|
||||
|
||||
$appendParts = $this->getRequestData($appendParameterName);
|
||||
|
||||
if (! is_array($appendParts) && ! is_null($appendParts)) {
|
||||
$appendParts = explode(static::getAppendsArrayValueDelimiter(), $appendParts);
|
||||
}
|
||||
|
||||
return collect($appendParts)->filter();
|
||||
}
|
||||
|
||||
public function fields(): Collection
|
||||
{
|
||||
$fieldsParameterName = config('query-builder.parameters.fields');
|
||||
|
||||
$fieldsPerTable = collect($this->getRequestData($fieldsParameterName));
|
||||
|
||||
if ($fieldsPerTable->isEmpty()) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return $fieldsPerTable->map(function ($fields) {
|
||||
return explode(static::getFieldsArrayValueDelimiter(), $fields);
|
||||
});
|
||||
}
|
||||
|
||||
public function sorts(): Collection
|
||||
{
|
||||
$sortParameterName = config('query-builder.parameters.sort');
|
||||
|
||||
$sortParts = $this->getRequestData($sortParameterName);
|
||||
|
||||
if (is_string($sortParts)) {
|
||||
$sortParts = explode(static::getSortsArrayValueDelimiter(), $sortParts);
|
||||
}
|
||||
|
||||
return collect($sortParts)->filter();
|
||||
}
|
||||
|
||||
public function filters(): Collection
|
||||
{
|
||||
$filterParameterName = config('query-builder.parameters.filter');
|
||||
|
||||
$filterParts = $this->getRequestData($filterParameterName, []);
|
||||
|
||||
if (is_string($filterParts)) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$filters = collect($filterParts);
|
||||
|
||||
return $filters->map(function ($value) {
|
||||
return $this->getFilterValue($value);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
protected function getFilterValue($value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return collect($value)->map(function ($valueValue) {
|
||||
return $this->getFilterValue($valueValue);
|
||||
})->all();
|
||||
}
|
||||
|
||||
if (Str::contains($value, static::getFilterArrayValueDelimiter())) {
|
||||
return explode(static::getFilterArrayValueDelimiter(), $value);
|
||||
}
|
||||
|
||||
if ($value === 'true') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($value === 'false') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function getRequestData(?string $key = null, $default = null)
|
||||
{
|
||||
if (config('query-builder.request_data_source') === 'body') {
|
||||
return $this->input($key, $default);
|
||||
}
|
||||
|
||||
return $this->get($key, $default);
|
||||
}
|
||||
|
||||
public static function setIncludesArrayValueDelimiter(string $includesArrayValueDelimiter): void
|
||||
{
|
||||
static::$includesArrayValueDelimiter = $includesArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function setAppendsArrayValueDelimiter(string $appendsArrayValueDelimiter): void
|
||||
{
|
||||
static::$appendsArrayValueDelimiter = $appendsArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function setFieldsArrayValueDelimiter(string $fieldsArrayValueDelimiter): void
|
||||
{
|
||||
static::$fieldsArrayValueDelimiter = $fieldsArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function setSortsArrayValueDelimiter(string $sortsArrayValueDelimiter): void
|
||||
{
|
||||
static::$sortsArrayValueDelimiter = $sortsArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function setFilterArrayValueDelimiter(string $filterArrayValueDelimiter): void
|
||||
{
|
||||
static::$filterArrayValueDelimiter = $filterArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function getIncludesArrayValueDelimiter(): string
|
||||
{
|
||||
return static::$includesArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function getAppendsArrayValueDelimiter(): string
|
||||
{
|
||||
return static::$appendsArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function getFieldsArrayValueDelimiter(): string
|
||||
{
|
||||
return static::$fieldsArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function getSortsArrayValueDelimiter(): string
|
||||
{
|
||||
return static::$sortsArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function getFilterArrayValueDelimiter(): string
|
||||
{
|
||||
return static::$filterArrayValueDelimiter;
|
||||
}
|
||||
|
||||
public static function resetDelimiters(): void
|
||||
{
|
||||
self::$includesArrayValueDelimiter = ',';
|
||||
self::$appendsArrayValueDelimiter = ',';
|
||||
self::$fieldsArrayValueDelimiter = ',';
|
||||
self::$sortsArrayValueDelimiter = ',';
|
||||
self::$filterArrayValueDelimiter = ',';
|
||||
}
|
||||
}
|
||||
@ -21,7 +21,7 @@ Route::prefix('auth')->group(function (){
|
||||
|
||||
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
|
||||
|
||||
Route::post('pld/overview', [\App\Http\Controllers\PldController::class, 'overviewByRegion']);
|
||||
Route::get('pld/overview', [\App\Http\Controllers\PldController::class, 'overviewByRegion']);
|
||||
Route::post('pld/list', [\App\Http\Controllers\PldController::class, 'listConsumption']);
|
||||
Route::post('pld/daily', [\App\Http\Controllers\PldController::class, 'consumptionByDaily']);
|
||||
Route::post('pld/schedule', [\App\Http\Controllers\PldController::class, 'consumptionBySchedule']);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user