aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-01 13:56:53 +0300
committerGravatar Vitaly Takmazov2016-12-01 13:56:53 +0300
commit3d7f60c1c0b48dad2f58bffe3352d1c5c1b93f00 (patch)
treeab311d5d6102b2c76c03b266fc1b794108cba0fc /juick-server/src/main/java/com/juick/server
parent92d6700919e87c81db4a42a4b286257f62566cbf (diff)
juick-api: Tags controller from perl, refactoring
Diffstat (limited to 'juick-server/src/main/java/com/juick/server')
-rw-r--r--juick-server/src/main/java/com/juick/server/MessagesQueries.java13
-rw-r--r--juick-server/src/main/java/com/juick/server/TagQueries.java16
-rw-r--r--juick-server/src/main/java/com/juick/server/helpers/TagStats.java29
-rw-r--r--juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java5
4 files changed, 50 insertions, 13 deletions
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<com.juick.Tag> getMessageTags(JdbcTemplate sql, int mid) {
+ public static List<TagStats> 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<Integer> 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<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) {
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<Tag> tags = TagQueries.getUserTagsAll(sql, currentUser.getUid());
+ List<TagStats> 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());
}