feat(profiles): add profile repository

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 15:11:23 +02:00
parent 99afc45c29
commit 5693371673
2 changed files with 71 additions and 0 deletions

View 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);
}

View File

@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { openInMemoryForTest } from '../../src/lib/server/db';
import {
createProfile,
listProfiles,
renameProfile,
deleteProfile
} from '../../src/lib/server/profiles/repository';
describe('profile repository', () => {
it('creates, lists, renames, deletes', () => {
const db = openInMemoryForTest();
const p = createProfile(db, 'Hendrik', '👨‍🍳');
expect(p.id).toBeGreaterThan(0);
expect(listProfiles(db).length).toBe(1);
renameProfile(db, p.id, 'Hendrik S.');
expect(listProfiles(db)[0].name).toBe('Hendrik S.');
deleteProfile(db, p.id);
expect(listProfiles(db).length).toBe(0);
});
it('rejects duplicate names', () => {
const db = openInMemoryForTest();
createProfile(db, 'Hendrik');
expect(() => createProfile(db, 'Hendrik')).toThrow();
});
it('rejects empty name', () => {
const db = openInMemoryForTest();
expect(() => createProfile(db, ' ')).toThrow(/empty/i);
});
});