From 8a77f7d91311f8c4d8ef026dacb4abbe0fa764d1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 26 Jan 2016 02:04:46 +0300 Subject: refactoring --- .../java/com/juick/server/MessagesQueries.java | 21 +++++++++--------- src/main/java/com/juick/server/UserQueries.java | 25 ++++++++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 422cfe7d..c4919a8d 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -349,31 +349,30 @@ public class MessagesQueries { } public static List getMyFeed(JdbcTemplate sql, int uid, int before) { - List mids = new ArrayList<>(40); + List mids; if (before > 0) { mids = sql.queryForList("SELECT message_id FROM messages " + "INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) " + "WHERE message_id=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " + "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20", - Integer.class, new Object[]{uid, before, uid}); + Integer.class, uid, before, uid); } else { mids = sql.queryForList("SELECT message_id FROM messages " + "INNER JOIN subscr_users ON (subscr_users.suser_id=? " + "AND subscr_users.user_id=messages.user_id) " + "WHERE (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " + "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20", - Integer.class, new Object[]{uid, uid}); + Integer.class, uid, uid); } if (before > 0) { mids.addAll(sql.queryForList("SELECT message_id FROM messages " + "WHERE user_id=? AND message_id getDiscussions(JdbcTemplate sql, int uid, int before) { if (before > 0) { return sql.queryForList("SELECT message_id FROM subscr_messages WHERE suser_id=? AND message_id 0) { return sql.queryForList("SELECT message_id FROM favorites WHERE user_id " + "IN (SELECT user_id FROM subscr_users WHERE suser_id=?) AND message_id 0) { return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 " + - "AND popular>0 ORDER BY message_id DESC LIMIT 20", new Object[] {before}, Integer.class); + "AND popular>0 ORDER BY message_id DESC LIMIT 20", Integer.class, before); } else { return sql.queryForList("SELECT message_id FROM messages WHERE privacy>0 " + "AND popular>0 ORDER BY message_id DESC LIMIT 20", Integer.class); diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 948b42d4..226e21b1 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -100,14 +100,12 @@ public class UserQueries { public static User getUserByJID(JdbcTemplate sql, String jid) { return sql.queryForObject("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", - new Object[] {jid}, - new UserMapper()); + new UserMapper(), jid); } public static List getUsersByName(JdbcTemplate sql, List unames) { - return sql.query("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", - new UserMapper(), - unames.toArray()); + return sql.query("SELECT id,nick FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", + new UserMapper()); } public static List getUsersByID(JdbcTemplate sql, List uids) { @@ -204,10 +202,19 @@ public class UserQueries { } public static int checkPassword(JdbcTemplate sql, String username, String password) { - String realPassword = sql.queryForObject("SELECT passw FROM users WHERE nick=?", String.class, username); - if (realPassword.equals(password)) { - return UserQueries.getUserByName(sql, username).getUID(); - } else { + try { + String realPassword = sql.queryForObject("SELECT passw FROM users WHERE nick=?", String.class, username); + if (realPassword.equals(password)) { + User user = UserQueries.getUserByName(sql, username); + if (user != null) { + return user.getUID(); + } else { + return -1; + } + } else { + return -1; + } + } catch (EmptyResultDataAccessException e) { return -1; } } -- cgit v1.2.3