From da6b366397aa15e2686840c1d9ffa5dae5d49d2a Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 16 Nov 2024 02:16:16 +0530 Subject: First Commit --- src/Store/User.java | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/Store/User.java (limited to 'src/Store/User.java') diff --git a/src/Store/User.java b/src/Store/User.java new file mode 100644 index 0000000..e414520 --- /dev/null +++ b/src/Store/User.java @@ -0,0 +1,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 signUpHandler() { + return ((client, data, ackSender) -> { + Map 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 loginHandler() { + return ((client, data, ackSender) -> { + Map 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 + + " }"; + } +} \ No newline at end of file -- cgit v1.2.3