From 842e903d5b987dd23e82a423bddae75acdea0c9c Mon Sep 17 00:00:00 2001
From: mykhailo.dubovskyi
Date: Wed, 18 Apr 2018 15:57:53 +0300
Subject: Like -> Reaction
---
.../com/juick/service/MessagesServiceImpl.java | 72 +++++++++++++++++++---
1 file changed, 65 insertions(+), 7 deletions(-)
(limited to 'juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java')
diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
index c0dccc6a..362a918c 100644
--- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -17,9 +17,7 @@
package com.juick.service;
-import com.juick.Message;
-import com.juick.Tag;
-import com.juick.User;
+import com.juick.*;
import com.juick.server.helpers.PrivacyOpts;
import com.juick.server.helpers.ResponseReply;
import com.juick.util.MessageUtils;
@@ -105,6 +103,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
}
}
+
+
/**
* @see Java, JDBC and MySQL Types
*/
@@ -226,12 +226,12 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Override
public RecommendStatus recommendMessage(final int mid, final int vuid) {
int wasDeleted = getJdbcTemplate()
- .update("DELETE FROM favorites WHERE user_id=? AND message_id=?", vuid, mid);
+ .update("DELETE FROM favorites WHERE user_id=? AND message_id=? and like_id=?", vuid, mid, Reaction.LIKE);
if (wasDeleted > 0) {
return RecommendStatus.Deleted;
} else {
boolean wasAdded = getJdbcTemplate()
- .update("INSERT INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", vuid, mid) == 1;
+ .update("INSERT INTO favorites(user_id, message_id, ts, like_id ) VALUES (?, ?, NOW(), ?)", vuid, mid,Reaction.LIKE) == 1;
if (wasAdded) {
return RecommendStatus.Added;
}
@@ -239,6 +239,19 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
return RecommendStatus.Error;
}
+ @Override
+ public RecommendStatus likeMessage(int mid, int vuid, int reactionId) throws IllegalArgumentException {
+ if(reactionId == Reaction.LIKE) throw new IllegalArgumentException("this reaction type does not support here");
+
+ boolean wasAdded = getJdbcTemplate()
+ .update("INSERT INTO favorites(user_id, message_id, ts, like_id ) VALUES (?, ?, NOW(), ?)", vuid, mid, reactionId) == 1;
+ if (wasAdded) {
+ return RecommendStatus.Added;
+ }
+
+ return RecommendStatus.Error;
+ }
+
@Transactional(readOnly = true)
@Override
public boolean canViewThread(final int mid, final int uid) {
@@ -299,6 +312,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
public com.juick.Message getMessage(final int mid) {
+
List list = getJdbcTemplate().query(
"SELECT messages.message_id as mid, 0 as rid, 0 as replyto, "
+ "messages.user_id as uid, users.nick, users.banned as banned, "
@@ -319,8 +333,13 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "privacy, replies, attach, place_id, lat, lon, tags, repliesby, q",
new MessageMapper(),
mid);
-
- return list.isEmpty() ? null : list.get(0);
+ if (!list.isEmpty()) {
+ final Message message = list.get(0);
+ Map> reactionStats = updateReactionsFor(Collections.singletonList(mid));
+ message.setReactions(reactionStats.get(message.getMid()));
+ return message;
+ }
+ return null;
}
@Transactional(readOnly = true)
@@ -740,6 +759,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Override
public List getMessages(final List mids) {
if (CollectionUtils.isNotEmpty(mids)) {
+
List msgs = getNamedParameterJdbcTemplate().query(
"SELECT messages.message_id, 0 as rid, 0 as replyto, "
+ "messages.user_id,users.nick, 0 as banned, "
@@ -758,12 +778,50 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "WHERE messages.message_id IN (:ids) GROUP BY messages.message_id",
new MapSqlParameterSource("ids", mids),
new MessageMapper());
+
+
+ Map> likes = updateReactionsFor(mids);
+
+ msgs.forEach(i -> i.setReactions(likes.get(i.getMid())));
+
msgs.sort(Comparator.comparing(item -> mids.indexOf(item.getMid())));
+
return msgs;
}
return Collections.emptyList();
}
+
+ @Transactional(readOnly = true)
+ @Override
+ public Map> updateReactionsFor(final List mids) {
+
+ return getNamedParameterJdbcTemplate().query("select f.message_id as mid, f.like_id as lid, " +
+ "r.description as desc, count(f.like_id) as count" +
+ " from favorites f LEFT JOIN reactions r ON f.like_id = r.like_id " +
+ " where f.message_id IN (:mids) " +
+ " group by f.message_id, f.like_id", new MapSqlParameterSource("mids", mids), (ResultSet rs) -> {
+ Map> results = new HashMap<>();
+
+
+ while (rs.next()) {
+ int messageId = rs.getInt("message_id");
+ int likeId = rs.getInt("like_id");
+ int count = rs.getInt("count");
+ String description = rs.getString("description");
+ Reaction reaction = new Reaction(likeId);
+ reaction.setCount(count);
+ reaction.setDescription(description);
+ results.computeIfAbsent(messageId, HashSet::new);
+ results.get(messageId).add(reaction);
+ }
+
+ return results;
+ });
+
+ }
+
+
@Transactional
@Override
public List getReplies(final User user, final int mid) {
--
cgit v1.2.3