111 lines
3.3 KiB
TypeScript
111 lines
3.3 KiB
TypeScript
|
|
import db from '@/lib/db';
|
|
import type { Toy, User } from '@/types';
|
|
import { randomUUID } from 'crypto';
|
|
|
|
// Helper to parse toy data from DB, converting JSON strings back to objects
|
|
const parseToy = (toyData: any): Toy => {
|
|
if (!toyData) return null;
|
|
return {
|
|
...toyData,
|
|
images: toyData.images ? JSON.parse(toyData.images) : [],
|
|
unavailableRanges: toyData.unavailableRanges ? JSON.parse(toyData.unavailableRanges) : [],
|
|
pricePerDay: Number(toyData.pricePerDay),
|
|
ownerAvatarUrl: toyData.ownerAvatarUrl,
|
|
dataAiHint: toyData.category?.toLowerCase() || 'toy'
|
|
};
|
|
};
|
|
|
|
// --- TOY OPERATIONS ---
|
|
|
|
export function getAllToys(): Toy[] {
|
|
const stmt = db.prepare(`
|
|
SELECT t.*, u.name as ownerName, u.avatarUrl as ownerAvatarUrl
|
|
FROM toys t
|
|
JOIN users u ON t.ownerId = u.id
|
|
`);
|
|
const toys = stmt.all();
|
|
return toys.map(parseToy);
|
|
}
|
|
|
|
export function getToyById(id: string): Toy | undefined {
|
|
const stmt = db.prepare(`
|
|
SELECT t.*, u.name as ownerName, u.avatarUrl as ownerAvatarUrl
|
|
FROM toys t
|
|
JOIN users u ON t.ownerId = u.id
|
|
WHERE t.id = ?
|
|
`);
|
|
const toy = stmt.get(id);
|
|
return toy ? parseToy(toy) : undefined;
|
|
}
|
|
|
|
export function getToysByOwner(ownerId: number): Toy[] {
|
|
const stmt = db.prepare(`
|
|
SELECT t.*, u.name as ownerName, u.avatarUrl as ownerAvatarUrl
|
|
FROM toys t
|
|
JOIN users u ON t.ownerId = u.id
|
|
WHERE t.ownerId = ?
|
|
`);
|
|
const toys = stmt.all(ownerId);
|
|
return toys.map(parseToy);
|
|
}
|
|
|
|
export function createToy(toyData: Omit<Toy, 'id' | 'ownerName' | 'ownerAvatarUrl' | 'dataAiHint'>): Toy {
|
|
const id = `toy-${randomUUID()}`;
|
|
const stmt = db.prepare(
|
|
`INSERT INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location)
|
|
VALUES (@id, @name, @description, @category, @images, @unavailableRanges, @ownerId, @pricePerDay, @location)`
|
|
);
|
|
|
|
stmt.run({
|
|
...toyData,
|
|
id,
|
|
images: JSON.stringify(toyData.images),
|
|
unavailableRanges: JSON.stringify(toyData.unavailableRanges),
|
|
});
|
|
|
|
return getToyById(id)!;
|
|
}
|
|
|
|
export function updateToy(toyData: Omit<Toy, 'ownerName' | 'ownerAvatarUrl' | 'dataAiHint'>): Toy {
|
|
const stmt = db.prepare(
|
|
`UPDATE toys SET
|
|
name = @name,
|
|
description = @description,
|
|
category = @category,
|
|
images = @images,
|
|
unavailableRanges = @unavailableRanges,
|
|
pricePerDay = @pricePerDay,
|
|
location = @location
|
|
WHERE id = @id AND ownerId = @ownerId` // Security check
|
|
);
|
|
|
|
const result = stmt.run({
|
|
...toyData,
|
|
images: JSON.stringify(toyData.images),
|
|
unavailableRanges: JSON.stringify(toyData.unavailableRanges),
|
|
});
|
|
|
|
if (result.changes === 0) {
|
|
throw new Error("Toy not found or user not authorized to update.");
|
|
}
|
|
|
|
return getToyById(toyData.id)!;
|
|
}
|
|
|
|
export function getOwnerProfile(ownerId: number): User | undefined {
|
|
return getUserById(ownerId);
|
|
}
|
|
|
|
// --- USER OPERATIONS ---
|
|
|
|
export function getAllUsers(): User[] {
|
|
const stmt = db.prepare('SELECT id, name, nickname, email, role, avatarUrl, bio FROM users');
|
|
return stmt.all() as User[];
|
|
}
|
|
|
|
export function getUserById(id: number): User | undefined {
|
|
const stmt = db.prepare('SELECT * FROM users WHERE id = ?');
|
|
return stmt.get(id) as User | undefined;
|
|
}
|