-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from cash-track/feature/tags
Feature / Tags
- Loading branch information
Showing
26 changed files
with
1,313 additions
and
107 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Tags; | ||
|
||
use App\Controller\AuthAwareController; | ||
use App\Database\Tag; | ||
use App\Repository\TagRepository; | ||
use App\Repository\UserRepository; | ||
use App\View\TagsView; | ||
use App\View\TagView; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Spiral\Auth\AuthScope; | ||
use Spiral\Http\ResponseWrapper; | ||
use Spiral\Router\Annotation\Route; | ||
|
||
final class CommonController extends AuthAwareController | ||
{ | ||
public function __construct( | ||
AuthScope $auth, | ||
private ResponseWrapper $response, | ||
private TagRepository $tagRepository, | ||
private TagsView $tagsView, | ||
private TagView $tagView, | ||
private UserRepository $userRepository, | ||
) { | ||
parent::__construct($auth); | ||
} | ||
|
||
#[Route(route: '/tags/common', name: 'tag.common.list', methods: 'GET', group: 'auth')] | ||
public function list(): ResponseInterface | ||
{ | ||
return $this->tagsView->json($this->tagRepository->findAllByUsersPK( | ||
$this->userRepository->getCommonUserIDs($this->user) | ||
)); | ||
} | ||
|
||
#[Route(route: '/tags/common/<id>', name: 'tag.common.index', methods: 'GET', group: 'auth')] | ||
public function index(int $id): ResponseInterface | ||
{ | ||
$tag = $this->tagRepository->findByPKByUsersPK($id, $this->userRepository->getCommonUserIDs($this->user)); | ||
|
||
if (! $tag instanceof Tag) { | ||
return $this->response->create(404); | ||
} | ||
|
||
return $this->tagView->json($tag); | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Service\Filter; | ||
|
||
use Cycle\Database\Query\SelectQuery; | ||
use Cycle\ORM\Select; | ||
|
||
trait Filter | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
protected array $filter = []; | ||
|
||
public function filter(array $query): static | ||
{ | ||
if (count($query) === 0) { | ||
return $this; | ||
} | ||
|
||
$this->filter = []; | ||
|
||
foreach (FilterType::cases() as $type) { | ||
if (! array_key_exists($type->value, $query)) { | ||
continue; | ||
} | ||
|
||
if (! $type->validate($query[$type->value])) { | ||
continue; | ||
} | ||
|
||
$this->filter[$type->value] = $query[$type->value]; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function hasFilter(FilterType $filter): bool | ||
{ | ||
return array_key_exists($filter->value, $this->filter); | ||
} | ||
|
||
public function getFilterValue(FilterType $filter): ?string | ||
{ | ||
return $this->filter[$filter->value] ?? null; | ||
} | ||
|
||
protected function filterColumnsMapping(): array | ||
{ | ||
return [ | ||
FilterType::ByDateFrom->value => 'created_at', | ||
FilterType::ByDateTo->value => 'created_at', | ||
]; | ||
} | ||
|
||
protected function injectFilter(Select|SelectQuery $query): void | ||
{ | ||
if (! count($this->filter)) { | ||
return; | ||
} | ||
|
||
foreach ($this->filter as $type => $value) { | ||
try { | ||
FilterType::from($type)->inject($query, $value, $this->filterColumnsMapping()); | ||
} catch (\ValueError) { | ||
continue; | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Service\Filter; | ||
|
||
use Cycle\Database\Query\SelectQuery; | ||
use Cycle\ORM\Select; | ||
|
||
// FIXME. PHPCS PSR12 Does not support PHP 8.1 new feature syntax | ||
// @codingStandardsIgnoreStart | ||
enum FilterType: string { | ||
case ByDateFrom = 'date-from'; | ||
case ByDateTo = 'date-to'; | ||
|
||
public function inject(Select|SelectQuery $query, string $value, array $mapping): void | ||
{ | ||
switch ($this) { | ||
case self::ByDateFrom: | ||
$query->where($mapping[$this->value] ?? 'created_at', '>=', new \DateTimeImmutable($value)); | ||
break; | ||
case self::ByDateTo: | ||
$query->where($mapping[$this->value] ?? 'created_at', '<=', new \DateTimeImmutable($value)); | ||
break; | ||
} | ||
} | ||
|
||
public function validate(string $value): bool | ||
{ | ||
switch ($this) { | ||
case self::ByDateFrom: | ||
case self::ByDateTo: | ||
try { | ||
new \DateTimeImmutable($value); | ||
} catch (\Throwable) { | ||
return false; | ||
} | ||
break; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
// @codingStandardsIgnoreEnd |
Oops, something went wrong.