aboutsummaryrefslogtreecommitdiff
path: root/src/Store/User.java
blob: e414520a083608735df2e15fb9ba9ec2119eaafc (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package Store;

import Server.JSONMessage;
import com.corundumstudio.socketio.listener.DataListener;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class User {
    private int id;
    private final String userName;
    private final String password;

    public User(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public boolean save() throws SQLException {
        if (!checkUniqueUsername(userName)) {
            return false;
        }

        String sql = "INSERT INTO User (userName, password) VALUES (?, ?)";
        PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);

        statement.setString(1, userName);
        statement.setString(2, password);

        statement.executeUpdate();

        return true;
    }

    private boolean checkUniqueUsername(String userName) throws SQLException {
        String sql = "SELECT * FROM User WHERE userName = '" + userName + "'";
        Statement statement = ConnectionHandler.connection.createStatement();

        ResultSet response = statement.executeQuery(sql);
        while (response.next()) {
            return false;
        }

        return true;
    }

    public boolean login() throws SQLException {
        String sql = "SELECT * FROM User WHERE userName = '" + this.userName + "' AND password = '" + this.password + "'";
        Statement statement = ConnectionHandler.connection.createStatement();

        boolean loggedIn = false;
        ResultSet response = statement.executeQuery(sql);
        while (response.next()) {
            this.id = response.getInt("ID");
            loggedIn = true;
        }

        return loggedIn;
    }

    public static void createTable() throws SQLException {
        String sql = "CREATE TABLE IF NOT EXISTS User (" +
                "ID INTEGER NOT NULL PRIMARY KEY," +
                "userName TEXT NOT NULL," +
                "password text NOT NULL" +
                ");";

        Statement statement = ConnectionHandler.connection.createStatement();
        statement.execute(sql);
    }

    public static DataListener<JSONMessage> signUpHandler() {
        return ((client, data, ackSender) -> {
            Map<String,String> d = data.getData();

            User user = new User(d.get("userName"), d.get("password"));
            try {
                if (user.save()) {
                    client.sendEvent("signedUp");
                } else {
                    client.sendEvent("usernameTaken");
                    client.sendEvent("signUpFailed");
                }
            } catch (SQLException e) {
                client.sendEvent("signUpFailed");
                System.out.println(e.getMessage());
            }
        });
    }

    public static DataListener<JSONMessage> loginHandler() {
        return ((client, data, ackSender) -> {
            Map<String,String> d = data.getData();

            User user = new User(d.get("userName"), d.get("password"));
            try {
                user.login();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }

            if (user.login()) {
                client.sendEvent("loggedIn", user.id);
            } else {
                client.sendEvent("loginFailed");
            }
        });
    }

    public String toJSON() {
        return "{" +
                "\"id\": "       + id       + "," +
                "\"userName\": " + userName + "," +
                "\"password\": " + password +
                " }";
    }
}