Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read entries #21

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions db/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
import {
getAllEntriesBySchool,
insertEntry,
editEntry
editEntry,
getEntryById
} from "./entry";

export interface Country {
Expand Down Expand Up @@ -59,18 +60,54 @@ export class DatabaseService {
this.db = await SQLite.openDatabaseAsync("local.db");

await this.db.execAsync(`
CREATE TABLE IF NOT EXISTS countries(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)
`);
CREATE TABLE IF NOT EXISTS countries(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)
`);

await this.db.execAsync(`
CREATE TABLE IF NOT EXISTS entries(
id TEXT PRIMARY KEY,
arrival_date TEXT NOT NULL,
arrival_time TEXT NOT NULL,
time_teachers_arrive TEXT NOT NULL,
time_children_leave TEXT NOT NULL,
time_classes_start TEXT NOT NULL,
time_classes_end TEXT NOT NULL,
recess_time TEXT NOT NULL,
num_hours_children TEXT NOT NULL,
num_teachers_absent TEXT NOT NULL,
cleanliness TEXT NOT NULL,
playground_used INTEGER NOT NULL,
sinks_used INTEGER NOT NULL,
classroom_decor TEXT NOT NULL,
classrooms_used INTEGER NOT NULL,
observations TEXT NOT NULL,
program_type TEXT NOT NULL,
num_children TEXT NOT NULL,
num_parents TEXT NOT NULL,
school TEXT NOT NULL
)
`);

console.log("db initialized successfully");
} catch (err) {
console.error("error initializing db: ", err);
}
}

async getEntryById(id: string): Promise<Entry | null> {
try {
if (!this.db) throw new Error("db not initialized");
return await getEntryById(this.db, id);
} catch (err) {
console.error("error getting entry by id:", err);
return null;
}
}

// Keep all existing methods
async addCountry(countryName: string): Promise<Country | null> {
try {
if (!this.db) throw new Error("Database not initialized");
Expand Down Expand Up @@ -106,15 +143,15 @@ export class DatabaseService {
}

async createEntry(entry: Omit<Entry, "id">): Promise<Entry | null> {
try {
if (!this.db) throw new Error("Database not initialized");
return await insertEntry(this.db, entry);
} catch (error) {
console.error("Error creating entry:", error);
return null;
}
try {
if (!this.db) throw new Error("Database not initialized");
return await insertEntry(this.db, entry);
} catch (error) {
console.error("Error creating entry:", error);
return null;
}

}

async addCommunity(
communityName: string,
countryName: string,
Expand Down Expand Up @@ -198,7 +235,7 @@ export class DatabaseService {
}
}

async editEntry(schoolId: string, entryId:string, newEntry: Entry): Promise<Entry | null> {
async editEntry(schoolId: string, entryId: string, newEntry: Entry): Promise<Entry | null> {
try {
if (!this.db) throw new Error("Database not initialized");
return await editEntry(this.db, schoolId, entryId, newEntry);
Expand All @@ -213,4 +250,4 @@ export const createDatabase = async () => {
const dbService = new DatabaseService();
await dbService.initDatabase();
return dbService;
};
};
72 changes: 47 additions & 25 deletions db/entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,38 @@ export interface Entry {
school: string;
}

export interface School {
id: string;
status: string;
}

export async function getEntryById(
db: SQLite.SQLiteDatabase,
id: string
): Promise<Entry | null> {
try {
const result = await db.getFirstAsync<any>(
`SELECT * FROM entries WHERE id = ?`,
[id]
);

if (!result) return null;

return {
...result,
playground_used: Boolean(result.playground_used),
sinks_used: Boolean(result.sinks_used),
classrooms_used: Boolean(result.classrooms_used),
};
} catch (error) {
console.error("Error getting entry by id:", error);
return null;
}
}

export async function insertEntry(
db: SQLite.SQLiteDatabase,
entry: Omit<Entry, "id">, // sqlite generated id
entry: Omit<Entry, "id">
): Promise<Entry | null> {
try {
const result = await db.runAsync(
Expand All @@ -36,7 +65,7 @@ export async function insertEntry(
cleanliness, playground_used, sinks_used,
classroom_decor, classrooms_used, observations,
program_type, num_children, num_parents, school
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
entry.arrival_date,
entry.arrival_time,
Expand All @@ -48,51 +77,42 @@ export async function insertEntry(
entry.num_hours_children,
entry.num_teachers_absent,
entry.cleanliness,
entry.playground_used ? 1 : 0,
entry.sinks_used ? 1 : 0,
entry.playground_used ? 1 : 0,
entry.sinks_used ? 1 : 0,
entry.classroom_decor,
entry.classrooms_used ? 1 : 0,
entry.classrooms_used ? 1 : 0,
entry.observations,
entry.program_type,
entry.num_children,
entry.num_parents,
entry.school,
entry.school,
]
);

if (result.changes > 0) {
const id = result.lastInsertRowId.toString(); // id from sqlite

const id = result.lastInsertRowId.toString();
return { ...entry, id };
}
} catch (error) {
console.error("Error inserting entry:", error);
}

return null;
}

export interface School {
id: string;
status: string;
return null;
}

export async function getAllEntriesBySchool(
db: SQLite.SQLiteDatabase,
id: string
): Promise<Entry[] | null> {
try {
// First, get the school information
const school = await db.getFirstAsync<Omit<School, "entries">>(
`SELECT * FROM schools WHERE _id = ?`,
[id]
);

if (!school) {
return null; // School not found
return null;
}

// Then, get all entries associated with this school
const entries = await db.getAllAsync<Entry>(
`SELECT * FROM entries WHERE school = ?`,
[id]
Expand Down Expand Up @@ -131,7 +151,7 @@ export async function editEntry(
observations = ?,
program_type = ?,
num_children = ?,
num_parents = ?,
num_parents = ?
WHERE id = ? AND school_id = ?`,
[
newEntry.arrival_date,
Expand All @@ -144,10 +164,10 @@ export async function editEntry(
newEntry.num_hours_children,
newEntry.num_teachers_absent,
newEntry.cleanliness,
newEntry.playground_used,
newEntry.sinks_used,
newEntry.playground_used ? 1 : 0,
newEntry.sinks_used ? 1 : 0,
newEntry.classroom_decor,
newEntry.classrooms_used,
newEntry.classrooms_used ? 1 : 0,
newEntry.observations,
newEntry.program_type,
newEntry.num_children,
Expand All @@ -156,11 +176,13 @@ export async function editEntry(
schoolId
]
);

if (result.changes > 0) {
return newEntry;
return newEntry;
}

return null;
} catch (error) {
console.error("Error editing entry:", error);
return null;
}
}
Loading