Created
February 29, 2016 15:24
-
-
Save vinicius73/175b0099ee1169876cfd to your computer and use it in GitHub Desktop.
Repo query filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Support\Queries; | |
use Illuminate\Database\Query\Builder as QueryBuilder; | |
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder; | |
use Carbon\Carbon; | |
abstract class BaseQueryBuilder | |
{ | |
/** | |
* @var array | |
*/ | |
protected $params; | |
/** | |
* @var EloquentQueryBuilder|QueryBuilder | |
*/ | |
protected $query; | |
/** | |
* @param string $field | |
* @param string $init | |
* @param string $finish | |
* | |
* @return void | |
*/ | |
protected function applyBetweenDates($field, $init, $finish) | |
{ | |
//Both dates were informed | |
if (! empty($init) && ! empty($finish)) { | |
$init = new Carbon($init); | |
$finish = new Carbon($finish); | |
if ($init->gt($finish)) { | |
throw new \InvalidArgumentException('['.$field.'] Initial Date must be less than the Final Date.'); | |
} | |
$this->query->whereBetween($field, [$init, $finish]); | |
return; | |
} | |
// Only the starting date was informed | |
if (! empty($init) && empty($finish)) { | |
$init = new Carbon($init); | |
$this->query->where($field, '>=', $init); | |
return; | |
} | |
// Only the final date was informed | |
if (! empty($finish)) { | |
$finish = (new Carbon($finish))->setTimeFromTimeString('23:59:59'); | |
$this->query->where($field, '<=', $finish); | |
return; | |
} | |
} | |
/** | |
* @return EloquentQueryBuilder|QueryBuilder | |
*/ | |
abstract public function getQuery(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Artesaos\Warehouse; | |
use Artesaos\Warehouse\Contracts\BaseRepository as BaseRepositoryContract; | |
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder; | |
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Query\Builder as QueryBuilder; | |
use Illuminate\Pagination\AbstractPaginator as Paginator; | |
abstract class BaseRepository implements BaseRepositoryContract | |
{ | |
/** | |
* Model class for repo. | |
* | |
* @var string | |
*/ | |
protected $modelClass; | |
/** | |
* @param EloquentQueryBuilder|QueryBuilder $query | |
* @param int $take | |
* @param bool $paginate | |
* | |
* @return EloquentCollection|Paginator | |
*/ | |
protected function doQuery($query = null, $take = 15, $paginate = true) | |
{ | |
if (is_null($query)) { | |
$query = $this->newQuery(); | |
} | |
if (true == $paginate) { | |
return $query->paginate($take); | |
} | |
if ($take > 0 || false !== $take) { | |
$query->take($take); | |
} | |
return $query->get(); | |
} | |
/** | |
* @return EloquentQueryBuilder|QueryBuilder | |
*/ | |
protected function newQuery() | |
{ | |
return app()->make($this->modelClass)->newQuery(); | |
} | |
/** | |
* Returns all records. | |
* If $take is false then brings all records | |
* If $paginate is true returns Paginator instance. | |
* | |
* @param int $take | |
* @param bool $paginate | |
* | |
* @return EloquentCollection|Paginator | |
*/ | |
public function getAll($take = 15, $paginate = true) | |
{ | |
return $this->doQuery(null, $take, $paginate); | |
} | |
/** | |
* @param string $column | |
* @param string|null $key | |
* | |
* @return \Illuminate\Support\Collection|array | |
*/ | |
public function lists($column, $key = null) | |
{ | |
return $this->newQuery()->lists($column, $key); | |
} | |
/** | |
* Retrieves a record by his id | |
* If fail is true $ fires ModelNotFoundException. | |
* | |
* @param int $id | |
* @param bool $fail | |
* | |
* @return Model | |
*/ | |
public function findByID($id, $fail = true) | |
{ | |
if ($fail) { | |
return $this->newQuery()->findOrFail($id); | |
} | |
return $this->newQuery()->find($id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Applications\Tenant\Http\Controllers\Finance; | |
use Illuminate\Http\Request; | |
use App\Applications\Tenant\Http\Controllers\BaseController; | |
use App\Domains\Tenants\Contracts\Repositories\Finance\Movements; | |
class IncomeController extends BaseController | |
{ | |
/** | |
* @var Movements | |
*/ | |
protected $repository; | |
//protected $pageTitleDefault = 'Recebimentos'; | |
protected $itemsPerPage = 15; | |
public function __construct(Movements $repository) | |
{ | |
$this->repository = $repository; | |
} | |
public function index(Request $request) | |
{ | |
$incomes = $this->repository->getAllIncomesByParams($request->all()); | |
return $this->view('finance.income.index', compact('incomes')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Domains\Finance\Repositories; | |
use App\Domains\Finance\Contracts\Repositories\Movements as MovementsContract; | |
use App\Domains\Finance\Movement; | |
use App\Domains\Finance\Queries\MovementsQueryFilter; | |
use Artesaos\Warehouse\BaseRepository; | |
class Movements extends BaseRepository implements MovementsContract | |
{ | |
protected $modelClass = Movement::class; | |
/** | |
* Retrieve items based on informed parameters. | |
* | |
* @param array $params | |
* @param int $take | |
* @param bool $paginate | |
* | |
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Pagination\AbstractPaginator | |
*/ | |
public function getAllIncomesByParams(array $params, $take = 15, $paginate = true) | |
{ | |
$query = (new MovementsQueryFilter($this->newIncomeQuery(), $params))->getQuery(); | |
return $this->doQuery($query, $take, $paginate); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Domains\Finance\Queries; | |
use Illuminate\Database\Query\Builder as QueryBuilder; | |
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder; | |
use App\Support\Queries\BaseQueryBuilder; | |
class MovementsQueryFilter extends BaseQueryBuilder | |
{ | |
/** | |
* MovementsFilterQuery constructor. | |
* @param EloquentQueryBuilder|QueryBuilder $query | |
* | |
* @param array $params | |
*/ | |
public function __construct($query, array $params) | |
{ | |
$this->params = $params; | |
$this->query = $query; | |
} | |
private function byPaidStatus() | |
{ | |
$status = array_get($this->params, 'paid_status', null); | |
if (! is_numeric($status) || ! in_array($status, [0, 1])) { | |
return; | |
} | |
$status = (int) $status; | |
if ($status === 1) { // is paid | |
$this->query->whereNotNull('paid_date'); | |
return; | |
} | |
if ($status === 0) { // is not paid | |
$this->query->whereNull('paid_date'); | |
return; | |
} | |
} | |
private function byAccounts() | |
{ | |
$account = array_get($this->params, 'account_id', 0); | |
if ($account < 1) { | |
return; | |
} | |
$this->query->where('account_id', $account); | |
} | |
private function byBankAccounts() | |
{ | |
$bank = (int) array_get($this->params, 'bank_account_id', 0); | |
if ($bank < 1) { | |
return; | |
} | |
$this->query->where('bank_account_id', $bank); | |
} | |
private function byDueDate() | |
{ | |
$init = array_get($this->params, 'due_date_init', null); | |
$finish = array_get($this->params, 'due_date_finish', null); | |
$this->applyBetweenDates('due_date', $init, $finish); | |
} | |
private function byPaidDate() | |
{ | |
$init = array_get($this->params, 'paid_date_init', null); | |
$finish = array_get($this->params, 'paid_date_finish', null); | |
$this->applyBetweenDates('paid_date', $init, $finish); | |
} | |
/** | |
* @return EloquentQueryBuilder|QueryBuilder | |
*/ | |
public function getQuery() | |
{ | |
$this->byPaidStatus(); | |
$this->byBankAccounts(); | |
$this->byAccounts(); | |
$this->byDueDate(); | |
$this->byPaidDate(); | |
return $this->query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment