|
1 | 1 | 'use server'; |
2 | 2 |
|
| 3 | +import { revalidatePath } from 'next/cache'; |
3 | 4 | import('harperdb'); |
4 | 5 |
|
5 | 6 | export async function listDogs() { |
6 | | - const dogs = []; |
7 | | - for await (const dog of tables.Dog.search()) { |
8 | | - dogs.push({ id: dog.id, name: dog.name }); |
| 7 | + // Original database logic - uncomment when ready |
| 8 | + try { |
| 9 | + const dogs = []; |
| 10 | + if (tables?.Dog) { |
| 11 | + for await (const dog of tables.Dog.search()) { |
| 12 | + dogs.push({ id: dog.id, name: dog.name, breed: dog.breed, age: dog.age, color: dog.color }); |
| 13 | + } |
| 14 | + } |
| 15 | + return dogs; |
| 16 | + } catch (error) { |
| 17 | + console.error('Error listing dogs:', error); |
| 18 | + return []; |
9 | 19 | } |
10 | | - return dogs; |
11 | 20 | } |
12 | 21 |
|
13 | 22 | export async function getDog(id) { |
14 | | - return tables.Dog.get(id); |
| 23 | + try { |
| 24 | + if (typeof tables === 'undefined' || !tables.Dog) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + return await tables.Dog.get(id); |
| 28 | + } catch (error) { |
| 29 | + console.error('Error getting dog:', error); |
| 30 | + return null; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export async function createDog(formData) { |
| 35 | + // Extract form values |
| 36 | + const name = formData.get('name'); |
| 37 | + const breed = formData.get('breed'); |
| 38 | + const age = parseInt(formData.get('age')); |
| 39 | + const color = formData.get('color'); |
| 40 | + |
| 41 | + // Validate required fields |
| 42 | + if (!name || !breed || !age || !color) { |
| 43 | + throw new Error('All fields are required'); |
| 44 | + } |
| 45 | + |
| 46 | + if (typeof tables === 'undefined' || !tables.Dog) { |
| 47 | + throw new Error('Database not available'); |
| 48 | + } |
| 49 | + |
| 50 | + await tables.Dog.create({ name, breed, age, color }); |
| 51 | + |
| 52 | + // Revalidate the dogs page to show updated data |
| 53 | + revalidatePath('/dogs'); |
| 54 | +} |
| 55 | + |
| 56 | +export async function deleteDog(dogId) { |
| 57 | + console.log('Deleting dog with id:', dogId); |
| 58 | + |
| 59 | + if (typeof tables === 'undefined' || !tables.Dog) { |
| 60 | + throw new Error('Database not available'); |
| 61 | + } |
| 62 | + |
| 63 | + await tables.Dog.delete(dogId); |
| 64 | + |
| 65 | + // Revalidate the dogs page to show updated data |
| 66 | + revalidatePath('/dogs'); |
15 | 67 | } |
0 commit comments