feat: cleaner sortmap implementation

This commit is contained in:
Sami Mazouz 2024-03-02 13:18:28 +01:00
parent 26b1185f20
commit fdf7b9a082
No known key found for this signature in database
7 changed files with 72 additions and 15 deletions

View File

@ -10,6 +10,7 @@
namespace Flarum\Tags\Content; namespace Flarum\Tags\Content;
use Flarum\Api\Client; use Flarum\Api\Client;
use Flarum\Api\Resource\DiscussionResource;
use Flarum\Frontend\Document; use Flarum\Frontend\Document;
use Flarum\Http\RequestUtil; use Flarum\Http\RequestUtil;
use Flarum\Http\SlugManager; use Flarum\Http\SlugManager;
@ -27,7 +28,8 @@ class Tag
protected Factory $view, protected Factory $view,
protected TagRepository $tags, protected TagRepository $tags,
protected TranslatorInterface $translator, protected TranslatorInterface $translator,
protected SlugManager $slugger protected SlugManager $slugger,
protected DiscussionResource $resource
) { ) {
} }
@ -42,7 +44,7 @@ class Tag
$page = Arr::pull($queryParams, 'page', 1); $page = Arr::pull($queryParams, 'page', 1);
$filters = Arr::pull($queryParams, 'filter', []); $filters = Arr::pull($queryParams, 'filter', []);
$sortMap = $this->getSortMap(); $sortMap = $this->resource->sortMap();
$tag = $this->slugger->forResource(TagModel::class)->fromSlug($slug, $actor); $tag = $this->slugger->forResource(TagModel::class)->fromSlug($slug, $actor);
@ -78,14 +80,6 @@ class Tag
return $document; return $document;
} }
/**
* Get a map of sort query param values and their API sort params.
*/
protected function getSortMap(): array
{
return resolve('flarum.forum.discussions.sortmap');
}
/** /**
* Get the result of an API request to list discussions. * Get the result of an API request to list discussions.
*/ */

View File

@ -12,6 +12,7 @@ use Flarum\Api\Resource\Contracts\{Findable,
Deletable}; Deletable};
use Flarum\Api\Resource\Concerns\Bootable; use Flarum\Api\Resource\Concerns\Bootable;
use Flarum\Api\Resource\Concerns\Extendable; use Flarum\Api\Resource\Concerns\Extendable;
use Flarum\Api\Resource\Concerns\HasSortMap;
use Flarum\Foundation\DispatchEventsTrait; use Flarum\Foundation\DispatchEventsTrait;
use Flarum\User\User; use Flarum\User\User;
use RuntimeException; use RuntimeException;
@ -30,6 +31,7 @@ abstract class AbstractDatabaseResource extends BaseResource implements
{ {
use Bootable; use Bootable;
use Extendable; use Extendable;
use HasSortMap;
use DispatchEventsTrait { use DispatchEventsTrait {
dispatchEventsFor as traitDispatchEventsFor; dispatchEventsFor as traitDispatchEventsFor;
} }

View File

@ -4,6 +4,7 @@ namespace Flarum\Api\Resource;
use Flarum\Api\Resource\Concerns\Bootable; use Flarum\Api\Resource\Concerns\Bootable;
use Flarum\Api\Resource\Concerns\Extendable; use Flarum\Api\Resource\Concerns\Extendable;
use Flarum\Api\Resource\Concerns\HasSortMap;
use Flarum\Api\Resource\Contracts\Collection; use Flarum\Api\Resource\Contracts\Collection;
use Flarum\Api\Resource\Contracts\Resource; use Flarum\Api\Resource\Contracts\Resource;
use Tobyz\JsonApiServer\Resource\AbstractResource as BaseResource; use Tobyz\JsonApiServer\Resource\AbstractResource as BaseResource;
@ -12,4 +13,5 @@ abstract class AbstractResource extends BaseResource implements Resource, Collec
{ {
use Bootable; use Bootable;
use Extendable; use Extendable;
use HasSortMap;
} }

View File

@ -0,0 +1,22 @@
<?php
namespace Flarum\Api\Resource\Concerns;
use Flarum\Api\Sort\SortColumn;
trait HasSortMap
{
public function sortMap(): array
{
/** @var SortColumn[] $sorts */
$sorts = $this->resolveSorts();
$map = [];
foreach ($sorts as $sort) {
$map = array_merge($map, $sort->sortMap());
}
return $map;
}
}

View File

@ -246,9 +246,13 @@ class DiscussionResource extends AbstractDatabaseResource
public function sorts(): array public function sorts(): array
{ {
return [ return [
SortColumn::make('lastPostedAt'), SortColumn::make('lastPostedAt')
SortColumn::make('commentCount'), ->descendingAlias('latest'),
SortColumn::make('createdAt'), SortColumn::make('commentCount')
->descendingAlias('top'),
SortColumn::make('createdAt')
->ascendingAlias('oldest')
->descendingAlias('newest'),
]; ];
} }

View File

@ -6,5 +6,36 @@ use Tobyz\JsonApiServer\Laravel\Sort\SortColumn as BaseSortColumn;
class SortColumn extends BaseSortColumn class SortColumn extends BaseSortColumn
{ {
// protected array $alias = [
'asc' => null,
'desc' => null,
];
public function ascendingAlias(?string $alias): static
{
$this->alias['asc'] = $alias;
return $this;
}
public function descendingAlias(?string $alias): static
{
$this->alias['desc'] = $alias;
return $this;
}
public function sortMap(): array
{
$map = [];
foreach ($this->alias as $direction => $alias) {
if ($alias) {
$sort = ($direction === 'asc' ? '' : '-') . $this->name;
$map[$alias] = $sort;
}
}
return $map;
}
} }

View File

@ -10,6 +10,7 @@
namespace Flarum\Forum\Content; namespace Flarum\Forum\Content;
use Flarum\Api\Client; use Flarum\Api\Client;
use Flarum\Api\Resource\DiscussionResource;
use Flarum\Frontend\Document; use Flarum\Frontend\Document;
use Flarum\Http\UrlGenerator; use Flarum\Http\UrlGenerator;
use Flarum\Locale\TranslatorInterface; use Flarum\Locale\TranslatorInterface;
@ -26,6 +27,7 @@ class Index
protected SettingsRepositoryInterface $settings, protected SettingsRepositoryInterface $settings,
protected UrlGenerator $url, protected UrlGenerator $url,
protected TranslatorInterface $translator, protected TranslatorInterface $translator,
protected DiscussionResource $resource,
) { ) {
} }
@ -37,7 +39,7 @@ class Index
$q = Arr::pull($queryParams, 'q'); $q = Arr::pull($queryParams, 'q');
$page = max(1, intval(Arr::pull($queryParams, 'page'))); $page = max(1, intval(Arr::pull($queryParams, 'page')));
$sortMap = resolve('flarum.forum.discussions.sortmap'); $sortMap = $this->resource->sortMap();
$params = [ $params = [
...$queryParams, ...$queryParams,