Feature: First version of the REST API done

This commit is contained in:
2015-08-14 08:04:31 +00:00
commit e6fc0be0ed
28 changed files with 3170 additions and 0 deletions

25
app/models/account.js Normal file
View File

@@ -0,0 +1,25 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId;
var CategorySchema = new Schema({
label: {type: String, required:true},
key: {type: String, required: true, index: {unique: false} },
sub_categories: [{
label: {type: String, required:true},
key: {type: String, required: true, index: {unique: false} },
}]
});
var AccountSchema = new Schema({
name: {type: String, required: true},
reference: {type: String, required: false},
categories: {type: [CategorySchema], required: true},
user_id: {type: ObjectId, ref: 'User', required: true},
created_at: {type: Date, default: Date.now}
});
var Account = mongoose.model('Account', AccountSchema);
var Category = mongoose.model('Category', CategorySchema);
module.exports = Account;

1514
app/models/categories.js Normal file

File diff suppressed because it is too large Load Diff

20
app/models/entry.js Normal file
View File

@@ -0,0 +1,20 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId;
var DEBIT = 'D',
BILL = 'B';
var EntrySchema = new Schema({
account_id: {type: ObjectId, ref: 'Category', required: true},
category: {type: ObjectId, ref: 'Account', required: false},
sub_category: {type: ObjectId, required: false},
label: {type: String, required:false},
type: {type: String, required: true},
amount: {type: Number, required: true},
date: {type: Date, required: true},
created_at: {type: Date, default: Date.now}
});
var Entry = mongoose.model('Entry', EntrySchema);
module.exports = Entry;

87
app/models/user.js Normal file
View File

@@ -0,0 +1,87 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
bcrypt = require('bcrypt'),
SALT_WORK_FACTOR = 10;
var UserSchema = new Schema({
username: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true },
language: {type: String, required: true, default: 'en'},
created_at: {type: Date, 'default': Date.now}
});
UserSchema.statics.getAuthenticated = function(username, password, callback) {
this.findOne({ username: username }, function(error, user) {
if (error) {
console.error(error);
return callback(error);
}
// make sure the user exists
if (!user) {
return callback(null, null, 404);
}
user.comparePassword(password, function(error, isMatch) {
if (isMatch) {
return callback(null, user);
}
return callback(null, null, 401);
});
});
};
UserSchema.pre('save', function(next) {
var user = this;
// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) {
return next();
}
// generate a salt
bcrypt.genSalt(SALT_WORK_FACTOR, function(error, salt) {
if (error) {
console.log(error);
return next(error);
}
// hash the password using our new salt
bcrypt.hash(user.password, salt, function(error, hash) {
if (error) {
return next(error);
}
// override the cleartext password with the hashed one
user.password = hash;
next();
});
});
});
UserSchema.methods.comparePassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(error, isMatch) {
if (error) {
return callback(error);
}
callback(null, isMatch);
});
};
var User = mongoose.model('User', UserSchema);
User.schema.path('username').validate(function (username) {
return username.length;
}, 'Username cannot be blank');
User.schema.path('password').validate(function(password) {
return password.length;
}, 'Password cannot be blank');
User.schema.path('language').validate(function(language) {
return /en|fr/i.test(language);
}, 'Unknown language ("en" or "fr" only)')
module.exports = User;