From 1205e3539d56df396808d03f754440964cf94be5 Mon Sep 17 00:00:00 2001 From: Denis Fedoseev Date: Fri, 4 Aug 2017 09:57:17 +0300 Subject: [PATCH] Pagination mostly works --- fotostore.pl | 23 +++++++++++-- lib/FotoStore/DB.pm | 14 ++++++-- templates/includes/images_list.html.ep | 45 ++++++++++++++++++-------- templates/layouts/base.html.ep | 1 + 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/fotostore.pl b/fotostore.pl index e2c7abe..7f09911 100755 --- a/fotostore.pl +++ b/fotostore.pl @@ -9,6 +9,7 @@ use File::Basename 'basename'; use File::Path 'mkpath'; use File::Spec 'catfile'; use Cwd; +use POSIX; use Imager; use DBI; @@ -141,10 +142,21 @@ get '/' => sub { get '/get_images' => ( authenticated => 1 ) => sub { my $self = shift; + #Getting current user my $current_user = $self->current_user; my $user_id = $current_user->{'user_id'}; - my $files_list = $db->get_files( $current_user->{'user_id'}, 20 ); + #Getting images list with paging + my $page = $self->param('page') || 1; + my $items = $self->param('per-page') || 20; + + if (($page !~ /^\d+$/) || ($page <= 1)) { $page = 1} + if (($items !~ /^\d+$/) || ($items <= 0)) { $items = 20} + + # process images list + my $req_result = $db->get_files( $current_user->{'user_id'}, $items , $page); + my $files_list = $req_result->{'images_list'}; + my $pages_count = ceil($req_result->{'total_rows'}/$items); my $thumbs_dir = File::Spec->catfile( $IMAGE_DIR, $current_user->{'user_id'}, @@ -157,6 +169,7 @@ get '/get_images' => ( authenticated => 1 ) => sub { for my $img_item (@$files_list) { my $file = $img_item->{'file_name'}; my $img_hash = {}; + $img_hash->{'id'} = $img_item->{'file_id'}; $img_hash->{'filename'} = $img_item->{'original_filename'}; $img_hash->{'original_url'} = File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'}, @@ -185,10 +198,14 @@ get '/get_images' => ( authenticated => 1 ) => sub { $img_hash->{'scales'} = \@scaled; push( @$images, $img_hash ); + + } + my $reply_data = { current_page => $page, items_per_page => $items, pages_count => $pages_count, images_list => $images }; + # Render - return $self->render( json => $images ); + return $self->render( json => $reply_data ); }; # Upload image file @@ -201,7 +218,7 @@ post '/upload' => ( authenticated => 1 ) => sub { my $user = $self->current_user(); my $user_id = $user->{'user_id'}; - $self->app->log->debug( "user:" . Dumper($user) ); + # $self->app->log->debug( "user:" . Dumper($user) ); # Not upload unless ($image) { diff --git a/lib/FotoStore/DB.pm b/lib/FotoStore/DB.pm index 49dea87..4951676 100644 --- a/lib/FotoStore/DB.pm +++ b/lib/FotoStore/DB.pm @@ -1,5 +1,6 @@ package FotoStore::DB; +use v5.20; use strict; use warnings; @@ -59,8 +60,17 @@ sub add_file($self, $user_id, $filename, $original_filename) { return $rows; } -sub get_files($self, $user_id, $count=20, $start_at=0) { - return $self->{'dbh'}->selectall_arrayref(q~select * from images where owner_id=? order by created_time desc~, { Slice => {} }, $user_id ); +sub get_files($self, $user_id, $items_count=20, $page=1) { + + # Calculate offset + # Pages in UI starts from 1, but here we need it to start from 0 + $page = 1 if ($page < 1); + my $start_at = --$page * $items_count; + + my ($rows_count) = $self->{'dbh'}->selectrow_array(q~select count(*) from images where owner_id=? ~, undef , $user_id); + my $images_list = $self->{'dbh'}->selectall_arrayref(q~select * from images where owner_id=? order by created_time desc LIMIT ? OFFSET ? ~, { Slice => {} }, $user_id, $items_count, $start_at ); + + return { total_rows => $rows_count, images_list => $images_list }; } 1; \ No newline at end of file diff --git a/templates/includes/images_list.html.ep b/templates/includes/images_list.html.ep index 5649542..5f8cfa2 100644 --- a/templates/includes/images_list.html.ep +++ b/templates/includes/images_list.html.ep @@ -39,7 +39,7 @@
-
{{ image.filename }}
+
{{ image.id }} - {{ image.filename }}
    @@ -50,7 +50,20 @@
+
+
+ + + +
+
+
Fotos per page: + +
+
+
+ \ No newline at end of file diff --git a/templates/layouts/base.html.ep b/templates/layouts/base.html.ep index a3c18a5..3dd0510 100644 --- a/templates/layouts/base.html.ep +++ b/templates/layouts/base.html.ep @@ -15,6 +15,7 @@ +