aboutsummaryrefslogtreecommitdiff
path: root/src/com/juick/server/VotesQueries.java
blob: d3af99c1ff06ac66684203b81adc8802c08e9b98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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<com.juick.Message> 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;
    }
}