diff --git a/lib/PiTube.pm b/lib/PiTube.pm index e0df98b83e5aa49b9134a4a9a688f65740595e90..c9b64fdd54d7ef49c7e0cfae6ea9f19881abdecd 100644 --- a/lib/PiTube.pm +++ b/lib/PiTube.pm @@ -75,6 +75,7 @@ sub startup { $r->get('/archive')->to(cb => sub { shift->render('archive'); }); $r->get('/api/records')->to('Record#list'); + $r->delete('/api/records/:id')->to('Record#delete'); $r->get('/archive/:id')->to('Record#player'); # $r->get('/streams/add')->over( is => 'administrator' )->to('Stream#add'); diff --git a/lib/PiTube/Controller/Record.pm b/lib/PiTube/Controller/Record.pm index cc823daf1dc623c7a2187c85347ddbcea9dcb7cf..590298f2f232abaf5900bf41947033e23a768813 100644 --- a/lib/PiTube/Controller/Record.pm +++ b/lib/PiTube/Controller/Record.pm @@ -1,6 +1,10 @@ package PiTube::Controller::Record; use Mojo::Base 'Mojolicious::Controller'; +use constant PAGE_SIZE => 5; +use constant SORT_COLUMNS => qr/stream_name|publish_user_name|begin/; +use constant SORT_DIRS => qr/asc|desc/; + sub list { my $c = shift; @@ -16,9 +20,21 @@ sub list { ] }; + my $count = $c->schema->resultset('Record_view')->count($cond); + + my $sort = { -desc => 'begin' }; + + if ( $c->param('sort') =~ SORT_COLUMNS && $c->param('order') =~ SORT_DIRS ) { + $sort = { '-' . $c->param('order') => $c->param('sort') }; + } + my $records = $c->schema->resultset('Record_view')->search( $cond, - { order_by => { -desc => 'begin' }} + { + order_by => $sort, + offset => $c->param('offset') // 0, + rows => $c->param('limit') // PAGE_SIZE, + } ); my @records = (); @@ -28,12 +44,16 @@ sub list { my %record = ( $record->get_columns(), + is_deletable => $record->is_deletable( $c->session->{user}{id} ), ); push @records, \%record; } - $c->render( json => \@records ); + $c->render( json => { + total => $count, + rows => \@records, + }); } sub player { @@ -66,4 +86,25 @@ sub player { $c->render(); } +sub delete { + my $c = shift; + + my $record = $c->schema->resultset('Record')->search({ + id => $c->stash->{id}, + is_protected => 'f', + publish_user_id => $c->session->{user}{id} + })->first; + + if ( ! $record ) { + $c->render( status => 404, text => ''); + return; + } + + $record->update({ + is_active => 'f' + }); + + $c->render( status => 204, text => '' ); +} + 1; diff --git a/lib/PiTube/Schema/Result/Record.pm b/lib/PiTube/Schema/Result/Record.pm index d19054fdc477b7922f208ebb14368d194f23d6fa..70d40ff06c2b2da4f3043526b308112744665e08 100644 --- a/lib/PiTube/Schema/Result/Record.pm +++ b/lib/PiTube/Schema/Result/Record.pm @@ -39,5 +39,12 @@ __PACKAGE__->belongs_to( __PACKAGE__->set_primary_key('id'); +sub is_deletable { + my $self = shift; + my $user_id = shift // 0; + + return 0 if $self->is_protected; + return $user_id == $self->publish_user_id; +} 1; diff --git a/templates/archive.html.ep b/templates/archive.html.ep index d06badfc10306a1fb32fa798825fbb8fa38c012c..9ff428e9f95b4c6035a471c43cb3b2463ec8be01 100644 --- a/templates/archive.html.ep +++ b/templates/archive.html.ep @@ -8,9 +8,10 @@ data-url="/api/records"> <thead> <tr> - <th data-field="stream_name" data-width="50" data-width-unit="%">Stream</th> - <th data-field="publish_user_name" data-width="30" data-width-unit="%">Vysílal</th> - <th data-field="begin" data-width="20" data-width-unit="%">Datum vysílání</th> + <th data-field="stream_name" data-width="45" data-width-unit="%" data-sortable="true">Stream</th> + <th data-field="publish_user_name" data-width="30" data-width-unit="%" data-sortable="true">Vysílal</th> + <th data-field="begin" data-width="20" data-width-unit="%" data-sortable="true">Datum vysílání</th> + <th data-field="id" data-width="5" data-width-unit="%" data-formatter="formatterActions"></th> </tr> </thead> </table> @@ -18,8 +19,24 @@ <script> $('#Records').bootstrapTable({ + sortable: true, + pagination: true, + pageSize: 10, + paginationParts: ['pageInfo', 'pageList'], + sidePagination: 'server', onClickCell: function (field, value, row, $element) { - window.location.href ='/archive/' + row.id; + if ( field == 'id' ) { + $.ajax({ + method: 'delete', + url: '/api/records/' + row.id, + success: function(rc) { + $('#Records').bootstrapTable('refresh', {silent: true}) + } + }); + } + else { + window.location.href ='/archive/' + row.id; + } }, }); @@ -31,5 +48,10 @@ function rowStyle(row, index) { } } +function formatterActions(value, row) { + if ( row.is_deletable ) { + return '<i class="fas fa-trash-alt text-3xl" title="Smazat"></i>'; + } +} </script>