From 18c9e608fa38b136e581adb974b29e4c5a446dc9 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 9 Feb 2014 04:54:56 +0700 Subject: Votes --- src/com/juick/server/MessagesQueries.java | 6 +- src/com/juick/server/UserQueries.java | 1 + src/com/juick/server/VotesQueries.java | 91 +++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/com/juick/server/VotesQueries.java (limited to 'src') diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index bf26b70b..dcba6830 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -130,7 +130,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT messages.user_id,users.nick,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.readonly,messages.privacy,messages.replies,messages.attach,messages.place_id,messages.lat,messages.lon FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + stmt = sql.prepareStatement("SELECT messages.user_id,users.nick,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.readonly,messages.privacy,messages.replies,messages.attach,messages.place_id,messages.lat,messages.lon,messages.votes FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); stmt.setInt(1, mid); rs = stmt.executeQuery(); if (rs.first()) { @@ -154,6 +154,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(10); msg.Place.lon = rs.getDouble(11); } + msg.Votes = rs.getInt(12); } } catch (SQLException e) { System.err.println(e); @@ -810,7 +811,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.replies,messages_txt.repliesby,messages.attach,messages.lat,messages.lon FROM (messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id IN (" + Utils.convertArrayInt2String(mids) + ") ORDER BY messages.message_id DESC"); + stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.replies,messages_txt.repliesby,messages.attach,messages.lat,messages.lon,messages.votes FROM (messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id IN (" + Utils.convertArrayInt2String(mids) + ") ORDER BY messages.message_id DESC"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { @@ -837,6 +838,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(13); msg.Place.lon = rs.getDouble(14); } + msg.Votes = rs.getInt(15); msgs.add(msg); } diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 8d2d0e60..a47ef815 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -284,6 +284,7 @@ public class UserQueries { user = new com.juick.User(); user.UID = rs.getInt(1); user.UName = rs.getString(2); + user.AuthHash = hash; } } catch (SQLException e) { System.err.println(e); diff --git a/src/com/juick/server/VotesQueries.java b/src/com/juick/server/VotesQueries.java new file mode 100644 index 00000000..d3af99c1 --- /dev/null +++ b/src/com/juick/server/VotesQueries.java @@ -0,0 +1,91 @@ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author ugnich + */ +public class VotesQueries { + + public static boolean voteMessage(Connection sql, int mid, int uid, int vote) { + boolean ret = false; + + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO messages_votes(message_id,user_id,vote) VALUES (?,?,?)"); + stmt.setInt(1, mid); + stmt.setInt(2, uid); + stmt.setInt(3, vote); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (ret) { + try { + String query; + if (vote > 0) { + query = "UPDATE messages SET votes=votes+1 WHERE message_id=?"; + } else { + query = "UPDATE messages SET votes=votes-1 WHERE message_id=?"; + } + stmt = sql.prepareStatement(query); + stmt.setInt(1, mid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return ret; + } + + public static int getMessageVotes(Connection sql, int mid) { + return SQLHelpers.getInt(sql, "SELECT votes FROM messages WHERE message_id=?", mid, 0); + } + + public static boolean fillMessagesVotes(Connection sql, ArrayList msgs, int vuid) { + boolean ret = false; + + String mids = ""; + final int midsSize = msgs.size(); + for (int i = 0; i < midsSize; i++) { + if (i > 0) { + mids += ","; + } + mids += msgs.get(i).MID; + } + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT message_id,vote FROM messages_votes WHERE user_id=? AND message_id IN (" + mids + ")"); + stmt.setInt(1, vuid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + int mid = rs.getInt(1); + for (int i = 0; i < midsSize; i++) { + if (msgs.get(i).MID == mid) { + msgs.get(i).UserVote = rs.getInt(2); + ret = true; + } + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } +} -- cgit v1.2.3