136 lines
4.9 KiB
Java
136 lines
4.9 KiB
Java
package Database;
|
|
|
|
import static android.util.Log.println;
|
|
|
|
import android.content.ContentValues;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
import android.util.Log;
|
|
import androidx.annotation.Nullable;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import Model.Habit;
|
|
import Model.User;
|
|
|
|
public class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
public static final String USER_TABLE = "USER_TABLE";
|
|
public static final String HABIT_TABLE = "HABIT_TABLE";
|
|
public static final String COLUMN_USER_ID = "USER_ID";
|
|
public static final String COLUMN_USERNAME = "USERNAME";
|
|
public static final String COLUMN_PASSWORD = "PASSWORD";
|
|
public static final String COLUMN_HABIT_ID = "HABIT_ID";
|
|
public static final String COLUMN_HABIT_NAME = "HABIT_NAME";
|
|
public static final String COLUMN_HABIT_TIME = "TIME";
|
|
|
|
public DatabaseHelper(@Nullable Context context) {
|
|
super(context, "HabitTracker.db", null, 2);
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(SQLiteDatabase sqLiteDatabase) {
|
|
String createUserTable = "CREATE TABLE " + USER_TABLE + " " +
|
|
"(" + COLUMN_USER_ID + " TEXT PRIMARY KEY, " + COLUMN_USERNAME + " TEXT, " + COLUMN_PASSWORD + " TEXT)";
|
|
|
|
String createHabitTable = "CREATE TABLE " + HABIT_TABLE + " " +
|
|
"(" + COLUMN_HABIT_ID + " TEXT PRIMARY KEY, " +
|
|
COLUMN_USER_ID + " TEXT, " +
|
|
COLUMN_HABIT_NAME + " TEXT, " +
|
|
COLUMN_HABIT_TIME + " TEXT, " +
|
|
"FOREIGN KEY(" + COLUMN_USER_ID + ") REFERENCES " + USER_TABLE + "(" + COLUMN_USER_ID + "))";
|
|
|
|
|
|
sqLiteDatabase.execSQL(createUserTable);
|
|
sqLiteDatabase.execSQL(createHabitTable);
|
|
}
|
|
|
|
@Override
|
|
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
|
|
if(oldVersion < 2) {
|
|
String addHabitColumn = "ALTER TABLE " + HABIT_TABLE + " ADD COLUMN " + COLUMN_HABIT_TIME + " TEXT";
|
|
sqLiteDatabase.execSQL(addHabitColumn);
|
|
}
|
|
}
|
|
|
|
public boolean createAccount(User user) {
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
ContentValues cv = new ContentValues();
|
|
|
|
cv.put(COLUMN_USER_ID, user.getUserId());
|
|
cv.put(COLUMN_USERNAME, user.getUsername());
|
|
cv.put(COLUMN_PASSWORD, user.getPassword());
|
|
|
|
long insert = db.insert(USER_TABLE, null, cv);
|
|
return insert != -1;
|
|
}
|
|
|
|
public User login(String username, String password) {
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
User user = new User();
|
|
String query = "SELECT * FROM " + USER_TABLE + " WHERE " + COLUMN_USERNAME + " = ? AND " + COLUMN_PASSWORD + " = ?";
|
|
String[] selectionArgs = {username, password};
|
|
try (Cursor cursor = db.rawQuery(query, selectionArgs)) {
|
|
|
|
println(Log.DEBUG, "username provided: ", username);
|
|
|
|
if (cursor.moveToFirst()) {
|
|
String userId = cursor.getString(0);
|
|
user.setUserId(userId);
|
|
user.setUsername(username);
|
|
user.setPassword(password);
|
|
|
|
return user;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean createHabit(Habit habit) {
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
ContentValues cv = new ContentValues();
|
|
|
|
cv.put(COLUMN_HABIT_ID, habit.getHabitId());
|
|
cv.put(COLUMN_USER_ID, habit.getUserId());
|
|
cv.put(COLUMN_HABIT_NAME, habit.getHabitName());
|
|
cv.put(COLUMN_HABIT_TIME, habit.getTime());
|
|
|
|
long insert = db.insert(HABIT_TABLE, null, cv);
|
|
return insert != -1;
|
|
}
|
|
|
|
public ArrayList<Habit> getAllHabits(String userId) {
|
|
ArrayList<Habit> habitList = new ArrayList<>();
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
// Define the query
|
|
String query = "SELECT * FROM " + HABIT_TABLE + " WHERE " + COLUMN_USER_ID + " = ?";
|
|
String[] selectionArgs = {userId};
|
|
Cursor cursor = db.rawQuery(query, selectionArgs);
|
|
|
|
// Loop through the cursor and populate the list
|
|
if (cursor.moveToFirst()) {
|
|
do {
|
|
String habitName = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_HABIT_NAME));
|
|
String time = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_HABIT_TIME));
|
|
|
|
// Create a new Habit object and add it to the list
|
|
Habit habit = new Habit(habitName, time);
|
|
habitList.add(habit);
|
|
} while (cursor.moveToNext());
|
|
}
|
|
|
|
// Close the cursor and database
|
|
cursor.close();
|
|
db.close();
|
|
|
|
return habitList;
|
|
}
|
|
|
|
}
|