summaryrefslogtreecommitdiff
path: root/lib/VNDB/DB/Tags.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2010-12-10 15:55:42 +0100
committerYorhel <git@yorhel.nl>2010-12-10 16:03:09 +0100
commita8c9a132748c77263f7f552b2c6838e5f086307a (patch)
treed6e0bf4153a41d053a9b245899adff8009fbe8ec /lib/VNDB/DB/Tags.pm
parent5c949ff5cb57fc56e05dde010cc852f0f1207ca0 (diff)
Keep track of last modification date for tag<->vn links
Currently unused, but this will be useful in the future. dbTagLinkEdit() is now a lot more complex, since the last modification date will be incorrect for unmodified tag links when we simply delete and re-insert all related links like the old function did.
Diffstat (limited to 'lib/VNDB/DB/Tags.pm')
-rw-r--r--lib/VNDB/DB/Tags.pm31
1 files changed, 27 insertions, 4 deletions
diff --git a/lib/VNDB/DB/Tags.pm b/lib/VNDB/DB/Tags.pm
index bfa56260..93023bc1 100644
--- a/lib/VNDB/DB/Tags.pm
+++ b/lib/VNDB/DB/Tags.pm
@@ -174,11 +174,34 @@ sub dbTagLinks {
# Change a user's tags for a VN entry
# Arguments: uid, vid, [ [ tag, vote, spoiler ], .. ]
sub dbTagLinkEdit {
- my($self, $uid, $vid, $tags) = @_;
- $self->dbExec('DELETE FROM tags_vn WHERE vid = ? AND uid = ?', $vid, $uid);
+ my($self, $uid, $vid, $new) = @_;
+
+ # compare with the old votes and determine which to delete, and/or insert
+ my %old = map +($_->{tag}, [ $_->{vote}, $_->{spoiler} // -1 ]),
+ @{$self->dbTagLinks(vid => $vid, uid => $self->authInfo->{id})};
+ my %new = map +($_->[0], [ $_->[1], $_->[2] ]), @$new;
+
+ my(%delete, %update, %insert);
+ for my $tag (keys %old, keys %new) {
+ if($old{$tag} && !$new{$tag}) {
+ $delete{$tag} = 1;
+ } elsif(!$old{$tag} && $new{$tag}) {
+ $insert{$tag} = $new{$tag};
+ } elsif($old{$tag}[0] != $new{$tag}[0] || $old{$tag}[1] != $new{$tag}[1]) {
+ $update{$tag} = $new{$tag};
+ }
+ }
+
+ # spoiler '-1' -> NULL
+ $new{$_}[1] == -1 && ($new{$_}[1] = undef) for keys %new;
+
+ # perform the changes
+ $self->dbExec('DELETE FROM tags_vn WHERE vid = ? AND uid = ? AND tag IN(!l)',
+ $vid, $uid, [ keys %delete ]) if keys %delete;
$self->dbExec('INSERT INTO tags_vn (tag, vid, uid, vote, spoiler) VALUES (?, ?, ?, ?, ?)',
- $_->[0], $vid, $uid, $_->[1], $_->[2] == -1 ? undef : $_->[2]
- ) for (@$tags);
+ $_, $vid, $uid, $insert{$_}[0], $insert{$_}[1]) for (keys %insert);
+ $self->dbExec('UPDATE tags_vn SET vote = ?, spoiler = ?, date = NOW() WHERE tag = ? AND vid = ? AND uid = ?',
+ $update{$_}[0], $update{$_}[1], $_, $vid, $uid) for (keys %update);
}