diff options
author | 2016-12-01 13:56:53 +0300 | |
---|---|---|
committer | 2016-12-01 13:56:53 +0300 | |
commit | 3d7f60c1c0b48dad2f58bffe3352d1c5c1b93f00 (patch) | |
tree | ab311d5d6102b2c76c03b266fc1b794108cba0fc /juick-server/src/main/java/com/juick/server/TagQueries.java | |
parent | 92d6700919e87c81db4a42a4b286257f62566cbf (diff) |
juick-api: Tags controller from perl, refactoring
Diffstat (limited to 'juick-server/src/main/java/com/juick/server/TagQueries.java')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/TagQueries.java | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/juick-server/src/main/java/com/juick/server/TagQueries.java b/juick-server/src/main/java/com/juick/server/TagQueries.java index ee9a1e21..6d416a37 100644 --- a/juick-server/src/main/java/com/juick/server/TagQueries.java +++ b/juick-server/src/main/java/com/juick/server/TagQueries.java @@ -18,6 +18,7 @@ package com.juick.server; import com.juick.Tag; +import com.juick.server.helpers.TagStats; import org.apache.commons.lang3.StringEscapeUtils; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -109,15 +110,16 @@ public class TagQueries { return holder.getKey().intValue(); } - public static List<com.juick.Tag> getUserTagsAll(JdbcTemplate sql, int uid) { + public static List<TagStats> getTagsStats(JdbcTemplate sql, int uid) { return sql.query("SELECT tags.name,COUNT(messages.message_id) " + "FROM (messages INNER JOIN messages_tags ON (messages.user_id=? " + "AND messages.message_id=messages_tags.message_id)) " + "INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id ORDER BY tags.name ASC", (rs, rowNum) -> { - Tag t = new Tag(StringEscapeUtils.unescapeHtml4(rs.getString(1))); - t.UsageCnt = rs.getInt(2); - return t; + TagStats s = new TagStats(); + s.setTag(new Tag(StringEscapeUtils.unescapeHtml4(rs.getString(1)))); + s.setUsageCount(rs.getInt(2)); + return s; }, uid); } @@ -132,12 +134,14 @@ public class TagQueries { .map(StringEscapeUtils::unescapeHtml4).collect(Collectors.toList()); } public static List<Tag> updateTags(JdbcTemplate sql, int mid, List<Tag> newTags) { - List<Tag> currentTags = MessagesQueries.getMessageTags(sql, mid); + List<Tag> currentTags = MessagesQueries.getMessageTags(sql, mid).stream() + .map(TagStats::getTag).collect(Collectors.toList()); newTags.stream().filter(currentTags::contains) .forEach(t -> sql.update("DELETE FROM messages_tags WHERE message_id=? AND tag_id=?", mid, t.TID)); newTags.stream().filter(t -> !currentTags.contains(t)) .forEach(t -> sql.update("INSERT INTO messages_tags(message_id,tag_id) VALUES (?,?)", mid, t.TID)); - return MessagesQueries.getMessageTags(sql, mid); + return MessagesQueries.getMessageTags(sql, mid).stream() + .map(TagStats::getTag).collect(Collectors.toList()); } public static List<Tag> fromString(JdbcTemplate sql, String txt, boolean tagsOnly) { |