Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhilash Nanda committed May 28, 2016
1 parent 76bf4de commit 581ba95
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 31 deletions.
18 changes: 12 additions & 6 deletions src/lib/base/BaseRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ let BaseSchema = require('src/lib/base/BaseSchema');

class Schema extends BaseSchema {
constructor(data) {
super();
this.setSchema(data);
super(data);
}
}

class BaseRepo {

constructor(data) {
this._schema = null;
constructor(props) {
props = props || {};
this._schema = new Schema(props);
this._table = null;
}

createSchema(schema) {
this._schema = new Schema(data);
this._schema.setSchema(schema);
}

* create(data) {
Expand All @@ -35,6 +35,10 @@ class BaseRepo {
this._schema.updateProperties(data);
}

* getProperties() {
return yield this._schema.getProperties();
}

* save() {
let data = null;
// Check if this._schema exists
Expand Down Expand Up @@ -77,4 +81,6 @@ class BaseRepo {

}

}
}

module.exports = BaseRepo;
7 changes: 7 additions & 0 deletions src/lib/base/BaseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class BaseSchema {
}

setProperties(props) {
props = props || {};
if (!('id' in props)) {
props['id'] = null;
}
if (!('last_modified' in props)) {
props['last_modified'] = Date.now();
}
this.props = props || {};
}

Expand Down
15 changes: 12 additions & 3 deletions src/repos/ContentRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ var config = require('src/config.js');
var r = require('rethinkdb');
var app = require('src/app.js');
let _ = require('lodash');
let BaseRepo = require('src/lib/base/BaseRepo');


class ContentRepo {
constructor() {
class ContentRepo extends BaseRepo {
constructor(data) {
super();
this._table = "content";

let schema = {
content: {'type': 'string'},
title: {'type': 'string'},
author: {'type': 'uuid'}
}

this.createSchema(schema);
}

* getById() {
Expand Down
16 changes: 3 additions & 13 deletions src/repos/UserRepo.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
'use strict';

var config = require('src/config.js');
var config = require('src/config');
var r = require('rethinkdb');
var app = require('src/app.js');
let _ = require('lodash');
let BaseRepo = require('src/lib/base/BaseRepo');
let bcrypt = require('co-bcrypt');


class UserRepo extends BaseRepo {
constructor(data) {
super();
constructor(props) {
super(props);
this._table = "users";

// Create the schema
Expand All @@ -23,14 +21,6 @@ class UserRepo extends BaseRepo {
this.createSchema(schema);
}

* setPassword(password) {
let salt = yield bcrypt.genSalt(10);
let hash = yield bcrypt.hash(password, salt);
this.update({
password_hash: hash
})
}

* getAll() {
try {
var cursor = yield r.table(this._table).run(app.context.rdbConn);
Expand Down
2 changes: 1 addition & 1 deletion src/routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

let Router = require('koa-router');
let UserController = require("src/controllers/user.js");
let UserController = require("src/controllers/UserController");

let router = new Router({
prefix: '/v1'
Expand Down
9 changes: 7 additions & 2 deletions src/services/UserService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var BaseService = require('src/lib/base/service');
var UserRepo = require('src/repos/UserRepo');

let bcrypt = require('co-bcrypt');

class UserService extends BaseService {
constructor(){
Expand Down Expand Up @@ -32,7 +33,11 @@ class UserService extends BaseService {
}

* setPassword() {

let salt = yield bcrypt.genSalt(10);
let hash = yield bcrypt.hash(password, salt);
userRepo.update({
password_hash: hash
});
}

* generateHash() {
Expand Down
14 changes: 14 additions & 0 deletions test/BaseSchemaSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ describe('BaseSchema testing', function() {
assert.equal(_.size(defaultSchema), _.size(baseSchema._schema));
});

it('schema size must pass', function *() {
let args = {
id: "s",
name: 's',
age: 2,
cgp: 1.2,
admin: false
};
let baseSchema = new BaseSchema(args);
baseSchema.setSchema(defaultSchema);
let m = yield baseSchema._checkSchemaSize();
assert(m[0]);
});

it('validate method must return true', function * () {
let args = {
id: "s",
Expand Down
13 changes: 7 additions & 6 deletions test/UserRepoSpec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
'use strict';
require('app-module-path').addPath(__dirname + "/..");

var assert = require('assert');
let assert = require('assert');
require('co-mocha');

var UserRepo = require('src/repos/UserRepo');
let UserRepo = require('src/repos/UserRepo');

describe('UserRepo testing', function() {
it('should instantiate', function * () {
var userRepo = new UserRepo();
let userRepo = new UserRepo();
assert.equal(typeof userRepo, 'object');
})

it('should take parameters', function * () {
var userRepo = new UserRepo({name: 'test', email: '[email protected]'});
assert.equal(userRepo.name, 'test');
assert.equal(userRepo.email, '[email protected]');
let userRepo = new UserRepo({name: 'test', email: '[email protected]'});
let props = yield userRepo.getProperties();
assert.equal(props.name, 'test');
assert.equal(props.email, '[email protected]');
})
});

0 comments on commit 581ba95

Please sign in to comment.