Skip to content

Commit

Permalink
Rename api resource as resources
Browse files Browse the repository at this point in the history
  • Loading branch information
gocreating committed Oct 14, 2016
1 parent 913fa64 commit 039baad
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ configs/**/credential.*
/build
/.deploy
/src/public/uploads
/src/public/user
/src/public/users
/src/native/styles/index.js
my-release-key.keystore

Expand Down
8 changes: 4 additions & 4 deletions specs/endToEnd/apis/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe('#locale', () => {

describe('#Unauthorized User', () => {
// GET /api/locale/{validLocaleName}
describe('GET /api/locale/{validLocaleName}', () => {
describe('GET /api/locales/{validLocaleName}', () => {
it('should download valid locale', (done) => {
async.eachSeries(validLocales, (validLocale, cb) => {
request
.get(constants.BASE + '/api/locale/' + validLocale)
.get(constants.BASE + '/api/locales/' + validLocale)
.end((err, res) => {
expect(err).to.equal(null);
expect(res).to.not.be.undefined;
Expand All @@ -38,11 +38,11 @@ describe('#locale', () => {
});

// GET /api/locale/{invalidLocaleName}
describe('GET /api/locale/{invalidLocaleName}', () => {
describe('GET /api/locales/{invalidLocaleName}', () => {
it('should reject invalid locale', (done) => {
async.eachSeries(invalidLocales, (invalidLocale, cb) => {
request
.get(constants.BASE + '/api/locale/' + invalidLocale)
.get(constants.BASE + '/api/locales/' + invalidLocale)
.end((err, res) => {
expect(err).to.equal(null);
expect(res).to.not.be.undefined;
Expand Down
8 changes: 4 additions & 4 deletions specs/endToEnd/apis/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ describe('#todo', () => {

describe('#Unauthorized User', () => {
// POST /api/todo
describe('POST /api/todo', () => {
describe('POST /api/todos', () => {
it('should create todo', (done) => {
async.eachSeries(fakeTodos, (fakeTodo, cb) => {
request
.post(constants.BASE + '/api/todo')
.post(constants.BASE + '/api/todos')
.send(fakeTodo)
.end((err, res) => {
expect(err).to.equal(null);
Expand All @@ -42,10 +42,10 @@ describe('#todo', () => {
});

// GET /api/todo
describe('GET /api/todo', () => {
describe('GET /api/todos', () => {
it('should list todos', (done) => {
request
.get(constants.BASE + '/api/todo')
.get(constants.BASE + '/api/todos')
.end((err, res) => {
expect(err).to.equal(null);
expect(res).to.not.be.undefined;
Expand Down
22 changes: 11 additions & 11 deletions specs/endToEnd/apis/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ describe('#user', () => {

describe('#Unauthorized User', () => {
// POST /api/user
describe('POST /api/user', () => {
describe('POST /api/users', () => {
fakeUser = {
name: features.user[0].name,
email: features.user[0].email.value,
password: features.user[0].password,
};
it('should create user', (done) => {
request
.post(constants.BASE + '/api/user')
.post(constants.BASE + '/api/users')
.send(fakeUser)
.end((err, res) => {
expect(err).to.equal(null);
Expand All @@ -43,7 +43,7 @@ describe('#user', () => {
});
it('should fail when email is duplicate', (done) => {
request
.post(constants.BASE + '/api/user')
.post(constants.BASE + '/api/users')
.send(fakeUser)
.end((err, res) => {
expect(err).to.equal(null);
Expand All @@ -57,14 +57,14 @@ describe('#user', () => {
});

// POST /api/user/login
describe('POST /api/user/login', () => {
describe('POST /api/users/login', () => {
it('should auth valid user', (done) => {
fakeUser = {
email: features.user[0].email.value,
password: features.user[0].password,
};
request
.post(constants.BASE + '/api/user/login')
.post(constants.BASE + '/api/users/login')
.send(fakeUser)
.end((err, res) => {
expect(err).to.equal(null);
Expand All @@ -78,7 +78,7 @@ describe('#user', () => {
});
it('should reject invalid user', (done) => {
request
.post(constants.BASE + '/api/user/login')
.post(constants.BASE + '/api/users/login')
.send({})
.end((err, res) => {
expect(err).to.equal(null);
Expand All @@ -95,7 +95,7 @@ describe('#user', () => {
describe('GET /api/user/logout', () => {
it('should unauth user', (done) => {
request
.get(constants.BASE + '/api/user/logout')
.get(constants.BASE + '/api/users/logout')
.end((err, res) => {
expect(err).to.equal(null);
expect(res).to.not.be.undefined;
Expand All @@ -107,10 +107,10 @@ describe('#user', () => {
});

// GET /api/user/me
describe('GET /api/user/me', () => {
describe('GET /api/users/me', () => {
it('should be rejected', (done) => {
request
.get(constants.BASE + '/api/user/me')
.get(constants.BASE + '/api/users/me')
.end((err, res) => {
expect(err).to.equal(null);
expect(res).to.not.be.undefined;
Expand All @@ -125,13 +125,13 @@ describe('#user', () => {

describe('#Authorized User', () => {
// GET /api/user/me
describe('GET /api/user/me', () => {
describe('GET /api/users/me', () => {
it('should show user', (done) => {
User.findOne({}, (err, user) => {
expect(err).to.equal(null);
let token = user.toJwtToken();
request
.get(constants.BASE + '/api/user/me')
.get(constants.BASE + '/api/users/me')
.set('Cookie', 'token=' + token)
.end((err, res) => {
expect(err).to.equal(null);
Expand Down
2 changes: 1 addition & 1 deletion src/common/api/firebase.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default (apiEngine) => ({
readToken: () => apiEngine.get('/api/user/me/firebase/token'),
readToken: () => apiEngine.get('/api/users/me/firebase/token'),
});
2 changes: 1 addition & 1 deletion src/common/api/locale.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default (apiEngine) => ({
read: (locale) => apiEngine.get(`/api/locale/${locale}`),
read: (locale) => apiEngine.get(`/api/locales/${locale}`),
});
6 changes: 3 additions & 3 deletions src/common/api/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default (apiEngine) => ({
// resolve(apiEngine.get('/api/todo'));
// }, 5000);
// }),
list: () => apiEngine.get('/api/todo'),
create: (todo) => apiEngine.post('/api/todo', { data: todo }),
remove: (id) => apiEngine.del(`/api/todo/${id}`),
list: () => apiEngine.get('/api/todos'),
create: (todo) => apiEngine.post('/api/todos', { data: todo }),
remove: (id) => apiEngine.del(`/api/todos/${id}`),
});
12 changes: 6 additions & 6 deletions src/common/api/user.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export default (apiEngine) => ({
register: (user) => apiEngine.post('/api/user', { data: user }),
login: (user) => apiEngine.post('/api/user/login', { data: user }),
logout: () => apiEngine.get('/api/user/logout'),
show: () => apiEngine.get('/api/user/me'),
update: (user) => apiEngine.put('/api/user/me', { data: user }),
register: (user) => apiEngine.post('/api/users', { data: user }),
login: (user) => apiEngine.post('/api/users/login', { data: user }),
logout: () => apiEngine.get('/api/users/logout'),
show: () => apiEngine.get('/api/users/me'),
update: (user) => apiEngine.put('/api/users/me', { data: user }),
uploadAvatar: (avatar) =>
apiEngine.post('/api/user/me/avatar', { files: { avatar } }),
apiEngine.post('/api/users/me/avatar', { files: { avatar } }),
});
2 changes: 1 addition & 1 deletion src/server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default {
// use `req.file` to access the avatar file
// and use `req.body` to access other fileds
res.json({
downloadURL: `/user/${req.user._id}/${req.file.filename}`,
downloadURL: `/users/${req.user._id}/${req.file.filename}`,
});
},
};
28 changes: 16 additions & 12 deletions src/server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ import todoController from '../controllers/todo';

export default ({ app }) => {
// user
app.post('/api/user', bodyParser.json, userController.create);
app.post('/api/user/login', bodyParser.json, userController.login);
app.get('/api/user/logout', userController.logout);
app.get('/api/user/me', authRequired, userController.show);
app.put('/api/user/me', authRequired, bodyParser.json, userController.update);
app.post('/api/users', bodyParser.json, userController.create);
app.post('/api/users/login', bodyParser.json, userController.login);
app.get('/api/users/logout', userController.logout);
app.get('/api/users/me', authRequired, userController.show);
app.put('/api/users/me',
authRequired,
bodyParser.json,
userController.update
);
if (configs.firebase) {
let firebaseController = require('../controllers/firebase').default;
app.get('/api/user/me/firebase/token',
app.get('/api/users/me/firebase/token',
authRequired, firebaseController.readToken);
}
app.post('/api/user/me/avatar',
app.post('/api/users/me/avatar',
authRequired,
fileUpload.disk({
destination: 'user/{userId}',
destination: 'users/{userId}',
filename: 'avatar.jpg',
}).single('avatar'),
userController.uploadAvatar);
Expand All @@ -34,10 +38,10 @@ export default ({ app }) => {
);

// locale
app.get('/api/locale/:locale', localeController.show);
app.get('/api/locales/:locale', localeController.show);

// todo
app.post('/api/todo', bodyParser.json, todoController.create);
app.get('/api/todo', todoController.list);
app.delete('/api/todo/:id', todoController.remove);
app.post('/api/todos', bodyParser.json, todoController.create);
app.get('/api/todos', todoController.list);
app.delete('/api/todos/:id', todoController.remove);
};

0 comments on commit 039baad

Please sign in to comment.