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