Skip to content

Commit

Permalink
(#1119) Add recordsTotal to /api/search/random
Browse files Browse the repository at this point in the history
  • Loading branch information
Difegue committed Dec 10, 2024
1 parent e6ec279 commit 0e5bb12
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/LANraragi/Controller/Api/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojolicious::Controller';
use List::Util qw(min);

use LANraragi::Model::Search;
use LANraragi::Utils::Generic qw(render_api_response);
use LANraragi::Utils::Generic qw(render_api_response);
use LANraragi::Utils::Database qw(invalidate_cache get_archive_json_multi);

# Undocumented API matching the Datatables spec.
Expand Down Expand Up @@ -52,7 +52,7 @@ sub handle_datatables {

# TODO add a parameter to datatables for grouptanks? Not really essential rn tho
my ( $total, $filtered, @ids ) =
LANraragi::Model::Search::do_search( $filter, $categoryfilter, $start, $sortkey, $sortorder, $newfilter, $untaggedfilter, 1);
LANraragi::Model::Search::do_search( $filter, $categoryfilter, $start, $sortkey, $sortorder, $newfilter, $untaggedfilter, 1 );

$self->render( json => get_datatables_object( $draw, $total, $filtered, @ids ) );
}
Expand All @@ -65,11 +65,11 @@ sub handle_api {

my $filter = $req->param('filter');
my $category = $req->param('category') || "";
my $start = $req->param('start') || 0;
my $start = $req->param('start') || 0;
my $sortkey = $req->param('sortby');
my $sortorder = $req->param('order');
my $newfilter = $req->param('newonly') || "false";
my $untaggedf = $req->param('untaggedonly') || "false";
my $newfilter = $req->param('newonly') || "false";
my $untaggedf = $req->param('untaggedonly') || "false";
my $grouptanks = $req->param('groupby_tanks') || "true";

$sortorder = ( $sortorder && $sortorder eq 'desc' ) ? 1 : 0;
Expand All @@ -96,15 +96,19 @@ sub get_random_archives {
my $req = $self->req;

my $filter = $req->param('filter');
my $category = $req->param('category') || "";
my $newfilter = $req->param('newonly') || "false";
my $untaggedf = $req->param('untaggedonly') || "false";
my $category = $req->param('category') || "";
my $newfilter = $req->param('newonly') || "false";
my $untaggedf = $req->param('untaggedonly') || "false";
my $grouptanks = $req->param('groupby_tanks') || "true";
my $random_count = $req->param('count') || 5;
my $random_count = $req->param('count') || 5;

# Use the search engine to get IDs matching the filter/category selection, with start=-1 to get all data
my ( $total, $filtered, @ids ) =
LANraragi::Model::Search::do_search( $filter, $category, -1, "title", 0, $newfilter eq "true", $untaggedf eq "true", $grouptanks eq "true" );
my ( $total, $filtered, @ids ) = LANraragi::Model::Search::do_search(
$filter, $category, -1, "title", 0,
$newfilter eq "true",
$untaggedf eq "true",
$grouptanks eq "true"
);
my @random_ids;

$random_count = min( $random_count, scalar(@ids) );
Expand All @@ -116,7 +120,12 @@ sub get_random_archives {
}

my @data = get_archive_json_multi(@random_ids);
$self->render( json => { data => \@data } );
$self->render(
json => {
data => \@data,
recordsTotal => $random_count
}
);
}

# get_datatables_object($draw, $total, $totalsearched, @pagedkeys)
Expand Down
1 change: 1 addition & 0 deletions tools/Documentation/api-documentation/search-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ When enabled, Tankoubons will show in search results, replacing all the archive
{% swagger-response status="200" description="" %}
```javascript
{
"recordsTotal": 4,
"data": [
{
"arcid": "2810d5e0a8d027ecefebca6237031a0fa7b91eb3",
Expand Down

0 comments on commit 0e5bb12

Please sign in to comment.