diff options
author | Vitaly Takmazov | 2016-12-19 14:59:29 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-12-19 14:59:29 +0300 |
commit | 16d02ab9e31cd4a68334575499d039d48a5fb0a9 (patch) | |
tree | cb28ca9fa739859ee6ce2a9788c67d0af6943d6d /juick-server/src/main/java/com/juick | |
parent | bb31e52ad79ed43269e7946a344a42192d21684a (diff) |
juick-server: fix updateTags query
Diffstat (limited to 'juick-server/src/main/java/com/juick')
-rw-r--r-- | juick-server/src/main/java/com/juick/service/TagServiceImpl.java | 37 |
1 files changed, 8 insertions, 29 deletions
diff --git a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server/src/main/java/com/juick/service/TagServiceImpl.java index 23b8d2a9..c1689249 100644 --- a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/TagServiceImpl.java @@ -189,35 +189,14 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { @Transactional @Override public List<Tag> updateTags(final int mid, final Collection<Tag> newTags) { - List<TagStats> currentTags = getMessageTags(mid); - - if (CollectionUtils.isEmpty(newTags)) - return currentTags.stream().map(TagStats::getTag).collect(Collectors.toList()); - - List<Integer> ids = new ArrayList<>(newTags.size()); - List<Object[]> params = new ArrayList<>(newTags.size()); - - newTags.stream().forEach(tag -> { - if (currentTags.contains(tag)) - ids.add(tag.TID); - else - params.add(new Object[]{mid, tag.TID}); - }); - - if (!params.isEmpty()) - getJdbcTemplate().batchUpdate( - "INSERT INTO messages_tags(message_id,tag_id) VALUES (?, ?)", - params, - new int[]{Types.INTEGER, Types.INTEGER}); - - if (!ids.isEmpty()) - getNamedParameterJdbcTemplate().update( - "DELETE FROM messages_tags WHERE message_id = :mid AND tag_id in (:ids)", - new MapSqlParameterSource() - .addValue("mid", mid) - .addValue("ids", ids)); - - return getMessageTags(mid).stream().map(TagStats::getTag).collect(Collectors.toList()); + List<Tag> currentTags = getMessageTags(mid).stream() + .map(TagStats::getTag).collect(Collectors.toList()); + newTags.stream().filter(currentTags::contains) + .forEach(t -> getJdbcTemplate().update("DELETE FROM messages_tags WHERE message_id=? AND tag_id=?", mid, t.TID)); + newTags.stream().filter(t -> !currentTags.contains(t)) + .forEach(t -> getJdbcTemplate().update("INSERT INTO messages_tags(message_id,tag_id) VALUES (?,?)", mid, t.TID)); + return getMessageTags(mid).stream() + .map(TagStats::getTag).collect(Collectors.toList()); } @Override |