2015-12-02 04:31:15 +08:00
|
|
|
const _widgets = [
|
2018-06-15 23:03:24 +08:00
|
|
|
{ id: 123, name: "Trout Lure" },
|
|
|
|
{ id: 124, name: "Evil Repellant" }
|
2015-12-02 04:31:15 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
const _moreWidgets = [
|
2018-06-15 23:03:24 +08:00
|
|
|
{ id: 223, name: "Bass Lure" },
|
|
|
|
{ id: 224, name: "Good Repellant" }
|
2015-12-02 04:31:15 +08:00
|
|
|
];
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const fruits = [
|
|
|
|
{ id: 1, name: "apple", farmer_id: 1, color_ids: [1, 2], category_id: 4 },
|
|
|
|
{ id: 2, name: "banana", farmer_id: 1, color_ids: [3], category_id: 3 },
|
|
|
|
{ id: 3, name: "grape", farmer_id: 2, color_ids: [2], category_id: 5 }
|
|
|
|
];
|
2015-12-02 04:31:15 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const farmers = [
|
|
|
|
{ id: 1, name: "Old MacDonald" },
|
|
|
|
{ id: 2, name: "Luke Skywalker" }
|
|
|
|
];
|
2015-12-02 04:31:15 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const colors = [
|
|
|
|
{ id: 1, name: "Red" },
|
|
|
|
{ id: 2, name: "Green" },
|
|
|
|
{ id: 3, name: "Yellow" }
|
|
|
|
];
|
2015-12-02 04:31:15 +08:00
|
|
|
|
|
|
|
export default function(helpers) {
|
|
|
|
const { response, success, parsePostData } = helpers;
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("/fruits/:id", function() {
|
2015-12-02 04:31:15 +08:00
|
|
|
const fruit = fruits[0];
|
|
|
|
return response({ __rest_serializer: "1", fruit, farmers, colors });
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("/fruits", function() {
|
2017-09-13 01:04:53 +08:00
|
|
|
return response({
|
|
|
|
__rest_serializer: "1",
|
|
|
|
fruits,
|
|
|
|
farmers,
|
|
|
|
colors,
|
2018-06-15 23:03:24 +08:00
|
|
|
extras: { hello: "world" }
|
2017-09-13 01:04:53 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("/barns/:id", function() {
|
2017-09-13 01:04:53 +08:00
|
|
|
return response({
|
|
|
|
__rest_serializer: "1",
|
|
|
|
meta: {
|
|
|
|
types: {
|
|
|
|
owner: "farmer"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
barn: { id: 1234, owner_id: farmers[0].id },
|
2018-06-15 23:03:24 +08:00
|
|
|
farmers: [farmers[0]]
|
2017-09-13 01:04:53 +08:00
|
|
|
});
|
2015-12-02 04:31:15 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("/widgets/:widget_id", function(request) {
|
|
|
|
const w = _widgets.findBy("id", parseInt(request.params.widget_id));
|
2015-12-02 04:31:15 +08:00
|
|
|
if (w) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return response({ widget: w, extras: { hello: "world" } });
|
2015-12-02 04:31:15 +08:00
|
|
|
} else {
|
|
|
|
return response(404);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.post("/widgets", function(request) {
|
2015-12-02 04:31:15 +08:00
|
|
|
const widget = parsePostData(request.requestBody).widget;
|
|
|
|
widget.id = 100;
|
2018-06-15 23:03:24 +08:00
|
|
|
return response(200, { widget });
|
2015-12-02 04:31:15 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.put("/widgets/:widget_id", function(request) {
|
2015-12-02 04:31:15 +08:00
|
|
|
const widget = parsePostData(request.requestBody).widget;
|
|
|
|
return response({ widget });
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.put("/cool_things/:cool_thing_id", function(request) {
|
2015-12-02 04:31:15 +08:00
|
|
|
const cool_thing = parsePostData(request.requestBody).cool_thing;
|
|
|
|
return response({ cool_thing });
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("/widgets", function(request) {
|
2015-12-02 04:31:15 +08:00
|
|
|
let result = _widgets;
|
|
|
|
|
|
|
|
const qp = request.queryParams;
|
|
|
|
if (qp) {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (qp.name) {
|
|
|
|
result = result.filterBy("name", qp.name);
|
|
|
|
}
|
|
|
|
if (qp.id) {
|
|
|
|
result = result.filterBy("id", parseInt(qp.id));
|
|
|
|
}
|
2015-12-02 04:31:15 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 01:04:53 +08:00
|
|
|
return response({
|
|
|
|
widgets: result,
|
|
|
|
meta: {
|
|
|
|
total_rows_widgets: 4,
|
2018-06-15 23:03:24 +08:00
|
|
|
load_more_widgets: "/load-more-widgets",
|
|
|
|
refresh_widgets: "/widgets?refresh=true"
|
2017-09-13 01:04:53 +08:00
|
|
|
}
|
|
|
|
});
|
2015-12-02 04:31:15 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("/load-more-widgets", function() {
|
|
|
|
return response({
|
|
|
|
widgets: _moreWidgets,
|
|
|
|
total_rows_widgets: 4,
|
|
|
|
load_more_widgets: "/load-more-widgets"
|
|
|
|
});
|
2015-12-02 04:31:15 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.delete("/widgets/:widget_id", success);
|
|
|
|
}
|