-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock.server.js
44 lines (42 loc) · 1.72 KB
/
mock.server.js
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
import { Server } from 'miragejs';
new Server({
seeds(server) {
server.db.loadData({
todos: []
});
},
routes() {
this.get('http://localhost:3000/', (schema) => {
return schema.db.todos;
});
this.post('http://localhost:3000/todos', (schema, request) => {
const todo = JSON.parse(request.requestBody);
todo._id = Date.now().toString();
todo.completed = false;
todo.owner = '131d51ee-5888-4474-b96c-3cf44119fb36';
return schema.db.todos.insert(todo);
});
this.post('http://localhost:3000/todos/:id', (schema, request) => {
const editType = request.queryParams.type;
if (editType === 'COMPLETE_TODO') {
return schema.db.todos.update({ _id: request.params.id }, { completed: true });
} else if (editType === 'DELETE_TODO') {
return schema.db.todos.remove({ _id: request.params.id });
} else if (editType === 'COMPLETE_ALL') {
const todos = JSON.parse(request.requestBody);
schema.db.todos
.filter((x) => todos.includes(x._id))
.forEach((x) => x.complete === true);
return 'success';
} else if (editType === 'UNCOMPLETE_ALL') {
const todos = JSON.parse(request.requestBody);
schema.db.todos
.filter((x) => todos.includes(x._id))
.forEach((x) => x.complete === false);
return 'success';
} else if (editType === 'CLEAR_ALL') {
return schema.db.todos.remove({ completed: true });
}
});
}
});