From f3963f0b12055ba6710b9b28ddc93a5b5fe91d67 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 22 Sep 2017 16:39:00 +0300 Subject: Initial getNotifications query * using Java 8 DateTime API where possible --- .../com/juick/service/MessagesServiceImpl.java | 33 ++++++++++++++++++++-- .../com/juick/service/PMQueriesServiceImpl.java | 6 ++-- 2 files changed, 33 insertions(+), 6 deletions(-) (limited to 'juick-server-jdbc/src/main') 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 3533d701..c23b04e4 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 @@ -40,7 +40,11 @@ import org.springframework.util.Assert; import javax.inject.Inject; import java.sql.*; -import java.util.*; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; /** * Created by aalexeev on 11/13/16. @@ -74,7 +78,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ user.setBanned(rs.getBoolean(6)); msg.setUser(user); msg.TimeAgo = rs.getInt(7); - msg.setDate(rs.getTimestamp(8)); + msg.setTimestamp(rs.getTimestamp(8).toInstant()); msg.ReadOnly = rs.getBoolean(9); msg.setPrivacy(rs.getInt(10)); msg.FriendsOnly = msg.getPrivacy() < 0; @@ -322,7 +326,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ msg.getUser().setUid(rs.getInt(1)); msg.getUser().setName(rs.getString(2)); msg.setReplyto(rs.getInt(3)); - msg.setDate(rs.getTimestamp(4)); + msg.setTimestamp(rs.getTimestamp(4).toInstant()); msg.setAttachmentType(rs.getString(5)); msg.setText(rs.getString(6)); String quote = rs.getString(7); @@ -834,4 +838,27 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return reply; }, -hours); } + + @Transactional(readOnly = true) + @Override + public List getNotifications(User user, LocalDateTime before) { + return getNamedParameterJdbcTemplate().query("SELECT replies.message_id as mid, replies.reply_id, replies.replyto, " + + "replies.user_id, users.nick, users.banned, " + + "TIMESTAMPDIFF(MINUTE, replies.ts, NOW()), replies.ts, " + + "0 as readonly, 0 as privacy, 0 as replies, " + + "replies.attach, 0 as place_id, 0 as lat, " + + "0 as lon, 0 as likes, 0 as hidden, " + + "NULL as tags, NULL as repliesby, replies.txt, " + + "IFNULL(qw.txt, t.txt) as q FROM replies INNER JOIN users " + + "ON replies.user_id = users.id " + + "LEFT JOIN replies qw ON replies.message_id = qw.message_id and replies.replyto = qw.reply_id " + + "LEFT JOIN messages_txt t on replies.message_id = t.message_id " + + "WHERE EXISTS (SELECT 1 FROM subscr_messages WHERE suser_id=:uid " + + "AND replies.message_id=message_id) " + + (before != null ? "AND replies.ts < :before " : StringUtils.EMPTY) + + "ORDER BY ts DESC LIMIT 20", new MapSqlParameterSource() + .addValue("uid", user.getUid()) + .addValue("before", before), + new MessageMapper()); + } } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java index 6b13f7bc..80be200b 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java @@ -125,7 +125,7 @@ public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesSe msg.setUser(new User()); msg.getUser().setUid(uuid); msg.setText(rs.getString(2)); - msg.setDate(rs.getTimestamp(3)); + msg.setTimestamp(rs.getTimestamp(3).toInstant()); return msg; }); @@ -149,7 +149,7 @@ public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesSe msg.getUser().setName(rs.getString(2)); msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); + msg.setTimestamp(rs.getTimestamp(5).toInstant()); return msg; }, uid); @@ -169,7 +169,7 @@ public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesSe msg.getUser().setName(rs.getString(2)); msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); + msg.setTimestamp(rs.getTimestamp(5).toInstant()); return msg; }, uid); -- cgit v1.2.3