From 2d6e1dc76e659b78b3103af2bcf1b7f13db62f0a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 30 Jan 2018 14:23:50 +0300 Subject: xmpp: D #message --- .../com/juick/service/MessagesServiceImpl.java | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'juick-server-jdbc/src/main/java/com') 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 2497e94a..b81ef578 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 @@ -48,6 +48,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; /** * Created by aalexeev on 11/13/16. @@ -227,6 +228,15 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } + @Transactional + void updateRepliesBy(int mid) { + List users = getJdbcTemplate().queryForList("SELECT users.nick FROM replies " + + "INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? " + + "GROUP BY replies.user_id ORDER BY COUNT(replies.reply_id) DESC LIMIT 5", String.class, mid); + String result = users.stream().map(u -> "@" + u).collect(Collectors.joining(",")); + getJdbcTemplate().update("UPDATE messages_txt SET repliesby=? WHERE message_id=?", result, mid); + } + @Transactional @Override public boolean recommendMessage(final int mid, final int vuid) { @@ -822,6 +832,30 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } return false; } + @Transactional + @Override + public boolean deleteReply(final int uid, final int mid, final int rid) { + User author = getMessageAuthor(mid); + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("mid", mid) + .addValue("uid", uid) + .addValue("rid", rid); + boolean result; + if (author.getUid() == uid) { + result = getNamedParameterJdbcTemplate() + .update("DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid", sqlParameterSource) > 0; + } else { + result = getNamedParameterJdbcTemplate() + .update("DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid AND user_id=:uid" + , sqlParameterSource) > 0; + } + if (result) { + getNamedParameterJdbcTemplate().update("UPDATE replies SET replies=replies-1 WHERE message_id=:mid", sqlParameterSource); + updateRepliesBy(mid); + return true; + } + return false; + } @Transactional(readOnly = true) @Override -- cgit v1.2.3