Skip to content

Commit

Permalink
feat: add getAllUsers function to retrieve all users from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed Jan 4, 2025
1 parent 83496ca commit 2a349bd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type DatabaseService = {
get isDatabaseConnected(): boolean;
registerUser(username: string, password: string, customerId: string): void;
findUser(username: string, password: string): UserRecordMini;
getAllUsers(): UserRecordMini[];
updateSession(customerId: string, contextId: string, userId: number): void;
}

Expand Down Expand Up @@ -67,7 +68,8 @@ function findUser(database: DatabaseSync, username: string, password: string): U
const query = database.prepare(
`SELECT * FROM user WHERE username = ? AND password = ?`,
);
const user = query.get(username, password) as UserRecordMini | null;
const hashedPassword = generatePasswordHash(password);
const user = query.get(username, hashedPassword) as UserRecordMini | null;
if (user == null) {
throw new Error("User not found");
}
Expand All @@ -78,6 +80,14 @@ function findUser(database: DatabaseSync, username: string, password: string): U
}
}

Check warning on line 81 in src/database/index.ts

View check run for this annotation

Codecov / codecov/patch

src/database/index.ts#L67-L81

Added lines #L67 - L81 were not covered by tests

function getAllUsers(database: DatabaseSync): UserRecordMini[] {
const query = database.prepare(
`SELECT * FROM user`,
);
const users = query.all() as UserRecordMini[];
return users;
}

Check warning on line 89 in src/database/index.ts

View check run for this annotation

Codecov / codecov/patch

src/database/index.ts#L83-L89

Added lines #L83 - L89 were not covered by tests

function updateSession(database: DatabaseSync, customerId: string, contextId: string, userId: number) {
const insert = database.prepare(
`INSERT OR REPLACE INTO session (contextId, customerId, userId) VALUES (?, ?, ?)`,
Expand Down Expand Up @@ -124,6 +134,10 @@ function initializeDatabaseService(): DatabaseService {
ensureDatabaseIsReady(databaseInstance);
return findUser(databaseInstance, username, password);
},
getAllUsers: () => {
ensureDatabaseIsReady(databaseInstance);
return getAllUsers(databaseInstance);
},
updateSession: (customerId, contextId, userId) => {
ensureDatabaseIsReady(databaseInstance);
return updateSession(databaseInstance, customerId, contextId, userId);
Expand Down

0 comments on commit 2a349bd

Please sign in to comment.