Resolve "Budget"

This commit is contained in:
Thomas Schleicher 2024-01-02 12:49:13 +00:00 committed by jastornig
parent 645b805aa7
commit 69610de100
17 changed files with 346 additions and 121 deletions

View file

@ -1,4 +1,3 @@
//created by thschleicher
import * as SQLite from "expo-sqlite";
import uuid from "react-native-uuid";
@ -6,27 +5,26 @@ import uuid from "react-native-uuid";
import { Query } from "expo-sqlite";
import { SimpleDate } from "../util/SimpleDate";
const db = SQLite.openDatabase("interactive_systeme.db");
let db: SQLite.SQLiteDatabase;
export const initDatabase = async () => {
db = SQLite.openDatabase("interactive_systeme.db");
try {
await db.transactionAsync(async (tx: SQLite.SQLTransactionAsync) => {
await tx.executeSqlAsync(
"CREATE TABLE IF NOT EXISTS category (guid VARCHAR(36) PRIMARY KEY, name TEXT, color TEXT, type TEXT);"
"CREATE TABLE IF NOT EXISTS category (guid VARCHAR(36) PRIMARY KEY, name TEXT, color TEXT, type TEXT, allocated_amount DOUBLE);"
);
await tx.executeSqlAsync(
"CREATE TABLE IF NOT EXISTS expense (guid VARCHAR(36) PRIMARY KEY, name TEXT, category_guid VARCHAR(36), datetime DATETIME, amount DOUBLE, FOREIGN KEY (category_guid) REFERENCES category(guid));"
);
console.log("Successfully initialized Tables!");
});
} catch (error) {
console.log("Error initializing the Tables!");
console.log("Error initializing the Database!");
throw (error);
}
};
export const addCategory = async (name: string, color: string, type: string) => {
export const addCategory = async (name: string, color: string, type: string, allocated_amount: number) => {
//needs user input validation for type and color (should be validated by this function)
@ -34,18 +32,17 @@ export const addCategory = async (name: string, color: string, type: string) =>
try {
await db.transactionAsync(async (tx) => {
await tx.executeSqlAsync("INSERT INTO category (guid, name, color, type) VALUES (?, ?, ?, ?);",
[UUID.toString(), name, color, type]
await tx.executeSqlAsync("INSERT INTO category (guid, name, color, type, allocated_amount) VALUES (?, ?, ?, ?, ?);",
[UUID.toString(), name, color, type, allocated_amount]
);
});
console.log("Category added successfully!");
} catch (error) {
console.log("Error adding category: ", error);
throw error;
}
}
export const addExpense = async (name: string, category_guid: string,datetime: string, amount: number) => {
export const addExpense = async (name: string, category_guid: string, datetime: string, amount: number) => {
//needs user input validation for type and color (should be validated by this function)
@ -56,7 +53,6 @@ export const addExpense = async (name: string, category_guid: string,datetime: s
await tx.executeSqlAsync("INSERT INTO expense (guid, name, category_guid, datetime, amount) VALUES (?, ?, ?, ?, ?);", [expenseUUID.toString(), name, category_guid, datetime, amount]
);
});
console.log("Expense added successfully!");
} catch (error) {
console.log("Error adding expense: ", error);
throw error;
@ -85,15 +81,15 @@ export const deleteExpense = async (guid: string) => {
}
}
export const executeQuery = async (query: string) => {
const sqliteQuary: Query[] = [{sql: query, args: []}];
const result = await db.execAsync(sqliteQuary, true);
export const executeQuery = async (query: Query) => {
const result = await db.execAsync([query], true);
if("error" in result[0]){
throw result[0].error
}
return result;
return result;
}
export const deleteExpenses = async () => {
try {
await db.transactionAsync(async (tx: SQLite.SQLTransactionAsync) => {
@ -125,9 +121,7 @@ export const deleteDatabase = () => {
console.log("Error deleting the Database: ", error);
throw error;
}
});
console.log("Database Deleted!")
}
const closeDatabase = async () => {
@ -149,9 +143,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")
await addCategory(`Category ${i}`, colors[random], "budget", 50)
}
const result = await executeQuery("SELECT * from category")
const result = await executeQuery({sql:"SELECT * from category", args:[]})
let categories: {[column: string]: any}[];
if("rows" in result[0]){
categories = result[0]["rows"]