mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-04-28 17:35:50 +08:00
user and app routes
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
// use spawn inheritance
|
||||
require('./spawn');
|
||||
|
||||
lib = {};
|
||||
|
||||
var config = require('./config').load()
|
||||
, fs = require('fs')
|
||||
, app = require('./app')
|
||||
|
||||
@@ -5,7 +5,6 @@ var Model = require('./model')
|
||||
module.exports = Model.spawn({
|
||||
|
||||
initialize: function() {
|
||||
console.log('initialized collection');
|
||||
this.models = [];
|
||||
},
|
||||
|
||||
@@ -20,6 +19,12 @@ module.exports = Model.spawn({
|
||||
);
|
||||
});
|
||||
|
||||
if(this.wrap) {
|
||||
var wrappedResults = {};
|
||||
wrappedResults[this.wrap] = result;
|
||||
return wrappedResults;
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
|
||||
@@ -61,7 +61,12 @@ module.exports = emitter.spawn({
|
||||
},
|
||||
|
||||
save: function() {
|
||||
this.sync(states.write);
|
||||
if(this.errors) {
|
||||
// manually continue the sync
|
||||
this.emit('change:state');
|
||||
} else {
|
||||
this.sync(states.write);
|
||||
}
|
||||
return this;
|
||||
},
|
||||
|
||||
@@ -139,7 +144,7 @@ module.exports = emitter.spawn({
|
||||
|
||||
toQuery: function() {
|
||||
//if(this.isNew()) throw new Error('Tried to query a single model without an id');
|
||||
return {_id: this.get('_id')};
|
||||
return this.query || {_id: this.get('_id')};
|
||||
},
|
||||
|
||||
error: function(err, type) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var Model = require('model')
|
||||
var Model = require('../../model')
|
||||
, ObjectID = require('mongodb').BSONPure.ObjectID
|
||||
, exec = require('child_process').exec
|
||||
, _ = require('underscore')
|
||||
@@ -10,14 +10,25 @@ module.exports = Model.spawn({
|
||||
collection: 'apps',
|
||||
|
||||
save: function() {
|
||||
var app = this.get('name').replace(' ', '-');
|
||||
// TODO: validate
|
||||
Model.save.apply(this, arguments);
|
||||
if(this.isNew()) {
|
||||
this.create(app, this.get('config'));
|
||||
var name = this.get('name')
|
||||
, app = name && name.replace(/ /g, '-')
|
||||
, creator = this.get('creator')
|
||||
;
|
||||
|
||||
console.log(app);
|
||||
|
||||
if(app && creator) {
|
||||
if(this.isNew()) {
|
||||
this.create(app, {name: name});
|
||||
} else {
|
||||
this.restart(app);
|
||||
}
|
||||
} else {
|
||||
this.restart(app);
|
||||
app || this.error('App.name is required.', 'App');
|
||||
creator || this.error('Must be logged in to create an app.', 'App');
|
||||
}
|
||||
|
||||
Model.save.apply(this, arguments);
|
||||
},
|
||||
|
||||
create: function(app, config) {
|
||||
|
||||
@@ -1,13 +1,37 @@
|
||||
var app = require('app');
|
||||
var app = require('../../app')
|
||||
, App = require('./app')
|
||||
;
|
||||
|
||||
app.get('/me/apps', function() {
|
||||
app.get('/apps', function() {
|
||||
|
||||
});
|
||||
|
||||
app.post('/app', function() {
|
||||
app.post('/app', function(req, res) {
|
||||
var session = req.session
|
||||
, me = session && session.user.uid
|
||||
;
|
||||
|
||||
App
|
||||
.spawn()
|
||||
.set({name: req.param('name'), creator: me})
|
||||
.notify(res)
|
||||
.save()
|
||||
;
|
||||
});
|
||||
|
||||
app.get('/app/:id', function() {
|
||||
App
|
||||
.spawn()
|
||||
.set({_id: req.param('id')})
|
||||
.notify(res)
|
||||
.fetch()
|
||||
;
|
||||
});
|
||||
|
||||
});
|
||||
app.del('/app/:id', function(req, res) {
|
||||
App
|
||||
.spawn()
|
||||
.notify(res)
|
||||
.remove()
|
||||
;
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
var app = require('app')
|
||||
var app = require('../../app')
|
||||
, User = require('./user')
|
||||
, Users = require('./users')
|
||||
;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var Model = require('model')
|
||||
var Model = require('../../model')
|
||||
, ObjectID = require('mongodb').BSONPure.ObjectID
|
||||
, _ = require('underscore')
|
||||
;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var Collection = require('collection')
|
||||
var Collection = require('../../collection')
|
||||
, User = require('./user')
|
||||
;
|
||||
|
||||
|
||||
@@ -22,7 +22,5 @@ function d(route, data, callback) {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(options);
|
||||
|
||||
$.ajax(options);
|
||||
}
|
||||
@@ -21,7 +21,7 @@ var app = {
|
||||
|
||||
var tests = {
|
||||
|
||||
'creating a user': {
|
||||
'1. creating a user': {
|
||||
route: '/user',
|
||||
data: user,
|
||||
expect: {
|
||||
@@ -32,7 +32,7 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'finding a user': {
|
||||
'2. finding a user': {
|
||||
route: '/user/' + user.uid,
|
||||
expect: {
|
||||
_id: 'toExist',
|
||||
@@ -41,7 +41,7 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'login a user': {
|
||||
'3. login a user': {
|
||||
route: '/user/login',
|
||||
data: user,
|
||||
expect: {
|
||||
@@ -50,12 +50,12 @@ var tests = {
|
||||
auth: 'toExist',
|
||||
errors: 'toNotExist'
|
||||
},
|
||||
complete: function(res) {
|
||||
after: function(res) {
|
||||
auth = res.auth;
|
||||
}
|
||||
},
|
||||
|
||||
'get me': {
|
||||
'4. get me': {
|
||||
route: '/me',
|
||||
expect: {
|
||||
uid: user.uid,
|
||||
@@ -65,25 +65,43 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'searching users': {
|
||||
route: '/search?type=user&find={uid: "skawful@gmail.com"}',
|
||||
'5. searching users': {
|
||||
route: '/search?type=users&find={"uid": "skawful@gmail.com"}',
|
||||
expect: {
|
||||
results: 'toExist',
|
||||
erros: 'toNotExist'
|
||||
errors: 'toNotExist'
|
||||
}
|
||||
},
|
||||
|
||||
'delete a user': {
|
||||
'6. delete a user': {
|
||||
route: '/me?method=delete',
|
||||
expect: {
|
||||
errors: 'toNotExist'
|
||||
}
|
||||
},
|
||||
|
||||
'7. creating an app': {
|
||||
route: '/app',
|
||||
data: app,
|
||||
expect: {
|
||||
_id: 'toExist',
|
||||
name: app.name,
|
||||
errors: 'toNotExist'
|
||||
},
|
||||
after: function(result) {
|
||||
app = result;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var testNames = Object.keys(tests)
|
||||
, sorted = testNames.sort()
|
||||
;
|
||||
|
||||
// execute tests
|
||||
for(var context in tests) {
|
||||
for(var i = 0; i < sorted.length; i++) {
|
||||
var context = sorted[i];
|
||||
if(tests.hasOwnProperty(context)) {
|
||||
describe(context, function() {
|
||||
var test = tests[context];
|
||||
@@ -91,11 +109,11 @@ for(var context in tests) {
|
||||
|
||||
var args = []
|
||||
, finished = false
|
||||
, complete = test.complete
|
||||
, after = test.after
|
||||
, callback = function(res) {
|
||||
console.log('finished');
|
||||
finished = true;
|
||||
complete && complete(res);
|
||||
after && after(res);
|
||||
// dynamic expects
|
||||
if(test.expect) {
|
||||
for(var p in test.expect) {
|
||||
@@ -120,7 +138,7 @@ for(var context in tests) {
|
||||
}
|
||||
;
|
||||
|
||||
// build arguments do d()
|
||||
// build arguments
|
||||
args.push(test.route);
|
||||
test.data && args.push(test.data);
|
||||
args.push(callback);
|
||||
|
||||
Reference in New Issue
Block a user