mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 06:03:39 +08:00
feat: cleaner sortmap implementation
This commit is contained in:
parent
26b1185f20
commit
fdf7b9a082
|
@ -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.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
22
framework/core/src/Api/Resource/Concerns/HasSortMap.php
Normal file
22
framework/core/src/Api/Resource/Concerns/HasSortMap.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user