Tương tự, chúng ta cũng có thể thêm các handler cho các phương thức HTTP khác. Thêm một đường dẫn /todo/[id] bằng cách tạo một tệp src/routes/todo/[id]/+server.js với các handler PUT và DELETE để chuyển đổi và xóa các công việc, sử dụng các hàm toggleTodo và deleteTodo trong src/lib/server/database.js:
src/routes/todo/[id]/+server.js
import * as database from '$lib/server/database.js';
export async function PUT({ params, request, cookies }) {
const { done } = await request.json();
const userid = cookies.get('userid');
await database.toggleTodo({ userid, id: params.id, done });
return new Response(null, { status: 204 });
}
export async function DELETE({ params, cookies }) {
const userid = cookies.get('userid');
await database.deleteTodo({ userid, id: params.id });
return new Response(null, { status: 204 });
}Vì không cần trả về dữ liệu gì cho trình duyệt, chúng ta sẽ trả về một Response trống với trạng thái 204 No Content.
Bây giờ, chúng ta có thể tương tác với endpoint này trong các event handlers:
src/routes/+page.svelte
<label>
<input
type="checkbox"
checked={todo.done}
on:change={async (e) => {
const done = e.currentTarget.checked;
await fetch(`/todo/${todo.id}`, {
method: 'PUT',
body: JSON.stringify({ done }),
headers: {
'Content-Type': 'application/json'
}
});
}}
/>
<span>{todo.description}</span>
<button
aria-label="Mark as complete"
on:click={async (e) => {
await fetch(`/todo/${todo.id}`, {
method: 'DELETE'
});
data.todos = data.todos.filter((t) => t !== todo);
}}
></button>
</label>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
<script>
export let data;
</script>
<div class="centered">
<h1>todos</h1>
<label>
thêm todo:
<input
type="text"
autocomplete="off"
on:keydown={async (e) => {if (e.key !== 'Enter') return;
const input = e.currentTarget;
const description = input.value;
const response = await fetch('/todo', {method: 'POST',
body: JSON.stringify({ description }), headers: {'Content-Type': 'application/json'
}
});
const { id } = await response.json(); data.todos = [...data.todos, {id,
description
}];
input.value = '';
}}
/>
</label>
<ul class="todos">
{#each data.todos as todo (todo.id)}<li>
<label>
<input
type="checkbox"
checked={todo.done} on:change={async (e) => {const done = e.currentTarget.checked;
// xử lý TODO change
}}
/>
<span>{todo.description}</span><button
aria-label="Mark as complete"
on:click={async (e) => {// xử lý TODO delete
}}
></button>
</label>
</li>
{/each}</ul>
</div>
<style>
.centered {max-width: 20em;
margin: 0 auto;
}
label {display: flex;
width: 100%;
}
input[type="text"] {flex: 1;
}
span {flex: 1;
}
button {border: none;
background: url(./remove.svg) no-repeat 50% 50%;
background-size: 1rem 1rem;
cursor: pointer;
height: 100%;
aspect-ratio: 1;
opacity: 0.5;
transition: opacity 0.2s;
}
button:hover {opacity: 1;
}
</style>