62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
class TodoService {
|
|
static async fetchTodos() {
|
|
const response = await fetch('/api/todos');
|
|
if (!response.ok) {
|
|
throw new Error('获取待办事项失败');
|
|
}
|
|
return await response.json();
|
|
}
|
|
|
|
static async fetchUsers() {
|
|
const response = await fetch('/api/users');
|
|
if (!response.ok) {
|
|
throw new Error('获取用户数据失败');
|
|
}
|
|
const data = await response.json();
|
|
return data.users || [];
|
|
}
|
|
|
|
static async updateTodo(todoId, todoData) {
|
|
const response = await fetch(`/api/todos/${todoId}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(todoData)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('更新待办事项失败');
|
|
}
|
|
return await response.json();
|
|
}
|
|
|
|
static async createTodo(todoData) {
|
|
const response = await fetch('/api/todos', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(todoData)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.message || '创建待办失败');
|
|
}
|
|
return await response.json();
|
|
}
|
|
|
|
static async deleteTodo(todoId) {
|
|
const response = await fetch(`/api/todos/${todoId}`, {
|
|
method: 'DELETE'
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('删除待办事项失败');
|
|
}
|
|
}
|
|
}
|
|
|
|
export default TodoService;
|