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
8 changes: 6 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var express = require('express');
var bodyParser = require('body-parser');
GLOBAL._ = require('underscore');
var fs= require('fs')
var fs = require('fs')
GLOBAL.sha1 = require('js-sha1');
var app = express();
GLOBAL.DB = {
save:function(){
Expand Down Expand Up @@ -29,15 +30,18 @@ app.use(function (req, res, next) {
}
var parts = req.headers['authorization'].split(":")
var nick = parts[0];
var pwd = parts[1];
var pwd = sha1(parts[1]);
//console.log(nick + pwd)
var user = _.find(DB.users, function (usr) {
return usr.nick == nick && pwd == usr.pwd;
})
//console.log(user)
if (!user) {
res.status(401).send({message: "invalid user or password"})
return;
}
req.currentUser =user;
//console.log(req.currentUser)
next(null);
})
require('./controllers/user')(app)
Expand Down
53 changes: 53 additions & 0 deletions controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,57 @@ module.exports=function(app){
res.send(post);
})

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

res.send(DB.posts);
})

app.get('/post/:id', function(req, res) {
var post = _.find(DB.posts,function(post){
return post.id ==req.params.id;
});
if(!post){
res.status(404).send({message:"not found"})
return;
}
res.send(post)
})

app.put('/posts/:id', function(req, res) {
var post = _.where( DB.posts,{id:req.params.id});

if(!post) {
res.status(404).send({message:"not found"})
return;
}

if(req.currentUser.id == post.authorId) {
post.content = req.body.content
res.send(DB.posts[req.params.id])
return;
}
res.status(400).send('You can\'t edit this post')
})

app.delete('/post/:id', function(req, res) {
var id = _.where( DB.posts, {id:req.params.id});

if(!id) {
res.send("There is no such post")
return;
}

if(!(req.currentUser.id == DB.posts[id].authorId || req.currentUser.id == DB.posts[id].ownerId)) {
res.status(400).send('You can\'t delete this post')
return;
}

DB.posts.splice(id, 1);
DB.save();
res.send(DB.posts)
})

}
91 changes: 87 additions & 4 deletions controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
var uniqueId = Date.now();
module.exports = function(app){
app.get('/me',function(req,res){
app.get('/me',function(req, res) {
if(!req.currentUser) {
res.status(404).send({message:"not found"})
return;
}

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.save();
res.send(req.currentUser)
})

app.get('/user', function (req, res) {
if(!DB.users || DB.users == []) {
res.status(400).send("There is no posts")
}
res.send(DB.users);
})
app.get('/user/:id',function(req,res){
app.get('/user/:id',function(req, res){
var user = _.clone(_.find(DB.users,function(usr){
return usr.id ==req.params.id;
}));
Expand All @@ -17,9 +34,66 @@ module.exports = function(app){
}
res.send(user)
})
app.get('/user/:id/wall',function(req,res){

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

app.post('/user/:id/follow', function(req, res) {
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.save();
res.send(req.currentUser.follow)
})

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

_.each(DB.users, 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) {
res.send(req.currentUser.follow)
})

app.delete('/user/:id/follow', function(req, res) {
if (!_.find(req.currentUser.follow, function(index, i, arr) {
return index == req.params.id
})) {
res.status(400).send('The is no such user in follows')
return;
}

var id = (function(array) {
for(var i = 0; i < array.length; i++) {
if(req.params.id == array[i]){
return i;
}
}
})(req.currentUser.follow);
console.log(id)
req.currentUser.follow.splice(id, 1);
DB.save();
res.send(req.currentUser.follow);
})

app.post('/register', function (req, res) {
//��������� �������� �� ��� � �����
if (!req.body.email) {
Expand All @@ -31,11 +105,20 @@ module.exports = function(app){
} else if (!req.body.pwd || !req.body.repeatPwd || req.body.pwd != req.body.repeatPwd) {
res.status(400).send({message: "Passwords do not match"})
return;
} else if(_.where(DB.users, {nick : req.body.nick}).length > 0) {
console.log(_.where(DB.users, {nick : req.body.nick}))
res.status(400).send({message: "There is such nick"})
return;
} else if(! (req.body.pwd.length > 6 && /[A-Z]/.test(req.body.pwd) && ! /^[a-zA-Z0-9- ]*$/.test(req.body.pwd)) ) {
res.status(400).send({message: "a password must be six characters including one uppercase letter, one " +
"special character and alphanumeric characters."})
return;
}

var user = {
email: req.body.email,
nick: req.body.nick,
pwd: req.body.pwd,
pwd: sha1(req.body.pwd),
id: ++uniqueId
};

Expand Down
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"}]}
19 changes: 17 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
{
"name": "application-name",
"name": "app",
"version": "0.0.1",
"dependencies": {
"body-parser": "^1.13.3",
"express": "^4.13.3",
"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",
"description": ""
}