|
| 1 | +var mongojs = require('mongojs') |
| 2 | + |
| 3 | +var config = require('../config.js'); |
| 4 | +var db; |
| 5 | + |
| 6 | +function logError(err){ |
| 7 | + if(err){ |
| 8 | + if(err.message == 'ns not found'){ |
| 9 | + return; |
| 10 | + } else { |
| 11 | + console.log(err); |
| 12 | + } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function connectToDB(cb){ |
| 17 | + var dbConnection = mongojs(config.dbConnectionString, config.dbCollections); |
| 18 | + db = dbConnection; |
| 19 | + cb(); |
| 20 | +} |
| 21 | + |
| 22 | +function getListOfUsers(cb){ |
| 23 | + if(!db){ |
| 24 | + connectToDB(function(){ |
| 25 | + getListOfUsers(function(res){ |
| 26 | + cb(res); |
| 27 | + }); |
| 28 | + }); |
| 29 | + } else { |
| 30 | + db.userRegistry.find(function(err, docs){ |
| 31 | + logError(err); |
| 32 | + cb(docs); |
| 33 | + }); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function getUser(username, cb){ |
| 38 | + if(!db){ |
| 39 | + connectToDB(function(){ |
| 40 | + getUser(username, function(res){ |
| 41 | + cb(res); |
| 42 | + }); |
| 43 | + }); |
| 44 | + } else { |
| 45 | + db.userRegistry.findOne({username: {$regex: new RegExp('^'+username+'$', 'i')}}, function(err, docs){ |
| 46 | + logError(err); |
| 47 | + cb(docs); |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function getUserAndPassword(username, password, cb){ |
| 53 | + if(!db){ |
| 54 | + connectToDB(function(){ |
| 55 | + getUserAndPassword(username, password, function(res){ |
| 56 | + cb(res); |
| 57 | + }); |
| 58 | + }); |
| 59 | + } else { |
| 60 | + db.userRegistry.findOne({username: {$regex: new RegExp('^'+username+'$', 'i')}, password: password}, function(err, docs){ |
| 61 | + logError(err); |
| 62 | + cb(docs); |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function getUserRegistry(cb){ |
| 68 | + if(!db){ |
| 69 | + connectToDB(function(){ |
| 70 | + getUserRegistry(function(res){ |
| 71 | + cb(res); |
| 72 | + }); |
| 73 | + }); |
| 74 | + } else { |
| 75 | + db.userRegistry.find({}, function(err, docs){ |
| 76 | + logError(err); |
| 77 | + cb(docs); |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function findUserFromAddress(address, cb){ |
| 83 | + if(!db){ |
| 84 | + connectToDB(function(){ |
| 85 | + findUserFromAddress(address, function(res){ |
| 86 | + cb(res); |
| 87 | + }); |
| 88 | + }); |
| 89 | + } else { |
| 90 | + db.userRegistry.findOne({'accountAddressRepo.address': address}, function(err, docs){ |
| 91 | + logError(err); |
| 92 | + cb(docs); |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function addUserToRegistry(entry, cb){ |
| 98 | + if(!db){ |
| 99 | + connectToDB(function(){ |
| 100 | + addUserToRegistry(entry, function(res){ |
| 101 | + cb(res); |
| 102 | + }); |
| 103 | + }); |
| 104 | + } else { |
| 105 | + db.userRegistry.insert(entry, function(err, docs){ |
| 106 | + logError(err); |
| 107 | + cb(docs); |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function updateUser(entry, cb){ |
| 113 | + if(!db){ |
| 114 | + connectToDB(function(){ |
| 115 | + updateUser(entry, function(res){ |
| 116 | + cb(res); |
| 117 | + }); |
| 118 | + }); |
| 119 | + } else { |
| 120 | + db.userRegistry.save(entry, function(err, docs){ |
| 121 | + logError(err); |
| 122 | + cb(docs); |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +function closeDB(){ |
| 128 | + if(db != null || db != undefined){ |
| 129 | + db.close(); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +exports.GetUser = getUser; |
| 134 | +exports.GetListOfUsers = getListOfUsers; |
| 135 | +exports.GetUserRegistry = getUserRegistry; |
| 136 | +exports.AddUserToRegistry = addUserToRegistry; |
| 137 | +exports.CloseDB = closeDB; |
| 138 | +exports.GetUserAndPassword = getUserAndPassword; |
| 139 | +exports.UpdateUser = updateUser; |
| 140 | +exports.FindUserFromAddress = findUserFromAddress; |
0 commit comments