Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
130 lines (115 loc) · 2.92 KB
/
Copy pathserver.ts
File metadata and controls
130 lines (115 loc) · 2.92 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { createClient } from "./utils/supabase/client";
type Task = {
id?: number;
created_at?: string;
title: string;
description: string;
status: string;
updated_at?: string;
userId?: string;
};
export async function signUpNewUser(userEmail: string, pw: string) {
const supabase = await createClient();
if (!supabase) {
throw new Error("Failed to initialize Supabase client");
}
try {
const { data, error } = await supabase.auth.signUp({
email: userEmail,
password: pw,
});
console.log("signup new user server.ts data", data);
console.log("signup new user server.ts error", error);
} catch (error) {
console.log(error);
}
}
export const getTasks = async (): Promise<{
data: Task[] | null;
error: Error | null;
}> => {
const supabase = await createClient();
if (!supabase) {
return {
data: null,
error: new Error("Failed to initialize Supabase client"),
};
}
const { data, error } = await supabase.from("tasks").select("*");
if (error) {
console.error("Error fetching tasks:", error);
return { data: null, error };
}
return { data, error: null };
};
export const createTask = async (task: Task) => {
const supabase = await createClient();
if (!supabase) {
return { error: new Error("Failed to initialize Supabase client") };
}
const { error } = await supabase.from("tasks").insert({
title: task.title,
description: task.description,
status: task.status,
});
if (error) {
console.error("Error creating task:", error);
return { error };
}
console.log("Task created successfully");
};
export const editTask = async (
task: Task
): Promise<{
data: Task | null;
error: Error | null;
}> => {
const supabase = await createClient();
if (!supabase) {
return {
data: null,
error: new Error("Failed to initialize Supabase client"),
};
}
try {
const { data, error } = await supabase
.from("tasks")
.update({
title: task.title,
description: task.description,
status: task.status,
})
.eq("id", task.id)
.select()
.single();
if (error) {
return { data: null, error: new Error(error.message) };
}
return { data, error: null };
} catch (err) {
const error =
err instanceof Error ? err : new Error("Unknown error occurred");
return { data: null, error };
}
};
export const deleteTask = async (
id: number | undefined
): Promise<{
data: Task | null;
error: Error | null;
}> => {
const supabase = await createClient();
if (!supabase) {
return {
data: null,
error: new Error("Failed to initialize Supabase client"),
};
}
const { data, error } = await supabase.from("tasks").delete().eq("id", id);
if (error) {
console.error("Error deleting task:", error);
return { data: null, error };
}
console.log("Task deleted successfully");
return { data, error: null };
};