Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1.1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

62 changes: 39 additions & 23 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@ var bodyParser = require('body-parser');
GLOBAL._ = require('underscore');
var fs= require('fs')
var app = express();
GLOBAL.DB = {
save:function(){
fs.writeFileSync('./db.json',JSON.stringify(this))
},
restore: function(){
GLOBAL.DB = _.extend(GLOBAL.DB,JSON.parse(fs.readFileSync('./db.json','utf-8')))
}
}
GLOBAL.DB.restore();
DB.users = DB.users || [];
DB.posts = DB.posts || [];
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/socialNetwork';


MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
GLOBAL.DB = db;
app.listen(127)
console.log(err)
console.log(db)
});

//app.use(express.static('public'))
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
});

app.use(bodyParser.json())

app.use(function (req, res, next) {
console.log(req.originalUrl)
if(req.originalUrl =='/register'){
Expand All @@ -30,18 +45,19 @@ app.use(function (req, res, next) {
var parts = req.headers['authorization'].split(":")
var nick = parts[0];
var pwd = parts[1];
var user = _.find(DB.users, function (usr) {
return usr.nick == nick && pwd == usr.pwd;
})
if (!user) {
DB.collection('users').find({nick:nick,pwd:pwd}).toArray(function(err, data){
console.log(data)
if (data.length>0) {
req.currentUser =data[0];
next(null);
return;
}
res.status(401).send({message: "invalid user or password"})
return;
}
req.currentUser =user;
next(null);
})
require('./controllers/user')(app)
require('./controllers/post')(app)

})


})

app.listen(100)
require('./controllers/user')(app)
require('./controllers/post')(app)
86 changes: 80 additions & 6 deletions controllers/post.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,92 @@

var ObjectId = require('mongodb').ObjectID
module.exports=function(app){

app.get('/post', function(req, res) {
DB.collection('posts').find({}).toArray(function (err, data) {
if(!data || data == []) {
res.status(400).send("There is no posts")
}
res.send(data);
})
})

app.post('/user/:id/wall',function(req,res){
if(!req.body.content){
res.status(400).send({message:'content required'})
return;
}
var post = {
content:req.body.content,
id:Date.now(),
authorId:req.currentUser.id,
ownerId:req.params.id
authorId:{$ref:"users",_id:req.currentUser._id},
ownerId:{$ref:"users",_id:req.params.id}
};
DB.posts.push(post)
DB.save();
res.send(post);
DB.collection('posts').insert(post,function(err,data){
res.send(data);
})
})

app.get('/user/:id/wall',function(req,res){
var posts = [];
DB.collection('posts').find({"ownerId._id": req.params.id}, function (err, post) {
posts.push(post)
})
res.send(posts)
})

app.get('/post/:id', function(req, res) {
DB.collection('posts').findOne({_id: new ObjectId(req.params.id)},
function (err, post) {
if (!post) {
res.status(404).send({message: "not found"})
return;
}
res.send(post);
})
})

app.put('/post/:id', function(req, res) { //�� ����������� ���������
DB.collection('posts').findOne({_id: new ObjectId(req.params.id)},
function (err, post) {
if (!post) {
res.status(404).send({message: "not found"})
return;
}

if(post.authorId._id.toString() == req.currentUser._id.toString()) {
post.content = req.body.content;

DB.collection('posts').update({_id: new ObjectId(req.params.id)}, {$set:{"content": post.content}}
, function() {
res.send(post);
})
return
} else {
res.status(404).send({message: "You can't modify post"})
}
})
})

app.delete('/post/:id', function(req, res) {

DB.collection('posts').findOne({_id: new ObjectId(req.params.id)},
function (err, post) {
if (!post) {
res.status(404).send({message: "not found"})
return;
}

if(post.ownerId._id.toString() != req.currentUser._id && post.authorId._id.toString() != req.currentUser._id) {
res.status(403).send({message: "You can't delete post"})
return
}
})


DB.collection('posts').remove({_id: new ObjectId(req.params.id)}, function(err, data) {
res.send(data)
})

})

}
158 changes: 137 additions & 21 deletions controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,143 @@
var uniqueId = Date.now();
module.exports = function(app){
app.get('/me',function(req,res){

var ObjectId = require('mongodb').ObjectID
var async = require('async')
module.exports = function (app) {

app.get('/me', function (req, res) {
res.send(req.currentUser);
})

app.put('/me', function(req, res){
if(req.currentUser.nick != req.body.nick && req.body.nick) req.currentUser.nick = req.body.nick
if(req.currentUser.pwd != req.body.pwd && req.body.pwd) req.currentUser.pwd = req.body.pwd
if(req.currentUser.email != req.body.email && req.body.email) req.currentUser.email = req.body.email
DB.collection('users').update({_id: req.currentUser._id}, req.currentUser)
res.send(req.currentUser)
})

app.get('/user', function (req, res) {
res.send(DB.users);
})
app.get('/user/:id',function(req,res){
var user = _.clone(_.find(DB.users,function(usr){
return usr.id ==req.params.id;
}));
delete user.pwd;
if(!user){
res.status(404).send({message:"not found"})
console.log('!!!!!!!!!!!!!!!!!!!!!')
DB.collection('users').find({}).toArray(function (err, data) {
res.send(data.map(function (user) {
delete user.pwd;
return user
}));

})


})
app.get('/user/:id', function (req, res) {
DB.collection('users').findOne({_id: new ObjectId(req.params.id)},
function (err, user) {
if (!user) {
res.status(404).send({message: "not found"})
return;
}
delete user.pwd;
res.send(user);
})
})
app.get('/user/:id/wall', function (req, res) {
var UsersCollection = DB.collection('users')
DB.collection('posts')
.find({"ownerId._id": req.params.id})
.toArray(function (err, posts) {
async.mapLimit(posts, 5, function (post, next) {
UsersCollection.findOne({_id: new ObjectId(post.authorId._id)},
function (err, data) {
post.author = data;
UsersCollection.findOne({_id: new ObjectId(post.ownerId._id)},
function (err, data) {
post.owner = data;
next(null,post);
})
})
}, function (err,data) {
res.send(data);
})


})


})

app.post('/user/:id/follow', function(req, res) {

if(req.params.id == req.currentUser._id) {
res.status(400).send('You can\'t follow yourself')
return;
}
res.send(user)
if(_.find(req.currentUser.follow, function(followingId) {
return req.params.id == followingId;

})) {
res.status(400).send('There is such user in follows already OR you can\'t follow he/she')
return;
}

if(!req.currentUser.follow) req.currentUser.follow = []
req.currentUser.follow.push(req.params.id);

DB.collection('users').update({_id: req.currentUser._id}, req.currentUser)
res.send(req.currentUser.follow)
})

app.delete('/user/:id/follow', function(req, res) {
DB.collection('users').findOne({_id: req.currentUser._id}, function(err, data){
var id;
data.follow.forEach(function(item, i, arr) {
if(item == req.params.id) {
id = i
return
}
})
if(id != undefined) {
data.follow.splice(id, 1)
DB.collection('users').update({_id: req.currentUser._id}, data)
res.send(req.currentUser)
} else {
res.status(400).send('No such user')
return
}

})
})
app.get('/user/:id/wall',function(req,res){
res.send(_.where( DB.posts,{ownerId:req.params.id}));

app.get('/user/:id/followers', function(req, res) {
var followers = [];

DB.collection('users').find({}).toArray(function (err, data) {
data.forEach(function(index, i, arr) {
_.each(index.follow, function(item, j, array) {
if(item == req.params.id) followers.push(index._id);
})
})
if(followers == []) {
res.status(400).send('The is no followers')
return
}
res.send(followers)
})

})

app.get('/user/:id/following', function(req, res) {
DB.collection('users').findOne({_id: new ObjectId(req.params.id)},
function (err, user) {
if (!user) {
res.status(404).send({message: "not found"})
return;
}
res.send(user.follow);
})
})


app.post('/register', function (req, res) {
//��������� �������� �� ��� � �����
console.log(req)
if (!req.body.email) {
res.status(400).send({message: "Email is required"})
return;
Expand All @@ -35,13 +151,13 @@ module.exports = function(app){
var user = {
email: req.body.email,
nick: req.body.nick,
pwd: req.body.pwd,
id: ++uniqueId
pwd: req.body.pwd
};

DB.users.push(_.clone(user))
DB.save();
delete user.pwd;
res.send(user)
DB.collection('users').insert(user, function (err, data) {
delete user.pwd;
res.send(user)
})

})
}
2 changes: 1 addition & 1 deletion db.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"users":[{"email":"gg@gg.gg","nick":"second","pwd":"123","id":1439224467621},{"email":"gg@gg.gg","nick":"second","pwd":"123","id":1439224467622},{"email":"gg@gg.gg","nick":"gena","pwd":"123","id":1439225345875}],"posts":[{"content":"Hellofrom gena","id":1439225509193,"authorId":1439225345875,"ownerId":"1439224467621"}]}
{"users":[{"nick":"nnn","pwd":"123","email":"mail@something","id":1439224467621,"follow":["1439224467621","1439224467622"]},{"email":"gg@gg.gg","nick":"somenick","pwd":"123","id":1439224467622},{"email":"gg@gg.gg","nick":"gena","pwd":"123","id":1439225345875,"follow":["1439224467622"]},{"email":"some email@.com","nick":"good nick","pwd":"5454","id":1439724325905},{"email":"some email@.com","nick":"good nick","pwd":"545454","id":1439724325906},{"email":"some email@.com","nick":"Anika","pwd":"545454","id":1439724369116},{"email":"some email@.com","nick":"London","pwd":"545454","id":1439724428510},{"email":"some email@.com","nick":"bOM","pwd":"545454@Add","id":1439724970887},{"email":"some email@.com","nick":"Soo","pwd":"545454Ad#d","id":1439726605541},{"email":"some email@.com","nick":"Sooso","pwd":"5d3e67b8f0f8da38365c9679050857d630cacccc","id":1439726775986}],"posts":[{"content":"some content","id":1439659529468,"authorId":1439224467621,"ownerId":"1439224467621"},{"content":"I want some sleep","id":1439659556786,"authorId":1439224467621,"ownerId":"1439224467621"},{"content":"I want some something","id":1439659566884,"authorId":1439224467621,"ownerId":"1439224467621"}]}
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
{
"name": "application-name",
"name": "app",
"version": "0.0.1",
"dependencies": {
"body-parser": "^1.13.3",
"express": "^4.13.3",
"mongodb": "^2.0.42",
"underscore": "^1.8.3"
}
},
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vizhukova/SocialNetworkServer.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/vizhukova/SocialNetworkServer/issues"
},
"homepage": "https://github.com/vizhukova/SocialNetworkServer#readme",
"devDependencies": {},
"description": ""
}
1 change: 1 addition & 0 deletions public/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bower_components
Loading