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 entry #20

Closed
wants to merge 1 commit into from
Closed
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
67 changes: 45 additions & 22 deletions db/base.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import * as SQLite from "expo-sqlite"
import { insertCountry, getCountryById } from "./country";
import * as SQLite from "expo-sqlite";
import { getEntryById } from "./entry";

export interface Country{
id: number,
name: String
export interface Entry {
id: string;
arrival_date: string;
arrival_time: string;
time_teachers_arrive: string;
time_children_leave: string;
time_classes_start: string;
time_classes_end: string;
recess_time: number;
num_hours_children: number;
num_teachers_absent: number;
cleanliness: number;
playground_used: boolean;
sinks_used: boolean;
classroom_decor: string;
classrooms_used: boolean;
observations: string;
program_type: string;
num_children: number;
num_parents: number;
}

export class DatabaseService {
Expand All @@ -13,11 +30,27 @@ export class DatabaseService {
try {
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 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 INTEGER NOT NULL,
num_hours_children INTEGER NOT NULL,
num_teachers_absent INTEGER NOT NULL,
cleanliness INTEGER 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 INTEGER NOT NULL,
num_parents INTEGER NOT NULL
)
`);

Expand All @@ -26,23 +59,13 @@ export class DatabaseService {
console.error("error initializing db: ", err)
}
}

async addCountry(countryName: string): Promise<Country|null> {
try {
if (!this.db) throw new Error("Database not initialized");
return await insertCountry(this.db, countryName, this.getCountryById.bind(this));
} catch (err) {
console.error("error adding country: ", err);
return null
}
}

async getCountryById(id: number): Promise<Country|null> {
async getEntryById(id: string): Promise<Entry|null> {
try {
if (!this.db) throw new Error("db not initialized");
return await getCountryById(this.db, id)
return await getEntryById(this.db, id)
} catch (err) {
console.error("error getting country by id", err);
console.error("error getting entry by id", err);
return null
}
}
Expand Down
43 changes: 43 additions & 0 deletions db/entry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as SQLite from "expo-sqlite";

export interface Entry {
id: string;
arrival_date: string;
arrival_time: string;
time_teachers_arrive: string;
time_children_leave: string;
time_classes_start: string;
time_classes_end: string;
recess_time: number;
num_hours_children: number;
num_teachers_absent: number;
cleanliness: number;
playground_used: boolean;
sinks_used: boolean;
classroom_decor: string;
classrooms_used: boolean;
observations: string;
program_type: string;
num_children: number;
num_parents: number;
}

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;
}
}
Loading