diff options
author | Yorhel <git@yorhel.nl> | 2019-12-03 17:46:58 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-12-03 17:47:00 +0100 |
commit | 8676d8ed8028cf7efb415c3f3dd1e4d4be9c91f8 (patch) | |
tree | f4235f79367c4ba740cc7d4ea0d782ac022b8ca5 /lib | |
parent | b26fb3e1b488f1e8b7ee2496b7a6e274953a30af (diff) |
API: Add "set ulist" command
TODO:
- Managing labels
- Assigning labels to VNs
Not yet sure what the API for the latter should look like.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Multi/API.pm | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/Multi/API.pm b/lib/Multi/API.pm index 154ec8a2..e58f3972 100644 --- a/lib/Multi/API.pm +++ b/lib/Multi/API.pm @@ -1399,6 +1399,7 @@ sub set { votelist => \&set_votelist, vnlist => \&set_vnlist, wishlist => \&set_wishlist, + ulist => \&set_ulist, ); return cerr $c, parse => 'Invalid arguments to set command' if @arg < 2 || @arg > 3 || ref($arg[0]) @@ -1479,4 +1480,47 @@ sub set_wishlist { [ $vp, $c->{uid}, $obj->{id} ]; } + +sub set_ulist { + my($c, $obj) = @_; + + return setpg $obj, 'DELETE FROM ulist_vns WHERE uid = $1 AND vid = $2', + [ $c->{uid}, $obj->{id} ] if !$obj->{opt}; + + my $opt = $obj->{opt}; + my @set; + my @bind = ($c->{uid}, $obj->{id}); + + if(exists $opt->{vote}) { + return cerr $c, badarg => 'Invalid vote', field => 'vote' if defined($opt->{vote}) && (ref $opt->{vote} || $opt->{vote} !~ /^[0-9]+$/ || $opt->{vote} < 10 || $opt->{vote} > 100); + if($opt->{vote}) { + push @bind, $opt->{vote}; + push @set, 'vote_date = NOW()', 'vote = $'.@bind; + } else { + push @set, 'vote_date = NULL', 'vote = NULL'; + } + } + + if(exists $opt->{notes}) { + return cerr $c, badarg => 'Invalid notes', field => 'notes' if ref $opt->{notes}; + push @bind, $opt->{notes} // ''; + push @set, 'notes = $'.@bind; + } + + for my $f ('started', 'finished') { + if(exists $opt->{$f}) { + return cerr $c, badarg => "Invalid $f date", field => $f if defined $opt->{$f} && (ref $opt->{$f} || $opt->{$f} !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/); + push @bind, $opt->{$f}; + push @set, "$f = \$".@bind; + } + } + + return cerr $c, missing => 'No fields to change' if !@set; + + push @set, 'lastmod = NOW()'; + cpg $c, 'INSERT INTO ulist_vns (uid, vid) VALUES ($1, $2) ON CONFLICT (uid, vid) DO NOTHING', [ $c->{uid}, $obj->{id} ], sub { + setpg $obj, 'UPDATE ulist_vns SET '.join(',', @set).' WHERE uid = $1 AND vid = $2', \@bind; + }; +} + 1; |