Skip to content

Commit

Permalink
Added sort in backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
dyzajash committed Jan 18, 2021
1 parent 25850a9 commit a5a1948
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Thumbs.db
*.db
.php_cs.cache
config/settings.local.php
*.log
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ezyang/htmlpurifier": "^4.13",
"guzzlehttp/guzzle": "^7.0",
"monolog/monolog": "^2.2",
"myclabs/php-enum": "^1.7",
"odan/session": "^5.1",
"php-di/php-di": "^6.3",
"predis/predis": "^1.1",
Expand Down
62 changes: 61 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Action/ShowBotAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$botQuery = BotQueryDto::fromRequest($request->getQueryParams());

if (true === $botQuery->getQueryDto()->isValid()) {
$results = $this->packService->searchInBot($bot, $botQuery->getQueryDto()->getValue());
$results = $this->packService->searchInBot($bot, $botQuery);
} else {
$lastPacks = $this->packService->latestBotPacks($bot, $botQuery->getPageNumber());
$lastPacks = $this->packService->latestBotPacks($bot, $botQuery);
}

return $this->responder->render(
Expand Down
6 changes: 5 additions & 1 deletion src/Action/ShowIndexAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Action;

use App\Domain\SortDto;
use App\Infrastructure\Service\PackService;
use App\Infrastructure\Service\SiteNewsService;
use App\Responder\Responder;
Expand All @@ -25,7 +26,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $this->responder->render(
$response,
'index.html.twig',
['latest' => $this->packService->latest(20), 'news' => $this->newsService->getLatestNews()]
[
'latest' => $this->packService->latest(SortDto::fromRequest($request->getQueryParams())),
'news' => $this->newsService->getLatestNews()
]
);
}
}
7 changes: 5 additions & 2 deletions src/Action/ShowSearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ public function __construct(Responder $responder, PackService $packService)
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$results = null;
$query = QueryDto::fromRequest($request->getQueryParams());
$params = $request->getQueryParams();
$rawPage = $params['page'];
$page = (isset($rawPage) && is_numeric($rawPage)) ? (int) $rawPage : 0;
$query = QueryDto::fromRequest($params);

if (true === $query->isValid()) {
$results = $this->packService->search($query->getValue());
$results = $this->packService->search($query, $page);
}

return $this->responder->render($response, 'search.html.twig', ['query' => $query->getValue(), 'results' => $results]);
Expand Down
11 changes: 9 additions & 2 deletions src/Domain/QueryDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
final class QueryDto
{
private ?string $query;
private SortDto $sortDto;

private function __construct(?string $query)
private function __construct(?string $query, SortDto $sortDto)
{
$this->query = $query;
$this->sortDto = $sortDto;
}

public function getValue(): ?string
Expand All @@ -26,7 +28,7 @@ public static function fromRequest(array $params): self
$query = HTMLPurify::purify($rawQuery);
}

return new self($query);
return new self($query, SortDto::fromRequest($params));
}

/** Check if clean query can be send to API */
Expand All @@ -38,4 +40,9 @@ public function isValid(): bool

return strlen($this->query) > 2;
}

public function getSortData(): SortDto
{
return $this->sortDto;
}
}
15 changes: 15 additions & 0 deletions src/Domain/SortDirectionEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Domain;

use MyCLabs\Enum\Enum;

/**
* @method static SortDirectionEnum ASC()
* @method static SortDirectionEnum DESC()
*/
final class SortDirectionEnum extends Enum
{
private const ASC = 'ASC';
private const DESC = 'DESC';
}
45 changes: 45 additions & 0 deletions src/Domain/SortDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Domain;

final class SortDto
{
private const DEFAULT_SORT = 'name';
private const ALLOWED_SORT_PARAMS = ['name', 'number', 'size', 'sizekbits', 'episodeNumber'];
private string $by;
private SortDirectionEnum $direction;

private function __construct(string $by, SortDirectionEnum $sortDirectionEnum)
{
$this->by = $by;
$this->direction = $sortDirectionEnum;
}

public static function fromRequest(array $params): self
{
$by = self::DEFAULT_SORT;
$direction = SortDirectionEnum::ASC();

$byParam = $params['by'];
if (isset($byParam) && in_array($byParam, self::ALLOWED_SORT_PARAMS, true)) {
$by = $byParam;
}

$directionParam = strtoupper($params['direction']);
if (isset($directionParam) && in_array($directionParam, SortDirectionEnum::toArray(), true)) {
$direction = new SortDirectionEnum($directionParam);
}

return new self($by, $direction);
}

public function getDirection(): SortDirectionEnum
{
return $this->direction;
}

public function getBy(): string
{
return $this->by;
}
}
19 changes: 10 additions & 9 deletions src/Infrastructure/Service/NIBLApi/CachedNIBLApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Infrastructure\Service\NIBLApi;

use App\Domain\Bot;
use App\Domain\SortDto;
use App\Exception\NIBLApiException;
use App\Infrastructure\Service\Cache\DataCache;
use App\Infrastructure\Service\Cache\ExpireKey;
Expand All @@ -25,13 +26,13 @@ public function __construct(LoggerInterface $logger, NIBLApiClient $apiClient, D
$this->logger = $logger;
}

public function latestPacks(int $limit = 20): array
public function latestPacks(SortDto $sortDto, int $limit = 20): array
{
if ($dataRaw = $this->dataCache->getItem(self::LATEST_PACKS_KEY)) {
return json_decode($dataRaw, true);
}

$data = $this->apiClient->latestPacks($limit);
$data = $this->apiClient->latestPacks($sortDto, $limit);
$this->dataCache->setItem(self::LATEST_PACKS_KEY, json_encode($data), ExpireKey::EXPIRE_IN_10_MIN);

return $data;
Expand All @@ -49,16 +50,16 @@ public function botList(): array
return $data;
}

public function botPacks(Bot $bot, int $page = 0): array
public function botPacks(Bot $bot, SortDto $sortDto, int $page = 0): array
{
if ($dataRaw = $this->dataCache->getItem(sprintf('%s_%s', self::BOT_PACKS_KEY_PARTIAL, $bot->getId()))) {
return json_decode($dataRaw, true);
}

try {
$data = $this->apiClient->botPacks($bot, $page);
$data = $this->apiClient->botPacks($bot, $sortDto, $page);
$this->dataCache->setItem(
sprintf('%s_%s_%d', self::BOT_PACKS_KEY_PARTIAL, $bot->getId(), $page),
sprintf('%s_%s_%d_%s_%s', self::BOT_PACKS_KEY_PARTIAL, $bot->getId(), $page, (string) $sortDto->getDirection(), $sortDto->getBy()),
json_encode($data),
ExpireKey::EXPIRE_IN_10_MIN
);
Expand All @@ -70,13 +71,13 @@ public function botPacks(Bot $bot, int $page = 0): array
return $data;
}

public function search(string $query): array
public function search(SortDto $sortDto, string $query, int $page = 0): array
{
return $this->apiClient->search($query);
return $this->apiClient->search($sortDto, $query, $page);
}

public function searchInBot(Bot $bot, string $query): array
public function searchInBot(Bot $bot, SortDto $sortDto, string $query, int $page = 0): array
{
return $this->apiClient->searchInBot($bot, $query);
return $this->apiClient->searchInBot($bot, $sortDto, $query, $page);
}
}
29 changes: 21 additions & 8 deletions src/Infrastructure/Service/NIBLApi/NIBLApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Infrastructure\Service\NIBLApi;

use App\Domain\Bot;
use App\Domain\SortDto;
use App\Exception\NIBLApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
Expand All @@ -27,10 +28,10 @@ public function __construct(LoggerInterface $logger, string $apiUrl)
/**
* @throws NIBLApiException
*/
public function latestPacks(int $limit = 20): array
public function latestPacks(SortDto $sortDto, int $limit = 20): array
{
try {
$response = $this->client->request('GET', 'latest', ['query' => ['limit' => $limit]]);
$response = $this->client->request('GET', 'latest', ['query' => ['limit' => $limit, 'sort' => $sortDto->getBy(), 'direction' => (string) $sortDto->getDirection()]]);

return $this->validateResponse($response);
} catch (GuzzleException $exception) {
Expand Down Expand Up @@ -59,10 +60,14 @@ public function botList(): array
/**
* @throws NIBLApiException
*/
public function botPacks(Bot $bot, int $page = 0): array
public function botPacks(Bot $bot, SortDto $sortDto, int $page = 0): array
{
try {
$response = $this->client->request('GET', sprintf('packs/%s/page', $bot->getId()), ['query' => ['page' => $page, 'size' => 50, 'sort' => 'number', 'direction' => 'ASC']]);
$response = $this->client->request(
'GET',
sprintf('packs/%s/page', $bot->getId()),
['query' => ['page' => $page, 'size' => 50, 'sort' => $sortDto->getBy(), 'direction' => (string) $sortDto->getDirection()]]
);

return $this->validateResponse($response);
} catch (GuzzleException $exception) {
Expand All @@ -75,10 +80,14 @@ public function botPacks(Bot $bot, int $page = 0): array
/**
* @throws NIBLApiException
*/
public function search(string $query): array
public function search(SortDto $sortDto, string $query, int $page = 0): array
{
try {
$response = $this->client->request('GET', 'search', ['query' => ['query' => $query]]);
$response = $this->client->request(
'GET',
'search/page',
['query' => ['page' => $page, 'query' => $query, 'sort' => $sortDto->getBy(), 'direction' => (string) $sortDto->getDirection()]]
);

return $this->validateResponse($response);
} catch (GuzzleException $exception) {
Expand All @@ -91,10 +100,14 @@ public function search(string $query): array
/**
* @throws NIBLApiException
*/
public function searchInBot(Bot $bot, string $query): array
public function searchInBot(Bot $bot, SortDto $sortDto, string $query, int $page = 0): array
{
try {
$response = $this->client->request('GET', sprintf('search/%s', $bot->getId()), ['query' => ['query' => $query]]);
$response = $this->client->request(
'GET',
sprintf('search/%s/page', $bot->getId()),
['query' => ['page' => $page, 'query' => $query, 'sort' => $sortDto->getBy(), 'direction' => (string) $sortDto->getDirection()]]
);

return $this->validateResponse($response);
} catch (GuzzleException $exception) {
Expand Down
9 changes: 5 additions & 4 deletions src/Infrastructure/Service/NIBLApi/NIBLApiContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
namespace App\Infrastructure\Service\NIBLApi;

use App\Domain\Bot;
use App\Domain\SortDto;

interface NIBLApiContract
{
public function latestPacks(int $limit = 20): array;
public function latestPacks(SortDto $sortDto, int $limit = 20): array;

public function botList(): array;

public function botPacks(Bot $bot, int $page = 0): array;
public function botPacks(Bot $bot, SortDto $sortDto, int $page = 0): array;

public function search(string $query): array;
public function search(SortDto $sortDto, string $query, int $page = 0): array;

public function searchInBot(Bot $bot, string $query): array;
public function searchInBot(Bot $bot, SortDto $sortDto, string $query, int $page = 0): array;
}
Loading

0 comments on commit a5a1948

Please sign in to comment.