From 3d7f60c1c0b48dad2f58bffe3352d1c5c1b93f00 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 1 Dec 2016 13:56:53 +0300 Subject: juick-api: Tags controller from perl, refactoring --- .../java/com/juick/server/MessagesQueries.java | 13 ++++++---- .../src/main/java/com/juick/server/TagQueries.java | 16 +++++++----- .../java/com/juick/server/helpers/TagStats.java | 29 ++++++++++++++++++++++ .../com/juick/server/protocol/JuickProtocol.java | 5 ++-- 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/server/helpers/TagStats.java (limited to 'juick-server/src/main/java/com/juick/server') diff --git a/juick-server/src/main/java/com/juick/server/MessagesQueries.java b/juick-server/src/main/java/com/juick/server/MessagesQueries.java index fd1e5b51..36d311df 100644 --- a/juick-server/src/main/java/com/juick/server/MessagesQueries.java +++ b/juick-server/src/main/java/com/juick/server/MessagesQueries.java @@ -21,6 +21,7 @@ import com.juick.Message; import com.juick.Tag; import com.juick.User; import com.juick.server.helpers.PrivacyOpts; +import com.juick.server.helpers.TagStats; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.springframework.dao.EmptyResultDataAccessException; @@ -308,16 +309,18 @@ public class MessagesQueries { } } - public static List getMessageTags(JdbcTemplate sql, int mid) { + public static List getMessageTags(JdbcTemplate sql, int mid) { return sql.query("SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags " + "INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)", - new Object[]{mid}, (rs, num) -> { + (rs, num) -> { com.juick.Tag t = new com.juick.Tag(StringEscapeUtils.unescapeHtml4(rs.getString(3))); t.TID = rs.getInt(1); t.SynonymID = rs.getInt(2); - t.UsageCnt = rs.getInt(4); - return t; - }); + TagStats s = new TagStats(); + s.setTag(t); + s.setUsageCount(rs.getInt(4)); + return s; + }, mid); } public static List getMessageTagsIDs(JdbcTemplate sql, int mid) { 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 getUserTagsAll(JdbcTemplate sql, int uid) { + public static List 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 updateTags(JdbcTemplate sql, int mid, List newTags) { - List currentTags = MessagesQueries.getMessageTags(sql, mid); + List 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 fromString(JdbcTemplate sql, String txt, boolean tagsOnly) { diff --git a/juick-server/src/main/java/com/juick/server/helpers/TagStats.java b/juick-server/src/main/java/com/juick/server/helpers/TagStats.java new file mode 100644 index 00000000..e8720991 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/helpers/TagStats.java @@ -0,0 +1,29 @@ +package com.juick.server.helpers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.juick.Tag; + +/** + * Created by vitalyster on 01.12.2016. + */ +public class TagStats { + private Tag tag; + private int usageCount; + + public Tag getTag() { + return tag; + } + + public void setTag(Tag tag) { + this.tag = tag; + } + + @JsonProperty("messages") + public int getUsageCount() { + return usageCount; + } + + public void setUsageCount(int usageCount) { + this.usageCount = usageCount; + } +} diff --git a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java index 1bac9fce..68c7c37c 100644 --- a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -8,6 +8,7 @@ import com.juick.Tag; import com.juick.User; import com.juick.formatters.PlainTextFormatter; import com.juick.server.*; +import com.juick.server.helpers.TagStats; import com.juick.server.protocol.annotation.UserCommand; import com.juick.util.TagUtils; import org.springframework.jdbc.core.JdbcTemplate; @@ -179,10 +180,10 @@ public class JuickProtocol { @UserCommand(pattern = "\\*", help = "* - Show your tags") public ProtocolReply commandTags(User currentUser, String... args) { - List tags = TagQueries.getUserTagsAll(sql, currentUser.getUid()); + List tags = TagQueries.getTagsStats(sql, currentUser.getUid()); String msg = "Your tags: (tag - messages)\n" + tags.stream() - .map(t -> String.format("\n*%s - %d", t.getName(), t.UsageCnt)).collect(Collectors.joining()); + .map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining()); return new ProtocolReply(msg, Optional.empty()); } -- cgit v1.2.3