diff --git a/backend/app/Facades/DataTable/DataTable.php b/backend/app/Facades/DataTable/DataTable.php new file mode 100644 index 00000000..63699505 --- /dev/null +++ b/backend/app/Facades/DataTable/DataTable.php @@ -0,0 +1,67 @@ +filters); + $sorting = json_decode($request->sorting); + $rels = $request->rels ?: array(); + + $dataTableInput = new DataTableInput( + $request->start, + $request->size, + $filters, + $sorting, + $rels, + $allowedFilters, + $allowedSortings, + $allowedGroupBy + ); + + $query = $this->makeQueryFromModel($mixed); + + $dataTableService = (new DataTableService($query, $dataTableInput)) + ->setAllowedFilters($allowedFilters) + ->setAllowedRelations($allowedRelations) + ->setAllowedSortings($allowedSortings) + ->setAllowedSelects($allowedSelects) + ->setAllowedGroupBy($allowedGroupBy); + + return $dataTableService->getData(); + } + + protected function makeQueryFromModel(Model|Builder $mixed): Builder + { + return ($mixed instanceof Model) ? $mixed->query() : $mixed; + } +} diff --git a/backend/app/Facades/DataTable/DataTableFacade.php b/backend/app/Facades/DataTable/DataTableFacade.php new file mode 100644 index 00000000..98f2ac64 --- /dev/null +++ b/backend/app/Facades/DataTable/DataTableFacade.php @@ -0,0 +1,13 @@ +app->bind('datatable', function () { + return new DataTable(); + }); + } + + /** + * Bootstrap services. + */ + public function boot(): void + { + // + } +} diff --git a/backend/app/Services/DataTable/DataTableInput.php b/backend/app/Services/DataTable/DataTableInput.php new file mode 100644 index 00000000..1bbe5200 --- /dev/null +++ b/backend/app/Services/DataTable/DataTableInput.php @@ -0,0 +1,83 @@ +start; + } + + public function getSize(): ?int + { + return $this->size; + } + + /** + * @return array returns an array of Filter objects + */ + public function getFilters(): array + { + $filters = array(); + + foreach ($this->filters as $filter) { + $filters[] = new Filter( + $filter->id, + $filter->value, + $filter->fn, + $filter->datatype, + $this->allowedFilters + ); + } + + return $filters; + } + + /** + * @return array + */ + public function getSorting(): ?array + { + $sorts = []; + if (!empty($this->sorting)){ + foreach ($this->sorting as $sort) { + $sorts[] = new Sort($sort->id, $sort->desc, $this->allowedSortings); + } + } + return $sorts; + } + + public function getRelations(): array + { + return $this->rels; + } + + public function getGroupBy(): ?array + { + return $this->GroupBy; + } +} diff --git a/backend/app/Services/DataTable/DataTableService.php b/backend/app/Services/DataTable/DataTableService.php new file mode 100644 index 00000000..5755ad1d --- /dev/null +++ b/backend/app/Services/DataTable/DataTableService.php @@ -0,0 +1,128 @@ +allowedFilters = $allowedFilters; + return $this; + } + + public function setAllowedRelations(array $allowedRelations): DataTableService + { + $this->allowedRelations = $allowedRelations; + return $this; + } + + public function setAllowedSortings(array $allowedSortings): DataTableService + { + $this->allowedSortings = $allowedSortings; + return $this; + } + + public function setAllowedSelects(array $allowedSelects): DataTableService + { + $this->allowedSelects = $allowedSelects; + return $this; + } + + public function setAllowedGroupBy(array $allowedGroupBy): DataTableService + { + $this->allowedGroupBy = $allowedGroupBy; + return $this; + } + + /** + * Handle 'getData' operations + * @return array + */ + public function getData(): array + { + $query = $this->buildQuery(); + $data = $query->get(); + + return array( + 'data' => $data, + 'meta' => [ + 'totalRowCount' => $this->totalRowCount + ] + ); + } + + protected function buildQuery(): Builder + { + $query = $this->query; + + foreach ($this->dataTableInput->getFilters() as $filter) { + $query = (new ApplyFilter($query, $filter))->apply(); + } + + $query = $this->applySelect($query, $this->allowedSelects); + $query = $this->includeRelationsInQuery($query, $this->allowedRelations); + $query = $this->applyGroupBy($query, $this->allowedGroupBy); + + $this->totalRowCount = $query->count(); + + if (!is_null($this->dataTableInput->getStart())) { + $query->offset($this->dataTableInput->getStart()); + } + + if(!is_null($this->dataTableInput->getSize())){ + $query->limit($this->dataTableInput->getSize()); + } + + $sorting = $this->dataTableInput->getSorting(); + foreach ($sorting as $sort){ + $query = (new ApplySort($query, $sort))->apply(); + } + + return $query; + } + + protected function applySelect(Builder $query, array $selectedFields): Builder + { + if (!empty($selectedFields)) { + $query->select($selectedFields); + } + + return $query; + } + + protected function applyGroupBy(Builder $query, array $groupBy): Builder + { + return empty($groupBy) ? $query : $query->groupBy($groupBy); + } + + protected function includeRelationsInQuery(Builder $query, array $rels): Builder + { + if (!empty($rels)) { + $query->with($rels); + } + + return $query; + } + + // (later) define mapping of relation names to prevent relation name expose. + // (later) define mapping of column names to prevent column name expose. +} diff --git a/backend/app/Services/DataTable/Enums/DataType.php b/backend/app/Services/DataTable/Enums/DataType.php new file mode 100644 index 00000000..e16e98cd --- /dev/null +++ b/backend/app/Services/DataTable/Enums/DataType.php @@ -0,0 +1,15 @@ +fieldName = $fieldName; + parent::__construct($message, $code, $previous); + } + + public function getFieldName() + { + return $this->fieldName; + } +} diff --git a/backend/app/Services/DataTable/Exceptions/InvalidParameterInterface.php b/backend/app/Services/DataTable/Exceptions/InvalidParameterInterface.php new file mode 100644 index 00000000..3e7b3c73 --- /dev/null +++ b/backend/app/Services/DataTable/Exceptions/InvalidParameterInterface.php @@ -0,0 +1,7 @@ +fieldName = $fieldName; + parent::__construct($message, $code, $previous); + } + + public function getFieldName() + { + return $this->fieldName; + } +} diff --git a/backend/app/Services/DataTable/Exceptions/InvalidSortingException.php b/backend/app/Services/DataTable/Exceptions/InvalidSortingException.php new file mode 100644 index 00000000..d1704699 --- /dev/null +++ b/backend/app/Services/DataTable/Exceptions/InvalidSortingException.php @@ -0,0 +1,21 @@ +fieldName = $fieldName; + parent::__construct($message, $code, $previous); + } + + public function getFieldName() + { + return $this->fieldName; + } +} diff --git a/backend/app/Services/DataTable/Filter/ApplyFilter.php b/backend/app/Services/DataTable/Filter/ApplyFilter.php new file mode 100644 index 00000000..60ac88ea --- /dev/null +++ b/backend/app/Services/DataTable/Filter/ApplyFilter.php @@ -0,0 +1,84 @@ +filter; + $query = $this->query; + + $searchType = SearchType::from($filter->getFn()); + switch ($searchType) { + case SearchType::CONTAINS: + $this->searchFilter = new FilterContains($query, $filter); + break; + + case SearchType::EQUALS: + $this->searchFilter = new FilterEquals($query, $filter); + break; + + case SearchType::NOT_EQUALS: + $this->searchFilter = new FilterNotEquals($query, $filter); + break; + + case SearchType::BETWEEN: + $this->searchFilter = new FilterBetween($query, $filter); + break; + + case SearchType::GREATER_THAN: + $this->searchFilter = new FilterGreaterThan($query, $filter); + break; + + case SearchType::LESS_THAN: + $this->searchFilter = new FilterLessThan($query, $filter); + break; + + default: + $searchFunction = $filter->getFn(); + throw new InvalidFilterException($searchFunction, "search function `$searchFunction` is invalid."); + + } + + $relation = $this->filter->getRelation(); + return method_exists($this->query->getModel(), $relation) ? $this->applyFilterToRelation($relation) : $this->searchFilter->apply(); + } + + protected function applyFilterToRelation(string $relation): Builder + { + return $this->query->whereHas($relation, function (Builder $query) { + $this->filter->removeRelationFromId(); + $this->applyFilter($query, $this->filter); + }); + } + + private function applyFilter(Builder $query, Filter $filter): Builder + { + return (new ApplyFilter($query, $filter))->apply(); + } +} diff --git a/backend/app/Services/DataTable/Filter/Filter.php b/backend/app/Services/DataTable/Filter/Filter.php new file mode 100644 index 00000000..22362a1d --- /dev/null +++ b/backend/app/Services/DataTable/Filter/Filter.php @@ -0,0 +1,73 @@ +isValid($this, $this->allowedFilters); + } + + public function getId(): string + { + return $this->id; + } + + public function getValue(): array|int|string + { + return $this->value; + } + + public function getFn(): string + { + return $this->fn; + } + + public function getDatatype(): string + { + return $this->datatype; + } + + public function getRelation(): string + { + $fieldArray = explode('.', $this->id); + return count($fieldArray) > 1 ? $fieldArray[0] : ''; + } + + public function getColumn(): string + { + $fieldArray = explode('.', $this->id); + return array_pop($fieldArray); + } + + public function removeRelationFromId(): void + { + $this->id = $this->getColumn(); + } + + public function setValue(int|array|string $value): void + { + $this->value = $value; + } + +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterBetween.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterBetween.php new file mode 100644 index 00000000..7a6292da --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterBetween.php @@ -0,0 +1,33 @@ +query; + [$minVal, $maxVal] = $this->filter->getValue(); + + if ($minVal) { + if ($this->filter->getDatatype() == "date") { + $minVal = $minVal . " 00:00:00"; + } + $this->filter->setValue($minVal); + $query = (new FilterGreaterThanOrEqual($this->query, $this->filter))->apply(); + } + + if ($maxVal) { + if ($this->filter->getDatatype() == "date") { + $maxVal = $maxVal . " 23:59:59"; + } + $this->filter->setValue($maxVal); + $query = (new FilterLessThanOrEqual($this->query, $this->filter))->apply(); + } + + return $query; + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterContains.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterContains.php new file mode 100644 index 00000000..686fe9f1 --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterContains.php @@ -0,0 +1,31 @@ +filter->getId(); + $value = '%' . $this->filter->getValue() . '%'; + + if ($this->filter->getDatatype() == DataType::TEXT->value) { + $query = $this->searchIgnoreCase($column, $value); + + } else { + $query = $this->query->where($column, 'LIKE', $value); + } + + return $query; + } + + private function searchIgnoreCase(string $column, string $value): Builder + { + $value = strtolower($value); + return $this->query->whereRaw("LOWER($column) LIKE ?", [$value]); + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterEquals.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterEquals.php new file mode 100644 index 00000000..594ce87d --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterEquals.php @@ -0,0 +1,30 @@ +filter->getId(); + $value = $this->filter->getValue(); + + if ($this->filter->getDatatype() == DataType::TEXT->value) { + $query = $this->searchIgnoreCase($column, $value); + } else { + $query = $this->query->where($column, $value); + } + + return $query; + } + + private function searchIgnoreCase(string $column, string $value): Builder + { + $value = strtolower($value); + return $this->query->whereRaw("LOWER($column) = ?", [$value]); + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterGreaterThan.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterGreaterThan.php new file mode 100644 index 00000000..30da5d32 --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterGreaterThan.php @@ -0,0 +1,18 @@ +filter->getDatatype() == DataType::NUMERIC) ? + (float)$this->filter->getValue() : $this->filter->getValue(); + + return $this->query->where($this->filter->getId(), '>', $value); + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterGreaterThanOrEqual.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterGreaterThanOrEqual.php new file mode 100644 index 00000000..8e97248a --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterGreaterThanOrEqual.php @@ -0,0 +1,18 @@ +filter->getDatatype() == DataType::NUMERIC) ? + (float)$this->filter->getValue() : $this->filter->getValue(); + + return $this->query->where($this->filter->getId(), '>=', $value); + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterLessThan.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterLessThan.php new file mode 100644 index 00000000..4069b9e0 --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterLessThan.php @@ -0,0 +1,18 @@ +filter->getDatatype() == DataType::NUMERIC) ? + (float)$this->filter->getValue() : $this->filter->getValue(); + + return $this->query->where($this->filter->getId(), '<', $value); + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterLessThanOrEqual.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterLessThanOrEqual.php new file mode 100644 index 00000000..84409052 --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterLessThanOrEqual.php @@ -0,0 +1,18 @@ +filter->getDatatype() == DataType::NUMERIC) ? + (float)$this->filter->getValue() : $this->filter->getValue(); + + return $this->query->where($this->filter->getId(), '<=', $value); + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/FilterNotEquals.php b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterNotEquals.php new file mode 100644 index 00000000..e525f2b7 --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/FilterNotEquals.php @@ -0,0 +1,17 @@ +filter->getId(); + $value = $this->filter->getValue(); + + return $this->query->whereNot($column, $value); + } +} diff --git a/backend/app/Services/DataTable/Filter/SearchFunctions/SearchFilter.php b/backend/app/Services/DataTable/Filter/SearchFunctions/SearchFilter.php new file mode 100644 index 00000000..d4dcbf6a --- /dev/null +++ b/backend/app/Services/DataTable/Filter/SearchFunctions/SearchFilter.php @@ -0,0 +1,22 @@ +query; + + if (!is_null($this->sort)) { + $query->orderBy($this->sort->getId(), $this->sort->getDirection()); + } + + return $query; + } +} diff --git a/backend/app/Services/DataTable/Sort/Sort.php b/backend/app/Services/DataTable/Sort/Sort.php new file mode 100644 index 00000000..5196797a --- /dev/null +++ b/backend/app/Services/DataTable/Sort/Sort.php @@ -0,0 +1,39 @@ +isValid($this, $this->allowedSortings); + } + + /** + * @return string + */ + public function getId(): string + { + return $this->id; + } + + public function getDirection(): string + { + return $this->desc === true ? 'desc' : 'asc'; + } +} diff --git a/backend/app/Services/DataTable/Validators/FilterValidator.php b/backend/app/Services/DataTable/Validators/FilterValidator.php new file mode 100644 index 00000000..ea837f9e --- /dev/null +++ b/backend/app/Services/DataTable/Validators/FilterValidator.php @@ -0,0 +1,69 @@ +isAllowed($filter, $allowedFilters)) { + $filterId = $filter->getId(); + throw new InvalidFilterException($filter->getId(), "filtering field `$filterId` is not allowed."); + } + + if (!$this->isValidSearchFunction($filter)) { + $searchFunction = $filter->getFn(); + throw new InvalidFilterException($searchFunction, "search function `$searchFunction` is invalid."); + } + + if ($this->isValidDataType($filter) == -1) { + throw new InvalidFilterException(null, "datatype property is not set in `filters` array."); + } + + if (!$this->isValidDataType($filter)) { + $datatype = $filter->getDatatype(); + throw new InvalidFilterException($datatype, "datatype `$datatype` is invalid."); + } + + return true; + } + + protected function isAllowed(Filter $filter, array $allowedFilters): bool + { + return $allowedFilters == ['*'] || in_array($filter->getId(), $allowedFilters); + } + + protected function isValidSearchFunction(Filter $filter): bool + { + $searchFunction = $filter->getFn(); + return isset($searchFunction) && in_array($searchFunction, SearchType::values()); + } + + protected function isValidDataType(Filter $filter): int + { + if (!property_exists($filter, 'datatype')) + return -1; + + return $filter->getDatatype() && in_array($filter->getDatatype(), DataType::values()) ? 1 : 0; + } +} diff --git a/backend/app/Services/DataTable/Validators/RelationValidator.php b/backend/app/Services/DataTable/Validators/RelationValidator.php new file mode 100644 index 00000000..ab87382b --- /dev/null +++ b/backend/app/Services/DataTable/Validators/RelationValidator.php @@ -0,0 +1,25 @@ +isAllowed($sorting, $allowedSortings)) { + $sortId = $sorting->getId(); + throw new InvalidSortingException($sortId, "sorting field `$sortId` is not allowed."); + } + + return true; + } + + protected function isAllowed(Sort $sorting, array $allowedSortings): bool + { + return $allowedSortings == ['*'] || in_array($sorting->getId(), $allowedSortings); + } +} diff --git a/backend/bootstrap/providers.php b/backend/bootstrap/providers.php index fc94ae60..069e33bc 100644 --- a/backend/bootstrap/providers.php +++ b/backend/bootstrap/providers.php @@ -1,7 +1,6 @@