Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
Get data from MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Konev committed Jun 16, 2015
1 parent 3aa5dbd commit 5811e02
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 72 deletions.
2 changes: 1 addition & 1 deletion server/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
passportLoginCb: [
passport.authenticate('yandex', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/account');
res.redirect('/');
}
],

Expand Down
44 changes: 20 additions & 24 deletions server/controllers/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ var db = require('../lib/db');

module.exports = {

getGoods: function(req, res) {
var user_id = (req.query.user && req.query.user.split('/')[0]) || req.user.id;

db.getUser(user_id, function(err, user) {
var page = user.name;

db.getUserGoods(user_id, function(err, cursor) {
cursor.toArray(function(err, arr) {
getGoods: function(uid, needCount, cb) {
db.getUserGoods(uid, function(err, cursor) {
cursor.toArray(function(err, arr) {
var result = '';

arr.forEach(function(item) {
page += '<br>' + item.content;
});
if (needCount === true || !arr.length) {
result = 'Всего товаров: ' + arr.length;
} else {
result = arr;
}

res.send(page);
});
cb(result);
});
});
},
Expand All @@ -35,23 +32,22 @@ module.exports = {
res.send('set name = ' + data);
},

getUser: function(req, res) {
return req.user && db.getUser(req.user.id, function(err, user) {
return user;
})
getUser: function(uid, cb) {
uid && db.getUser(uid, cb)
},

getUsers: function(req, res) {
db.getUsers(function(err, cursor)
{
getUsers: function(needCount, cb) {
db.getUsers(function(err, cursor) {
cursor.toArray(function(err, arr) {
var result = '';

arr.forEach(function(user) {
result += JSON.stringify(user, null, 2);
});
if (needCount === true || !arr.length) {
result = 'Всего зарегистрировано пользователей: ' + arr.length;
} else {
result = arr;
}

res.end(result);
cb(result);
});
});
}
Expand Down
44 changes: 44 additions & 0 deletions server/controllers/good.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var configs = require('../configs/default'),
db = require('./db');

module.exports = {
index: function(req, res, next) {
var data = configs.defaults(req),
render = function() {
return res.render('index', data, function(err, html) {
if (err) {
res.send(500, err);
} else {
res.send(html);
}
});
};

data.contentType = 'userGoods';

db.getGoods(req.user.id, false, function (result) {
data.userGoods = result;
render();
});
},

users: function(req, res, next) {
var data = configs.defaults(req),
render = function() {
return res.render('index', data, function(err, html) {
if (err) {
res.send(500, err);
} else {
res.send(html);
}
});
};

data.contentType = 'users';

db.getUsers(false, function(result) {
data.users = result;
render();
}, true);
}
};
28 changes: 12 additions & 16 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
var configs = require('../configs/default'),
db = require('../lib/db');
var configs = require('../configs/default');

module.exports = {
index: function(req, res, next) {
var data = configs.defaults(req);
var data = configs.defaults(req),
render = function() {
return res.render('index', data, function(err, html) {
if (err) {
res.send(500, err);
} else {
res.send(html);
}
});
};

//TODO: Промисы для монги
data.contentType = req.route.path === '/account' ? 'user' : data.contentType;
data.contentType === 'user' && db.getUser(req.user.id, function (err, user) {
data.user = user;
});

res.render('index', data, function(err, html) {
if (err) {
res.send(500, err);
} else {
res.send(html);
}
});
render();
}
};
44 changes: 44 additions & 0 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var configs = require('../configs/default'),
db = require('./db');

module.exports = {
index: function(req, res, next) {
var data = configs.defaults(req),
render = function() {
return res.render('index', data, function(err, html) {
if (err) {
res.send(500, err);
} else {
res.send(html);
}
});
};

data.contentType = 'user';

db.getUser(req.user.id, function (err, user) {
data.user = user;
render();
});
},

users: function(req, res, next) {
var data = configs.defaults(req),
render = function() {
return res.render('index', data, function(err, html) {
if (err) {
res.send(500, err);
} else {
res.send(html);
}
});
};

data.contentType = 'users';

db.getUsers(false, function (result) {
data.users = result;
render();
}, true);
}
};
44 changes: 24 additions & 20 deletions server/lib/db.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
var MongoClient = require('mongodb').MongoClient,
vow = require('vow'),
var Promise = require("bluebird"),
MongoClient = Promise.promisifyAll(require('mongodb').MongoClient),

db_users, db_goods;

MongoClient.connect('mongodb://localhost:27017/goods-shelf', function(err, db) {
if (err) {
MongoClient.connectAsync('mongodb://localhost:27017/goods-shelf')
.then(function(db) {
console.log("Connected to DB");

db_users = db.collection('users');
db_goods = db.collection('goods');
})
.catch(function(err) {
console.log("I can't connect to DB. Is it running?");
throw err;
}

console.log("Connected to DB");

db_users = db.collection('users');
db_goods = db.collection('goods');
});
});

module.exports = {
upsertUser: function(id, profile, clb) {
db_users.update({ id: id }, { $set: { profile: profile }}, { upsert: true }, clb);
upsertUser: function(id, profile, cb) {
db_users.update({ id: id }, { $set: { profile: profile }}, { upsert: true }, cb);
},

getUser: function(id, clb) {
db_users.findOne({ id: id }, clb);
getUser: function(id, cb) {
db_users.findOne({ id: id }, cb);
},

addUser: function(name)
{
addUser: function(name) {
db_users.insert({ name: name }, function(err, records) { console.log(records); });
},

getUsers: function(clb)
{
db_users.find({}, clb);
getUsers: function(cb) {
db_users.find({}, cb);
},

getUserGoods: function(id, cb) {
var params = id !== undefined ? { "uid": id } : {};

db_goods.find(params, cb);
}
};
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"express": ">= 4.12.3",
"mongodb": "~2.0.31",
"vow": "~0.4.9",
"bluebird": "~2.9.27",
"passport": "~0.2.2",
"passport-yandex": "~0.0.3",
"express-session": "~1.11.2",
Expand Down
15 changes: 5 additions & 10 deletions server/router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var index = require('./controllers/index'),
user = require('./controllers/user'),
good = require('./controllers/good'),

db = require('./controllers/db'),
auth = require('./controllers/auth'),
Expand All @@ -7,22 +9,15 @@ var index = require('./controllers/index'),

module.exports = function (app) {
app.get('/', index.index);
app.get('/goods', index.index);
app.get('/account', ensureAuthanticated, index.index);
app.get('/users', ensureAuthanticated, index.index);
app.get('/goods', good.index);
app.get('/account', ensureAuthanticated, user.index);
app.get('/users', ensureAuthanticated, user.users);

// авторизация
app.get('/login', auth.passportLogin);
app.get('/login/callback', auth.passportLoginCb);
app.get('/logout', auth.passportLogout);


//some mongodb routes
//app.get('/get/', db.getGoods);
//app.get('/post/', db.postGood);
//app.get('/user/add', db.addUser);
//app.get('/users/', db.getUsers);

app.get('*', function (req, res) {
res.sendStatus(404);
});
Expand Down

0 comments on commit 5811e02

Please sign in to comment.