2015-10-28 14:36:45 +03:00
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2015-10-13 16:52:41 +03:00
|
|
|
import Ember from 'ember';
|
2016-02-02 15:22:41 +03:00
|
|
|
import Mirage from 'ember-cli-mirage';
|
2015-10-13 16:52:41 +03:00
|
|
|
|
2016-06-11 19:52:36 +03:00
|
|
|
const {
|
|
|
|
$,
|
|
|
|
isBlank,
|
|
|
|
String: {dasherize}
|
|
|
|
} = Ember;
|
2015-10-13 16:52:41 +03:00
|
|
|
|
|
|
|
function paginatedResponse(modelName, allModels, request) {
|
2015-11-13 14:48:59 +03:00
|
|
|
let page = +request.queryParams.page || 1;
|
2015-10-13 16:52:41 +03:00
|
|
|
let limit = request.queryParams.limit || 15;
|
|
|
|
let pages, models, next, prev;
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
allModels = allModels || [];
|
|
|
|
|
2015-10-13 16:52:41 +03:00
|
|
|
if (limit === 'all') {
|
|
|
|
models = allModels;
|
|
|
|
pages = 1;
|
|
|
|
} else {
|
|
|
|
limit = +limit;
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
let start = (page - 1) * limit;
|
|
|
|
let end = start + limit;
|
2015-10-13 16:52:41 +03:00
|
|
|
|
|
|
|
models = allModels.slice(start, end);
|
|
|
|
pages = Math.ceil(allModels.length / limit);
|
|
|
|
|
|
|
|
if (start > 0) {
|
|
|
|
prev = page - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end < allModels.length) {
|
|
|
|
next = page + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
2015-10-28 14:36:45 +03:00
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
pages,
|
2015-10-13 16:52:41 +03:00
|
|
|
total: allModels.length,
|
|
|
|
next: next || null,
|
|
|
|
prev: prev || null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[modelName]: models
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
function mockSubscribers(server) {
|
|
|
|
server.get('/subscribers/', function (db, request) {
|
|
|
|
let response = paginatedResponse('subscribers', db.subscribers, request);
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
|
|
|
|
server.post('/subscribers/', function (db, request) {
|
|
|
|
let [attrs] = JSON.parse(request.requestBody).subscribers;
|
2016-05-08 16:15:04 +03:00
|
|
|
let [subscriber] = db.subscribers.where({email: attrs.email});
|
|
|
|
|
|
|
|
if (subscriber) {
|
|
|
|
return new Mirage.Response(422, {}, {
|
|
|
|
errors: [{
|
2016-05-11 20:56:58 +03:00
|
|
|
errorType: 'ValidationError',
|
|
|
|
message: 'Email already exists.',
|
2016-05-08 16:15:04 +03:00
|
|
|
property: 'email'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
attrs.created_at = new Date();
|
|
|
|
attrs.created_by = 0;
|
|
|
|
|
|
|
|
subscriber = db.subscribers.insert(attrs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscriber
|
|
|
|
};
|
|
|
|
}
|
2016-04-15 17:45:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
server.put('/subscribers/:id/', function (db, request) {
|
|
|
|
let {id} = request.params;
|
|
|
|
let [attrs] = JSON.parse(request.requestBody).subscribers;
|
|
|
|
let subscriber = db.subscribers.update(id, attrs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscriber
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
server.del('/subscribers/:id/', function (db, request) {
|
|
|
|
db.subscribers.remove(request.params.id);
|
|
|
|
|
|
|
|
return new Mirage.Response(204, {}, {});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.post('/subscribers/csv/', function (/*db, request*/) {
|
|
|
|
// NB: we get a raw FormData object with no way to inspect it in Chrome
|
|
|
|
// until version 50 adds the additional read methods
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/FormData#Browser_compatibility
|
|
|
|
|
|
|
|
server.createList('subscriber', 50);
|
|
|
|
|
|
|
|
return {
|
2016-05-08 10:12:37 +03:00
|
|
|
meta: {
|
|
|
|
stats: {
|
|
|
|
imported: 50,
|
|
|
|
duplicates: 3,
|
|
|
|
invalid: 2
|
|
|
|
}
|
|
|
|
}
|
2016-04-15 17:45:50 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-13 16:52:41 +03:00
|
|
|
export default function () {
|
|
|
|
// this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server
|
|
|
|
this.namespace = 'ghost/api/v0.1'; // make this `api`, for example, if your API is namespaced
|
2016-04-15 17:45:50 +03:00
|
|
|
this.timing = 400; // delay for each request, automatically set to 0 during testing
|
2015-10-13 16:52:41 +03:00
|
|
|
|
2016-04-13 12:44:09 +03:00
|
|
|
// Mock endpoints here to override real API requests during development
|
2016-04-15 17:45:50 +03:00
|
|
|
mockSubscribers(this);
|
2016-04-13 12:44:09 +03:00
|
|
|
|
|
|
|
// keep this line, it allows all other API requests to hit the real server
|
|
|
|
this.passthrough();
|
|
|
|
|
|
|
|
// add any external domains to make sure those get passed through too
|
|
|
|
this.passthrough('https://count.ghost.org/');
|
|
|
|
this.passthrough('http://www.gravatar.com/**');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mock all endpoints here as there is no real API during testing
|
|
|
|
export function testConfig() {
|
|
|
|
// this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server
|
|
|
|
this.namespace = 'ghost/api/v0.1'; // make this `api`, for example, if your API is namespaced
|
|
|
|
// this.timing = 400; // delay for each request, automatically set to 0 during testing
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
/* Authentication ------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.post('/authentication/token', function () {
|
|
|
|
return {
|
|
|
|
access_token: '5JhTdKI7PpoZv4ROsFoERc6wCHALKFH5jxozwOOAErmUzWrFNARuH1q01TYTKeZkPW7FmV5MJ2fU00pg9sm4jtH3Z1LjCf8D6nNqLYCfFb2YEKyuvG7zHj4jZqSYVodN2YTCkcHv6k8oJ54QXzNTLIDMlCevkOebm5OjxGiJpafMxncm043q9u1QhdU9eee3zouGRMVVp8zkKVoo5zlGMi3zvS2XDpx7xsfk8hKHpUgd7EDDQxmMueifWv7hv6n',
|
|
|
|
expires_in: 3600,
|
|
|
|
refresh_token: 'XP13eDjwV5mxOcrq1jkIY9idhdvN3R1Br5vxYpYIub2P5Hdc8pdWMOGmwFyoUshiEB62JWHTl8H1kACJR18Z8aMXbnk5orG28br2kmVgtVZKqOSoiiWrQoeKTqrRV0t7ua8uY5HdDUaKpnYKyOdpagsSPn3WEj8op4vHctGL3svOWOjZhq6F2XeVPMR7YsbiwBE8fjT3VhTB3KRlBtWZd1rE0Qo2EtSplWyjGKv1liAEiL0ndQoLeeSOCH4rTP7',
|
|
|
|
token_type: 'Bearer'
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-02-02 15:22:41 +03:00
|
|
|
this.post('/authentication/passwordreset', function (db, request) {
|
|
|
|
// jscs:disable requireObjectDestructuring
|
|
|
|
let {passwordreset} = $.deparam(request.requestBody);
|
|
|
|
let email = passwordreset[0].email;
|
|
|
|
// jscs:enable requireObjectDestructuring
|
|
|
|
|
|
|
|
if (email === 'unknown@example.com') {
|
|
|
|
return new Mirage.Response(404, {}, {
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'There is no user with that email address.',
|
|
|
|
errorType: 'NotFoundError'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
passwordreset: [
|
|
|
|
{message: 'Check your email for further instructions.'}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
/* Download Count ------------------------------------------------------- */
|
|
|
|
|
|
|
|
let downloadCount = 0;
|
2016-02-09 13:20:24 +03:00
|
|
|
this.get('https://count.ghost.org/', function () {
|
2015-11-13 14:48:59 +03:00
|
|
|
downloadCount++;
|
|
|
|
return {
|
|
|
|
count: downloadCount
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-10-13 16:52:41 +03:00
|
|
|
/* Notifications -------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.get('/notifications/', 'notifications');
|
|
|
|
|
2015-11-04 18:20:11 +03:00
|
|
|
/* Posts ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.post('/posts/', function (db, request) {
|
2015-11-13 14:48:59 +03:00
|
|
|
let [attrs] = JSON.parse(request.requestBody).posts;
|
2015-11-04 18:20:11 +03:00
|
|
|
let post;
|
|
|
|
|
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
|
|
|
|
attrs.slug = attrs.title.dasherize();
|
|
|
|
}
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
// NOTE: this does not use the post factory to fill in blank fields
|
2015-11-04 18:20:11 +03:00
|
|
|
post = db.posts.insert(attrs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
posts: [post]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
this.get('/posts/', function (db, request) {
|
|
|
|
// TODO: handle status/staticPages/author params
|
|
|
|
let response = paginatedResponse('posts', db.posts, request);
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
|
Timezones: Always use the timezone of blog setting
closes TryGhost/Ghost#6406
follow-up PR of #2
- adds a `timeZone` Service to provide the offset (=timezone reg. moment-timezone) of the users blog settings
- `gh-datetime-input` will read the offset of the timezone now and adjust the `publishedAt` date with it. This is the date which will be shown in the PSM 'Publish Date' field. When the user writes a new date/time, the offset is considered and will be deducted again before saving it to the model. This way, we always work with a UTC publish date except for this input field.
- gets `availableTimezones` from `configuration/timezones` API endpoint
- adds a `moment-utc` transform on all date attr (`createdAt`, `updatedAt`, `publishedAt`, `unsubscribedAt` and `lastLogin`) to only work with UTC times on serverside
- when switching the timezone in the select box, the user will be shown the local time of the selected timezone
- `createdAt`-property in `gh-user-invited` returns now `moment(createdAt).fromNow()` as `createdAt` is a moment date already
- added clock service to show actual time ticking below select box
- default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London'
- if no timezone is saved in the settings yet, the default value will be used
- shows the local time in 'Publish Date' in PSM by default, until user overwrites it
- adds dependency `moment-timezone 0.5.4` to `bower.json`
---------
**Tests:**
- sets except for clock service in test env
- adds fixtures to mirage
- adds `service.ajax` and `service:ghostPaths` to navigation-test.js
- adds unit test for `gh-format-timeago` helper
- updates acceptance test `general-setting`
- adds acceptance test for `editor`
- adds integration tests for `services/config` and `services/time-zone`
---------
**Todos:**
- [ ] Integration tests: ~~`services/config`~~, ~~`services/time-zone`~~, `components/gh-datetime-input`
- [x] Acceptance test: `editor`
- [ ] Unit tests: `utils/date-formatting`
- [ ] write issue for renaming date properties (e. g. `createdAt` to `createdAtUTC`) and translate those for server side with serializers
2016-02-02 10:04:40 +03:00
|
|
|
this.get('/posts/:id', function (db, request) {
|
|
|
|
let {id} = request.params;
|
|
|
|
let post = db.posts.find(id);
|
|
|
|
|
|
|
|
return {
|
|
|
|
posts: [post]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.put('/posts/:id/', function (db, request) {
|
|
|
|
let {id} = request.params;
|
|
|
|
let [attrs] = JSON.parse(request.requestBody).posts;
|
|
|
|
delete attrs.id;
|
|
|
|
|
|
|
|
let post = db.posts.update(id, attrs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
posts: [post]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-03-20 20:26:42 +03:00
|
|
|
this.del('/posts/:id/', function (db, request) {
|
|
|
|
db.posts.remove(request.params.id);
|
|
|
|
|
|
|
|
return new Mirage.Response(204, {}, {});
|
|
|
|
});
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
/* Roles ---------------------------------------------------------------- */
|
|
|
|
|
2015-12-07 00:24:06 +03:00
|
|
|
this.get('/roles/', function (db, request) {
|
|
|
|
if (request.queryParams.permissions === 'assign') {
|
|
|
|
let roles = db.roles.find([1,2,3]);
|
|
|
|
return {roles};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
roles: db.roles
|
|
|
|
};
|
|
|
|
});
|
2015-11-13 14:48:59 +03:00
|
|
|
|
2015-10-13 16:52:41 +03:00
|
|
|
/* Settings ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.get('/settings/', function (db, request) {
|
2015-11-13 14:48:59 +03:00
|
|
|
let filters = request.queryParams.type.split(',');
|
|
|
|
let settings = [];
|
2015-10-13 16:52:41 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
filters.forEach((filter) => {
|
2015-10-13 16:52:41 +03:00
|
|
|
settings.pushObjects(db.settings.where({type: filter}));
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2015-10-28 14:36:45 +03:00
|
|
|
settings,
|
2015-10-13 16:52:41 +03:00
|
|
|
meta: {
|
|
|
|
filters: {
|
|
|
|
type: request.queryParams.type
|
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
}
|
2015-10-13 16:52:41 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.put('/settings/', function (db, request) {
|
2016-02-09 20:16:18 +03:00
|
|
|
let newSettings = JSON.parse(request.requestBody).settings;
|
2015-10-13 16:52:41 +03:00
|
|
|
|
|
|
|
db.settings.remove();
|
|
|
|
db.settings.insert(newSettings);
|
|
|
|
|
|
|
|
return {
|
|
|
|
meta: {},
|
|
|
|
settings: db.settings
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-03-29 11:40:44 +03:00
|
|
|
/* Apps - Slack Test Notification --------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.post('/slack/test', function () {
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
|
Timezones: Always use the timezone of blog setting
closes TryGhost/Ghost#6406
follow-up PR of #2
- adds a `timeZone` Service to provide the offset (=timezone reg. moment-timezone) of the users blog settings
- `gh-datetime-input` will read the offset of the timezone now and adjust the `publishedAt` date with it. This is the date which will be shown in the PSM 'Publish Date' field. When the user writes a new date/time, the offset is considered and will be deducted again before saving it to the model. This way, we always work with a UTC publish date except for this input field.
- gets `availableTimezones` from `configuration/timezones` API endpoint
- adds a `moment-utc` transform on all date attr (`createdAt`, `updatedAt`, `publishedAt`, `unsubscribedAt` and `lastLogin`) to only work with UTC times on serverside
- when switching the timezone in the select box, the user will be shown the local time of the selected timezone
- `createdAt`-property in `gh-user-invited` returns now `moment(createdAt).fromNow()` as `createdAt` is a moment date already
- added clock service to show actual time ticking below select box
- default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London'
- if no timezone is saved in the settings yet, the default value will be used
- shows the local time in 'Publish Date' in PSM by default, until user overwrites it
- adds dependency `moment-timezone 0.5.4` to `bower.json`
---------
**Tests:**
- sets except for clock service in test env
- adds fixtures to mirage
- adds `service.ajax` and `service:ghostPaths` to navigation-test.js
- adds unit test for `gh-format-timeago` helper
- updates acceptance test `general-setting`
- adds acceptance test for `editor`
- adds integration tests for `services/config` and `services/time-zone`
---------
**Todos:**
- [ ] Integration tests: ~~`services/config`~~, ~~`services/time-zone`~~, `components/gh-datetime-input`
- [x] Acceptance test: `editor`
- [ ] Unit tests: `utils/date-formatting`
- [ ] write issue for renaming date properties (e. g. `createdAt` to `createdAtUTC`) and translate those for server side with serializers
2016-02-02 10:04:40 +03:00
|
|
|
/* Configuration -------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.get('/configuration/timezones/', function (db) {
|
|
|
|
return {
|
|
|
|
configuration: [{
|
|
|
|
timezones: db.timezones
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-11-04 18:20:11 +03:00
|
|
|
/* Slugs ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.get('/slugs/post/:slug/', function (db, request) {
|
|
|
|
return {
|
|
|
|
slugs: [
|
2016-06-11 19:52:36 +03:00
|
|
|
{slug: dasherize(decodeURIComponent(request.params.slug))}
|
2015-12-07 00:24:06 +03:00
|
|
|
]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/slugs/user/:slug/', function (db, request) {
|
|
|
|
return {
|
|
|
|
slugs: [
|
2016-06-11 19:52:36 +03:00
|
|
|
{slug: dasherize(decodeURIComponent(request.params.slug))}
|
2015-11-04 18:20:11 +03:00
|
|
|
]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
/* Setup ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.post('/authentication/setup', function (db, request) {
|
|
|
|
let [attrs] = $.deparam(request.requestBody).setup;
|
|
|
|
let [role] = db.roles.where({name: 'Owner'});
|
|
|
|
let user;
|
|
|
|
|
|
|
|
// create owner role unless already exists
|
|
|
|
if (!role) {
|
|
|
|
role = db.roles.insert({name: 'Owner'});
|
|
|
|
}
|
|
|
|
attrs.roles = [role];
|
|
|
|
|
|
|
|
if (!isBlank(attrs.email)) {
|
|
|
|
attrs.slug = attrs.email.split('@')[0].dasherize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: this does not use the user factory to fill in blank fields
|
|
|
|
user = db.users.insert(attrs);
|
|
|
|
|
|
|
|
delete user.roles;
|
|
|
|
|
|
|
|
return {
|
|
|
|
users: [user]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/authentication/setup/', function () {
|
|
|
|
return {
|
|
|
|
setup: [
|
|
|
|
{status: true}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
/* Subscribers ---------------------------------------------------------- */
|
|
|
|
|
|
|
|
mockSubscribers(this);
|
|
|
|
|
2015-10-13 16:52:41 +03:00
|
|
|
/* Tags ----------------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.post('/tags/', function (db, request) {
|
2015-11-13 14:48:59 +03:00
|
|
|
let [attrs] = JSON.parse(request.requestBody).tags;
|
2015-10-13 16:52:41 +03:00
|
|
|
let tag;
|
|
|
|
|
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.name)) {
|
|
|
|
attrs.slug = attrs.name.dasherize();
|
|
|
|
}
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
// NOTE: this does not use the tag factory to fill in blank fields
|
2015-10-13 16:52:41 +03:00
|
|
|
tag = db.tags.insert(attrs);
|
|
|
|
|
|
|
|
return {
|
2015-10-28 14:36:45 +03:00
|
|
|
tag
|
2015-10-13 16:52:41 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/tags/', function (db, request) {
|
2015-11-13 14:48:59 +03:00
|
|
|
let response = paginatedResponse('tags', db.tags, request);
|
2015-10-13 16:52:41 +03:00
|
|
|
// TODO: remove post_count unless requested?
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/tags/slug/:slug/', function (db, request) {
|
2015-11-13 14:48:59 +03:00
|
|
|
let [tag] = db.tags.where({slug: request.params.slug});
|
2015-10-13 16:52:41 +03:00
|
|
|
|
|
|
|
// TODO: remove post_count unless requested?
|
|
|
|
|
|
|
|
return {
|
2015-10-28 14:36:45 +03:00
|
|
|
tag
|
2015-10-13 16:52:41 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.put('/tags/:id/', function (db, request) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let {id} = request.params;
|
2015-11-13 14:48:59 +03:00
|
|
|
let [attrs] = JSON.parse(request.requestBody).tags;
|
|
|
|
let record = db.tags.update(id, attrs);
|
2015-10-13 16:52:41 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
tag: record
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-03-20 20:26:42 +03:00
|
|
|
this.del('/tags/:id/', function (db, request) {
|
|
|
|
db.tags.remove(request.params.id);
|
|
|
|
|
|
|
|
return new Mirage.Response(204, {}, {});
|
|
|
|
});
|
2015-10-13 16:52:41 +03:00
|
|
|
|
|
|
|
/* Users ---------------------------------------------------------------- */
|
|
|
|
|
2015-11-13 14:48:59 +03:00
|
|
|
this.post('/users/', function (db, request) {
|
|
|
|
let [attrs] = JSON.parse(request.requestBody).users;
|
|
|
|
let user;
|
|
|
|
|
|
|
|
if (!isBlank(attrs.email)) {
|
|
|
|
attrs.slug = attrs.email.split('@')[0].dasherize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: this does not use the user factory to fill in blank fields
|
|
|
|
user = db.users.insert(attrs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
users: [user]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-10-13 16:52:41 +03:00
|
|
|
// /users/me = Always return the user with ID=1
|
|
|
|
this.get('/users/me', function (db) {
|
|
|
|
return {
|
|
|
|
users: [db.users.find(1)]
|
|
|
|
};
|
|
|
|
});
|
2015-11-04 18:20:11 +03:00
|
|
|
|
|
|
|
this.get('/users/', 'users');
|
2015-12-07 00:24:06 +03:00
|
|
|
|
|
|
|
this.get('/users/slug/:slug/', function (db, request) {
|
|
|
|
let user = db.users.where({slug: request.params.slug});
|
|
|
|
|
|
|
|
return {
|
|
|
|
users: user
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-03-20 20:26:42 +03:00
|
|
|
this.del('/users/:id/', function (db, request) {
|
|
|
|
db.users.remove(request.params.id);
|
|
|
|
|
|
|
|
return new Mirage.Response(204, {}, {});
|
|
|
|
});
|
2015-12-07 00:24:06 +03:00
|
|
|
|
|
|
|
this.get('/users/:id', function (db, request) {
|
|
|
|
return {
|
|
|
|
users: [db.users.find(request.params.id)]
|
|
|
|
};
|
|
|
|
});
|
2016-03-03 11:52:27 +03:00
|
|
|
|
|
|
|
this.put('/users/:id/', function (db, request) {
|
|
|
|
let {id} = request.params;
|
|
|
|
let [attrs] = JSON.parse(request.requestBody).users;
|
|
|
|
let record = db.users.update(id, attrs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
user: record
|
|
|
|
};
|
|
|
|
});
|
2016-03-25 07:03:45 +03:00
|
|
|
|
2016-04-12 14:34:40 +03:00
|
|
|
/* External sites ------------------------------------------------------- */
|
|
|
|
|
|
|
|
this.get('http://www.gravatar.com/avatar/:md5', function () {
|
|
|
|
return '';
|
|
|
|
}, 200);
|
2015-10-13 16:52:41 +03:00
|
|
|
}
|