feat(profiles): add profile repository
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
37
src/lib/server/profiles/repository.ts
Normal file
37
src/lib/server/profiles/repository.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type Database from 'better-sqlite3';
|
||||
import type { Profile } from '$lib/types';
|
||||
|
||||
export function listProfiles(db: Database.Database): Profile[] {
|
||||
return db
|
||||
.prepare('SELECT id, name, avatar_emoji FROM profile ORDER BY name')
|
||||
.all() as Profile[];
|
||||
}
|
||||
|
||||
export function createProfile(
|
||||
db: Database.Database,
|
||||
name: string,
|
||||
avatarEmoji: string | null = null
|
||||
): Profile {
|
||||
const trimmed = name.trim();
|
||||
if (!trimmed) throw new Error('Profile name cannot be empty');
|
||||
return db
|
||||
.prepare(
|
||||
`INSERT INTO profile(name, avatar_emoji) VALUES (?, ?)
|
||||
RETURNING id, name, avatar_emoji`
|
||||
)
|
||||
.get(trimmed, avatarEmoji) as Profile;
|
||||
}
|
||||
|
||||
export function renameProfile(
|
||||
db: Database.Database,
|
||||
id: number,
|
||||
newName: string
|
||||
): void {
|
||||
const trimmed = newName.trim();
|
||||
if (!trimmed) throw new Error('Profile name cannot be empty');
|
||||
db.prepare('UPDATE profile SET name = ? WHERE id = ?').run(trimmed, id);
|
||||
}
|
||||
|
||||
export function deleteProfile(db: Database.Database, id: number): void {
|
||||
db.prepare('DELETE FROM profile WHERE id = ?').run(id);
|
||||
}
|
||||
Reference in New Issue
Block a user