diff --git a/services/database.ts b/services/database.ts index ec5f656..370108d 100644 --- a/services/database.ts +++ b/services/database.ts @@ -7,6 +7,11 @@ import { SimpleDate } from "../util/SimpleDate"; let db: SQLite.SQLiteDatabase; +export enum CategoryType { + SAVING = "saving", + EXPENSE = "expense", +} + export const initDatabase = async () => { db = SQLite.openDatabase("interactive_systeme.db"); try { @@ -24,11 +29,11 @@ export const initDatabase = async () => { } }; -export const updateCategory = async (guid: string, name: string, color: string, type: string, allocated_amount: number) => { +export const updateCategory = async (guid: string, name: string, color: string, CategoryType: CategoryType, allocated_amount: number) => { try { await db.transactionAsync(async (tx) => { - await tx.executeSqlAsync("UPDATE category SET name = ?, color = ?, type = ?, allocated_amount = ? WHERE guid = ?", [name, color, type, allocated_amount, guid]); + await tx.executeSqlAsync("UPDATE category SET name = ?, color = ?, type = ?, allocated_amount = ? WHERE guid = ?", [name, color, CategoryType, allocated_amount, guid]); }); } catch (error) { console.log("Error updating category: ", error); @@ -36,7 +41,7 @@ export const updateCategory = async (guid: string, name: string, color: string, } }; -export const addCategory = async (name: string, color: string, type: string, allocated_amount: number) => { +export const addCategory = async (name: string, color: string, CategoryType: CategoryType, allocated_amount: number) => { //needs user input validation for type and color (should be validated by this function) @@ -45,7 +50,7 @@ export const addCategory = async (name: string, color: string, type: string, all try { await db.transactionAsync(async (tx) => { await tx.executeSqlAsync("INSERT INTO category (guid, name, color, type, allocated_amount) VALUES (?, ?, ?, ?, ?);", - [UUID.toString(), name, color, type, allocated_amount] + [UUID.toString(), name, color, CategoryType, allocated_amount] ); }); } catch (error) { @@ -171,8 +176,9 @@ export const DEV_populateDatabase = async () => { for(let i=0; i < 5; i++){ let random = Math.floor(Math.random() * colors.length); - await addCategory(`Category ${i}`, colors[random], "budget", 50) - await addCategory(`Category ${i+6}`, colors[random], "budget", 50) + await addCategory(`Category ${i}`, colors[random], CategoryType.EXPENSE, 50) + random = Math.floor(Math.random() * colors.length); + await addCategory(`Category ${i+6}`, colors[random], CategoryType.SAVING, 50) } const result = await executeQuery({sql:"SELECT * from category", args:[]}) let categories: {[column: string]: any}[];