From 16d02ab9e31cd4a68334575499d039d48a5fb0a9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 19 Dec 2016 14:59:29 +0300 Subject: juick-server: fix updateTags query --- .../java/com/juick/service/TagServiceImpl.java | 37 +++++----------------- 1 file changed, 8 insertions(+), 29 deletions(-) (limited to 'juick-server/src/main/java/com/juick/service') 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 updateTags(final int mid, final Collection newTags) { - List currentTags = getMessageTags(mid); - - if (CollectionUtils.isEmpty(newTags)) - return currentTags.stream().map(TagStats::getTag).collect(Collectors.toList()); - - List ids = new ArrayList<>(newTags.size()); - List 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 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 -- cgit v1.2.3