package database_comm;
import java.sql.*;

public class UserDatabase
{
	//Debug user class for SQL connection
	//TODO remove
	public static class User
	{
		public String username;
		public String passwordHash;
		public String firstName;
		public String lastName;
		public boolean isAdmin;

		public String toString()
		{
			return username + ", " + passwordHash + ", " + firstName + " " + lastName + (isAdmin ? ", admin" : ", user");
		}
	}

	//Returns an user with the given credentials from the database. null if not found
	public static User getUserFromLogin(String username, String passwordHash)
	{

		//Create and execute query to find user with matching credentials
		DatabaseConnection connection = new DatabaseConnection();
		String query = "SELECT * FROM USERS WHERE USERNAME = '" + username + "' AND PASS_HASH = '" +  passwordHash + "'";
		ResultSet rs = connection.executeQuery(query);
		
		//Set up our return user with null by default
		User returnUser = null;
		
		try
		{
			//Check if our query got any results
			if (rs.next())
			{
				//If so, create user instance from result data
				returnUser = new User();
				returnUser.username = rs.getString("USERNAME");
				returnUser.passwordHash = rs.getString("PASS_HASH");
				returnUser.firstName = rs.getString("FIRST");
				returnUser.lastName = rs.getString("LAST");
				returnUser.isAdmin = rs.getBoolean("IS_ADMIN");
			}
			else
			{
				//If not, print that the user was not found
				System.out.println("User with the given credentials not found");
			}			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		//Close the connection and return our results
		connection.close();		
		return returnUser;
	}
	
	//Adds the given user to the database, returns true if successful
	public static boolean addUserToDatabase(User user)
	{
		//Create database connection
		DatabaseConnection connection = new DatabaseConnection();

		//Set up query to insert into USERS table and execute it
		String query = "INSERT INTO USERS (USERNAME, PASS_HASH, FIRST, LAST, IS_ADMIN) "
				+ "VALUES ('" + user.username + "', '" + user.passwordHash + "', '" + user.firstName + "', '" + user.lastName +
				"', " + (user.isAdmin ? "1" : "0" ) + ")";
		//System.out.println(query);
		connection.execute(query);
		
		
		return false;
	}
	
	//Placeholder get cash string
	//TODO remove
	public static String getCashString(int amount)
	{
		int cents = amount % 100;
		int dollars = amount - cents;
		dollars /= 100;
		return dollars + "." + cents;
	}


}

@@ -36,410 +36,97 @@
 sql.
-*;%0A%0Apublic class UserDatabase%0A%7B%0A%09//Debug user class for SQL connection%0A%09//TODO remove%0A%09public static class User%0A%09%7B%0A%09%09public String username;%0A%09%09public String passwordHash;%0A%09%09public String firstName;%0A%09%09public String lastName;%0A%09%09public boolean isAdmin;%0A%0A%09%09public String toString()%0A%09%09%7B%0A%09%09%09return username + %22, %22 + passwordHash + %22, %22 + firstName + %22 %22 + lastName + (isAdmin ? %22, admin%22 : %22, user%22);%0A%09%09%7D%0A%09%7D%0A
+ResultSet;%0Aimport java.sql.SQLException;%0A%0Aimport login.User;%0A%0Apublic class UserDatabase%0A%7B
 %0A%09//
@@ -811,60 +811,16 @@
 ser(
-);%0A%09%09%09%09returnUser.username = rs.getString(%22USERNAME%22
+username
 );%0A%09
@@ -829,27 +829,29 @@
 %09returnUser.
-pas
+setPa
 swordHash = 
@@ -851,35 +851,21 @@
 Hash
- = rs.getString(%22PASS_HASH%22
+(passwordHash
 );%0A%09
@@ -878,25 +878,28 @@
 urnUser.
-f
+setF
 irstName
  = rs.ge
@@ -886,27 +886,25 @@
 setFirstName
- = 
+(
 rs.getString
@@ -908,24 +908,25 @@
 ing(%22FIRST%22)
+)
 ;%0A%09%09%09%09return
@@ -934,19 +934,20 @@
 ser.
-l
+setL
 astName
- = 
+(
 rs.g
@@ -962,16 +962,17 @@
 (%22LAST%22)
+)
 ;%0A%09%09%09%09re
@@ -984,18 +984,17 @@
 ser.
-i
 s
+et
 Admin
- = 
+(
 rs.g
@@ -1014,16 +1014,17 @@
 _ADMIN%22)
+)
 ;%0A%09%09%09%7D%0A%09
@@ -1703,24 +1703,29 @@
  + user.
-u
+getU
 sername
+()
  + %22', '
@@ -1733,19 +1733,21 @@
  + user.
-pas
+getPa
 swordHas
@@ -1743,24 +1743,26 @@
 tPaswordHash
+()
  + %22', '%22 + 
@@ -1762,25 +1762,32 @@
 , '%22
- 
+%0A%09%09%09%09
 + user.
-f
+getF
 irstName
  + %22
@@ -1782,16 +1782,18 @@
 irstName
+()
  + %22', '
@@ -1805,16 +1805,21 @@
 ser.
-l
+getL
 astName
+()
  +%0A%09
@@ -1842,16 +1842,18 @@
 .isAdmin
+()
  ? %221%22 :
@@ -1868,18 +1868,16 @@
  %22)%22;%0A%09%09
-//
 System.o

Implement User class in UserDatabase