From: Damyan Ivanov Date: Tue, 16 Nov 2021 06:25:22 +0000 (+0000) Subject: implement unwanted album list manipulation X-Git-Url: https://git.ktnx.net/?p=mpd-feeder.git;a=commitdiff_plain;h=e72d5851c56146ac78be91b720ed7d7451fcc0d0 implement unwanted album list manipulation --- diff --git a/lib/App/MPD/Feeder/Command.pm b/lib/App/MPD/Feeder/Command.pm index 9a425be..d2c4130 100644 --- a/lib/App/MPD/Feeder/Command.pm +++ b/lib/App/MPD/Feeder/Command.pm @@ -3,7 +3,7 @@ package App::MPD::Feeder::Command; use strict; use warnings; use utf8; -use feature 'say'; +use feature qw(fc say); use Log::Any qw($log); use Object::Pad; @@ -65,7 +65,53 @@ isa App::MPD::Feeder { } if ( $cmd eq 'add-unwanted-album' ) { - die "NOT IMPLEMENTED\n"; + die + "Syntax: mpd-feeder add-unwanted-album «album name» by «artist name»\n" + unless @args == 3 and $args[1] =~ /^by$/i; + $self->set_db_needs_update(0); + my ( $album, $artist ) = @args[ 0, 2 ]; + if ( $self->db->add_unwanted_album( $album, $artist ) ) { + $log->info( + "Album «$album» by «$artist» added to the unwanted list\n" + ); + } + else { + $log->warn( + "Album «$album» by «$artist» already in the unwanted list\n" + ); + } + + return 0; + } + + if ( $cmd eq 'del-unwanted-album' ) { + die + "Syntax: mpd-feeder del-unwanted-album «album name» by «artist name»\n" + unless @args == 3 and $args[1] =~ /^by$/i; + $self->set_db_needs_update(0); + my ( $album, $artist ) = @args[ 0, 2 ]; + if ( $self->db->del_unwanted_album( $album, $artist ) ) { + $log->info( + "Album «$album» by «$artist» deleted from the unwanted list\n" + ); + } + else { + $log->warn( + "Album «$album» by «$artist» is not in the unwanted list\n" + ); + } + + return 0; + } + + if ( $cmd eq 'list-unwanted-albums' ) { + die "This command has no arguments\n" if @args; + $self->set_db_needs_update(0); + my $count = $self->db->walk_unwanted_albums( + sub ( $album, $artist ) { say "«$album» by «$artist»" } ); + say "Total unwanted albums: $count"; + + return 0; } if ( $cmd eq 'one-shot' ) { diff --git a/lib/App/MPD/Feeder/DB.pm b/lib/App/MPD/Feeder/DB.pm index 6216436..5ba1dde 100644 --- a/lib/App/MPD/Feeder/DB.pm +++ b/lib/App/MPD/Feeder/DB.pm @@ -293,6 +293,59 @@ SQL return $count; } + method add_unwanted_album($album, $artist) { + $self->connect; + + try { + $db->do( + <<'SQL', +INSERT INTO unwanted_albums(album, artist, generation) +VALUES($1, $2, $3) +SQL + undef, $album, $artist, $generation + ); + return 1; + } + catch { + my $err = $@; + + $log->debug("PostgreSQL error: $err"); + $log->debug( "SQLSTATE = " . $db->state ); + return 0 if $db->state eq '23505'; + + die $err; + } + } + + method del_unwanted_album($album, $artist) { + $self->connect; + + return 1 == $db->do( + <<'SQL', +DELETE FROM unwanted_albums +WHERE album = $1 AND artist = $2 +SQL + undef, $album, $artist + ); + } + + method walk_unwanted_albums($callback) { + $self->connect; + + my $count = 0; + + my $sth = $db->prepare('SELECT album, artist FROM unwanted_albums ORDER BY 2, 1'); + my ( $album, $artist ); + $sth->execute; + $sth->bind_columns( \$album, \$artist ); + while ( $sth->fetchrow_arrayref ) { + $count++; + $callback->($album, $artist); + } + + return $count; + } + method disconnect { return unless $db;