updated Database.ts to use ENUM for Category Type

This commit is contained in:
Tocuro 2024-01-08 10:42:31 +01:00 committed by Jakob Stornig
parent 114ad92818
commit 6ca505d505

View file

@ -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}[];