Skip to content

Commit

Permalink
feat(bot search): pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
brmn committed Apr 3, 2021
1 parent 43bf8ea commit bade33b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/Bot/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final private function parseParams(string $paramString, array $paramList): array
$params = [];

foreach ($paramList as $name => $rules) {
$pattern = "/{$name}=([^ ]*)/";
$pattern = "/{$name}=([^ $]*)( |$)/";

$matches = [];

Expand Down
29 changes: 23 additions & 6 deletions app/Bot/Purchases/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
final class Search extends Command
{
protected const COMMAND = '/search';
protected const PARAMS = ['inn' => ['required']];
protected const PARAMS = ['inn' => ['required'], 'perpage' => ['int', 'max:50'], 'page' => ['int', 'max:10']];

private UserSearchesPurchases $search;
private UserRepo $userRepo;
Expand All @@ -39,13 +39,21 @@ public function handle(BotMan $bot): void
} catch (InvalidArgumentException $e) {
$bot->reply("wrong command format: {$e->getMessage()}");

return;
} catch (Exception $e) {
\Log::error('bot search query', [$e->getMessage()]);

$bot->reply("something went wrong");

return;
}

try {
$bot->reply($this->format($this->search->process($this->getUser($bot), $query)));
} catch (Exception $e) {
$bot->reply("something went wrong: {$e->getMessage()}");
\Log::error('bot search process', [$e->getMessage()]);

$bot->reply("something went wrong");
}
}

Expand All @@ -69,8 +77,10 @@ protected function mapParams(array $params): array
Assert::notNull($params['inn'], 'empty inn');

return [
'query' => $params[self::REST_OF_THE_QUERY],
'inn' => Inn::make($params['inn'])
'query' => empty($params[self::REST_OF_THE_QUERY]) ? null : $params[self::REST_OF_THE_QUERY],
'inn' => Inn::make($params['inn']),
'perpage' => (int)$params['perpage'],
'page' => (int)$params['page'],
];
}

Expand All @@ -86,8 +96,15 @@ private function makeQuery(BotMan $bot): PurchasesSearchQuery

private function format(PurchasesSearchResult $searchResult): OutgoingMessage
{
//@todo
$result = "Contracts found {$searchResult->data['contracts']['total']}\n\n";

foreach ($searchResult->data['contracts']['data'] as $contract) {
$result .= "signDate: {$contract['signDate']} / publishDate: {$contract['publishDate']}\n"
. "price: {$contract['price']} {$contract['currency']['code']}\n"
. "products: " . implode('; ', array_column($contract['products'], 'name')) . "\n"
. "{$contract['contractUrl']}\n\n\n";
}

return OutgoingMessage::create($searchResult->__toString());
return OutgoingMessage::create($result);
}
}
6 changes: 4 additions & 2 deletions app/Clearspending/ClearspendingApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function contractsSearch(array $query): array
]
);

// \Log::info('clearspending api', [$response->getStatusCode()]);

$result = Utils::jsonDecode($response->getBody()->getContents(), true);

if ($result === null) {
Expand All @@ -96,8 +98,8 @@ private function filterParams(array $query, array $paramList): array
{
return array_filter(
$query,
static fn(string $key): bool => in_array($key, $paramList, true),
ARRAY_FILTER_USE_KEY
static fn(?string $item, string $key): bool => in_array($key, $paramList, true) && $item !== null,
ARRAY_FILTER_USE_BOTH
);
}
}
4 changes: 3 additions & 1 deletion app/Usecases/Purchases/DTO/PurchasesSearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class PurchasesSearchQuery extends DataTransferObject
{
public string $query;
public ?string $query;
public Inn $inn;
public ?int $perpage;
public ?int $page;
}
4 changes: 3 additions & 1 deletion app/Usecases/Purchases/UserSearchesPurchases.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public function process(User $user, PurchasesSearchQuery $query): PurchasesSearc
[
'productsearch' => $query->query,
'customerinn' => $query->inn->getValue(),
'perpage' => $query->perpage,
'page' => $query->page,
]
)
]
);
} catch (GuzzleException $e) {
return new PurchasesSearchResult(['data' => 'network error']);
throw $e;
}
}
}
6 changes: 6 additions & 0 deletions known-issues.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
<code>Str</code>
</UndefinedClass>
</file>
<file src="app/Bot/Purchases/Search.php">
<UndefinedClass occurrences="2">
<code>\Log</code>
<code>\Log</code>
</UndefinedClass>
</file>
</files>

0 comments on commit bade33b

Please sign in to comment.