diff options
Diffstat (limited to 'juick-server')
-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 |