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