-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathserver-final.js
More file actions
117 lines (95 loc) · 2.65 KB
/
server-final.js
File metadata and controls
117 lines (95 loc) · 2.65 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
/*
This is the final solution for the Tutorial that fully implements the API for the Reminders app.
*/
import {
Model,
hasMany,
belongsTo,
RestSerializer,
createServer,
Factory,
trait,
} from "miragejs";
export default function ({ environment = "development" } = {}) {
return createServer({
environment,
serializers: {
reminder: RestSerializer.extend({
include: ["list"],
embed: true,
}),
},
models: {
list: Model.extend({
reminders: hasMany(),
}),
reminder: Model.extend({
list: belongsTo(),
}),
},
factories: {
list: Factory.extend({
name(i) {
return `List ${i}`;
},
withReminders: trait({
afterCreate(list, server) {
if (!list.reminders.length) {
server.createList("reminder", 5, { list });
}
},
}),
}),
reminder: Factory.extend({
text(i) {
return `Reminder ${i}`;
},
}),
},
seeds(server) {
server.create("reminder", { text: "Walk the dog" });
server.create("reminder", { text: "Take out the trash" });
server.create("reminder", { text: "Work out" });
// server.createList("reminder", 5);
// server.createList("reminder", 30);
server.create("list", {
name: "Home",
reminders: [server.create("reminder", { text: "Do taxes" })],
});
server.create("list", {
name: "Work",
reminders: [server.create("reminder", { text: "Visit bank" })],
});
},
routes() {
this.get("/api/lists", (schema, request) => {
return schema.lists.all();
});
this.get("/api/lists/:id/reminders", (schema, request) => {
let list = schema.lists.find(request.params.id);
return list.reminders;
});
this.get("/api/reminders", (schema) => {
return schema.reminders.all();
});
this.post("/api/reminders", (schema, request) => {
let attrs = JSON.parse(request.requestBody);
return schema.reminders.create(attrs);
});
this.post("/api/lists", (schema, request) => {
let attrs = JSON.parse(request.requestBody);
return schema.lists.create(attrs);
});
this.delete("/api/reminders/:id", (schema, request) => {
let id = request.params.id;
return schema.reminders.find(id).destroy();
});
this.delete("/api/lists/:id", (schema, request) => {
let id = request.params.id;
let list = schema.lists.find(id);
list.reminders.destroy();
return list.destroy();
});
},
});
}