Skip to content

Commit

Permalink
fist mongoose model with some methods (probably not working)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonlitvinenko committed Jul 10, 2011
1 parent 753fae7 commit b68b313
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
32 changes: 17 additions & 15 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 4 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ var tasks = require('./models/tasks').tasks();
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/tracker');

var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;

var Task = new Schema({
author : ObjectId
, title : String
});




var app = module.exports = express.createServer();

// Configuration
Expand Down Expand Up @@ -52,9 +41,10 @@ app.get('/', function(req, res) {


app.post('/tasks', function(req, res) {
var lastId = tasks.add(req.body.title);

okWithJSON(res, lastId);
tasks.create(req.body.title, function(error){
// todo do how to get id?
okWithJSON(res, lastId);
});
});

app.get('/tasks', function(req, res) {
Expand Down
23 changes: 19 additions & 4 deletions models/tasks.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
var mongoose = require('mongoose');

var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;

var Task = new Schema({
id: ObjectId,
title: String
});

mongoose.model('Task', Task);


exports.tasks = function() {
var datastore = [];

return {
add: function(title) {
datastore.push({id: datastore.length, title: title, completed: false});
return datastore.length - 1;
create: function(title, callback) {
var Task = mongoose.model('Task');
var task = new Task();
task.title = title;
task.save(callback);
},

getAll: function() {
return datastore;
}
}
}
};

4 changes: 2 additions & 2 deletions tests/models/tasks-model-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var taskModel = require('../../models/tasks').tasks;

(function shouldHaveOneTaskAfterAddition() {
var tasks = taskModel();
tasks.add('title');
tasks.create('title');

assert.equal(tasks.getAll().length, 1)
assert.equal(tasks.getAll()[0].title, 'title');
Expand All @@ -18,7 +18,7 @@ var taskModel = require('../../models/tasks').tasks;

(function getTaskshouldReturnExistingTask() {
var tasks = taskModel();
tasks.add('title');
tasks.create('title');

assert.equal(tasks.getAll().length, 1)
})();

0 comments on commit b68b313

Please sign in to comment.