Skip to content

Commit

Permalink
API Models
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyra committed Nov 3, 2014
1 parent 779c549 commit b5a5892
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion api/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';

var mongoose = require('mongoose'),
User = mongoose.model('User');

exports.all = function(req, res) {
res.send(200, { result: "OK" });
User.find({}, function(err, users) {
if(err) {
res.send(400);
}
res.send(users);
});
};
11 changes: 11 additions & 0 deletions api/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var UserSchema = new Schema({
username: {type: String, index: {unique: true}},
email: {type: String, index: {unique: true}}
});

module.exports = mongoose.model('User', UserSchema);
16 changes: 16 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var fs = require('fs');
var express = require('express');
// var mongoose = require('mongoose');
var morgan = require('morgan');
Expand Down Expand Up @@ -27,6 +28,21 @@ app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride());

// API Models
(function(path) {
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if(stat.isFile()) {
if(/(.*)\.(js|coffee)/.test(file)) {
require(newPath);
}
} else if(stat.isDirectory()) {
// TODO: Allow for subfolders for models?
}
});
})(__dirname + '/api/models');

// API Routes
require('./api/routes')(app);

Expand Down

0 comments on commit b5a5892

Please sign in to comment.