aboutsummaryrefslogtreecommitdiff
path: root/src/Store
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-16 02:16:16 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-16 02:16:16 +0530
commitda6b366397aa15e2686840c1d9ffa5dae5d49d2a (patch)
treea53bc42092a564bfbf66a67b70e3aa9cb8689798 /src/Store
First Commit
Diffstat (limited to 'src/Store')
-rw-r--r--src/Store/Book.java214
-rw-r--r--src/Store/ConnectionHandler.java28
-rw-r--r--src/Store/User.java120
3 files changed, 362 insertions, 0 deletions
diff --git a/src/Store/Book.java b/src/Store/Book.java
new file mode 100644
index 0000000..4232d77
--- /dev/null
+++ b/src/Store/Book.java
@@ -0,0 +1,214 @@
+package Store;
+
+import Server.JSONMessage;
+import Server.SocketServer;
+import UI.MainWindow;
+import com.corundumstudio.socketio.listener.DataListener;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.Map;
+
+public class Book {
+ private int id;
+ private String title;
+ private String author;
+ private int issuedBy;
+ private String issuedByName = "";
+
+ public Book(int id) {
+ this.id = id;
+ }
+
+ public Book(String title, String author) {
+ this.title = title;
+ this.author = author;
+ this.issuedBy = 0; // 0 means available since IDs start with 1
+ }
+
+ public Book(int id, String title, String author, int issuedBy, String issuedByName) {
+ this.id = id;
+ this.title = title;
+ this.author = author;
+ this.issuedBy = issuedBy;
+ this.issuedByName = issuedByName;
+ }
+
+ public String getTitle() {
+ return this.title;
+ }
+
+ public String getAuthor() {
+ return this.author;
+ }
+
+ public int getIssuedBy() {
+ return this.issuedBy;
+ }
+
+ public int getId() {
+ return this.id;
+ }
+
+ public String getIssuedByName() {
+ return issuedByName;
+ }
+
+ public int bookIssuedBy() throws SQLException {
+ String sql = "SELECT issuedBy from Book where ID = " + this.id;
+ Statement statement = ConnectionHandler.connection.createStatement();
+
+ ResultSet response = statement.executeQuery(sql);
+ while (response.next()) {
+ return response.getInt("issuedBy");
+ }
+
+ return 0;
+ }
+
+ public boolean issue(int userId) throws SQLException {
+ if (bookIssuedBy() == 0) {
+ String sql = "UPDATE Book SET issuedBy = ? where ID = ?";
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setInt(1, userId);
+ statement.setInt(2, this.id);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public boolean returnBook(int userId) throws SQLException {
+ if (bookIssuedBy() == userId) {
+ String sql = "UPDATE Book SET issuedBy = 0 where ID = ?";
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setInt(1, this.id);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public void save() throws SQLException {
+ String sql = "INSERT INTO Book (title, author, issuedBy) VALUES (?, ?, ?)";
+
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setString(1, title);
+ statement.setString(2, author);
+ statement.setInt(3, issuedBy);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+ }
+
+ public void delete() throws SQLException {
+ String sql = "DELETE FROM Book WHERE ID = ?";
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setInt(1, this.id);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+ }
+
+ public static ArrayList<Book> getAll(boolean availableOnly) throws SQLException {
+ ArrayList<Book> books = new ArrayList<>();
+
+ String sql;
+ if (availableOnly) {
+ sql = "SELECT * FROM Book WHERE issuedBy = 0";
+ } else {
+ sql = "SELECT Book.ID, Book.title, Book.issuedBy, Book.author, " +
+ "User.userName FROM Book LEFT JOIN User ON User.ID = Book.issuedBy";
+ }
+ Statement statement = ConnectionHandler.connection.createStatement();
+
+ ResultSet response = statement.executeQuery(sql);
+ while (response.next()) {
+ books.add(new Book(response.getInt("ID"), response.getString("title"), response.getString("author"), response.getInt("issuedBy"), response.getString("userName")));
+ }
+
+ return books;
+ }
+
+ public static DataListener<JSONMessage> getListHandler(boolean availableOnly) {
+ return ((client, data, ackSender) -> {
+ ArrayList<Book> books = getAll(availableOnly);
+ client.sendEvent(availableOnly ? "availableBooksList" : "allBooksList", books);
+ });
+ }
+
+ public static DataListener<JSONMessage> issueHandler() {
+ return ((client, data, ackSender) -> {
+ Map d = data.getData();
+
+ int userId = (int) d.get("id");
+ Book b = new Book((int) d.get("bookId"));
+
+ if (b.issue(userId)) {
+ client.sendEvent("issued");
+ } else {
+ client.sendEvent("alreadyIssued");
+ }
+ });
+ }
+
+ public static DataListener<JSONMessage> returnHandler() {
+ return ((client, data, ackSender) -> {
+ Map d = data.getData();
+
+ int userId = (int) d.get("id");
+ Book b = new Book((int) d.get("bookId"));
+
+ if (b.returnBook(userId)) {
+ client.sendEvent("returned");
+ } else {
+ client.sendEvent("returnFailed");
+ }
+ });
+ }
+
+ public static void createTable() throws SQLException {
+ String sql = "CREATE TABLE IF NOT EXISTS Book (" +
+ "ID INTEGER NOT NULL PRIMARY KEY, " +
+ "title TEXT NOT NULL, " +
+ "author text NOT NULL, " +
+ "issuedBy INTEGER NOT NULL," +
+ "FOREIGN KEY(issuedBy) REFERENCES User(ID)" +
+ ");";
+
+ Statement statement = ConnectionHandler.connection.createStatement();
+ statement.execute(sql);
+ }
+}
diff --git a/src/Store/ConnectionHandler.java b/src/Store/ConnectionHandler.java
new file mode 100644
index 0000000..45d4446
--- /dev/null
+++ b/src/Store/ConnectionHandler.java
@@ -0,0 +1,28 @@
+package Store;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public class ConnectionHandler {
+ String address;
+ public static Connection connection;
+
+ public ConnectionHandler(String address) {
+ this.address = address;
+ }
+
+ public void connect() {
+ try {
+ connection = DriverManager.getConnection(this.address);
+
+ // create tables if they don't exist
+ Book.createTable();
+ User.createTable();
+ } catch (SQLException e) {
+ System.err.println(e.getMessage());
+ }
+ }
+
+ // TODO: disconnect
+}
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<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 +
+ " }";
+ }
+} \ No newline at end of file