diff --git a/README.md b/README.md index 2f10b94..762e530 100644 --- a/README.md +++ b/README.md @@ -2,38 +2,38 @@ Отрефакторить /registration запрос: - сделать проверки - хэшировать пароль + сделать проверки OK + хэшировать пароль OK Сделать API для следующих запросов: GET : - /post - возвращает список всех записей + /post - возвращает список всех записей OK - /posts/:id - возвращает запись с заданным id (req.param.id) + /posts/:id - возвращает запись с заданным id (req.param.id) OK - /user/:id/following - возвращает список кумиров указанного юзера (кого этот юзер фолловит, отслеживает) + /user/:id/following - возвращает список кумиров указанного юзера (кого этот юзер фолловит, отслеживает) OK - /user/:id/folowers - возвращает список фанатов указанного юзера (кто отслеживает указанного юзера) + /user/:id/folowers - возвращает список фанатов указанного юзера (кто отслеживает указанного юзера) OK DELETE: - /posts/:id - удаляет запись с заданным id (только если currentUser.id == author.id || currentUser.id == owner.id) + /posts/:id - удаляет запись с заданным id (только если currentUser.id == author.id || currentUser.id == owner.id) OK - /user/:id/follow - отписаться (перестать отслеживать) от юзера с заданным id + /user/:id/follow - отписаться (перестать отслеживать) от юзера с заданным id OK PUT: - /me - редактировать данные о себе + /me - редактировать данные о себе OK - /posts/:id редактировать запись (только если currentUser.id == author.id) + /posts/:id редактировать запись (только если currentUser.id == author.id) OK POST: - /user/:id/follow - подписаться на юзера с заданным id + /user/:id/follow - подписаться на юзера с заданным id OK diff --git a/Social%20Network%20Project.json.postman_collection b/Social%20Network%20Project.json.postman_collection new file mode 100644 index 0000000..49cd1e5 --- /dev/null +++ b/Social%20Network%20Project.json.postman_collection @@ -0,0 +1,341 @@ +{ + "id": "5e033b16-8151-c70f-c605-aae95c988855", + "name": "Social Network Project", + "description": "", + "order": [ + "1a2ef65c-0abb-0221-f322-c53163f10dce", + "939d616a-0028-1bfd-34c1-78a1e9b1f13d", + "e21a8734-8e8c-ad5a-3fe3-b53b12ed21b2", + "c9a62701-3fe8-f06b-a18e-81289ba24f24", + "bda1c237-b1cd-a984-1fa2-123f19c63c19", + "e9caca8e-1d55-1040-8392-7934708a38a4", + "bef8ea8b-fe43-d350-f253-0bcc1c53e0e9", + "25e93113-5298-1ad0-7c19-301e03088aad", + "ee8239e4-7996-5261-0d21-c0d8f06c0b75", + "c6e0d4c7-0e38-a69a-b615-28dba23268b9", + "8ab80377-2d4c-3f9c-097e-99a4df147cdc", + "39354428-48ed-3696-8c84-87bce170a377", + "a8d5f618-7cc2-eb10-8332-4c2786daa29f", + "fbb2183d-5d75-2cf1-4f44-b395fdd43a7f", + "124203f4-6fde-c67a-c332-c2169f274227" + ], + "folders": [], + "timestamp": 1439307457010, + "owner": "", + "remoteLink": "", + "public": false, + "requests": [ + { + "id": "124203f4-6fde-c67a-c332-c2169f274227", + "headers": "authorization: vl:123456\nContent-Type: application/json\n", + "url": "127.0.0.10:100/posts/:id", + "preRequestScript": "", + "pathVariables": { + "id": "1439462753604" + }, + "method": "PUT", + "data": [], + "dataMode": "raw", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439468997772, + "name": "127.0.0.10:100/posts/:id", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [], + "rawModeData": "{\n \"content\": \"Hamsters. Full lifecycle from birth to death.\"\n}" + }, + { + "id": "1a2ef65c-0abb-0221-f322-c53163f10dce", + "headers": "Content-Type: application/json\n", + "url": "127.0.0.10:100/register", + "preRequestScript": "", + "pathVariables": {}, + "method": "POST", + "data": [], + "dataMode": "raw", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439371930549, + "name": "127.0.0.10:100/register", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "rawModeData": "{\n \"email\": \"am@dfg.com\",\n \"nick\": \"vl\",\n \"pwd\": \"123456\",\n \"repeatPwd\": \"123456\"\n}" + }, + { + "id": "25e93113-5298-1ad0-7c19-301e03088aad", + "headers": "authorization: vl:123456\n", + "url": "127.0.0.10:100/posts/:id", + "preRequestScript": "", + "pathVariables": { + "id": "1439225509193" + }, + "method": "GET", + "data": [], + "dataMode": "params", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439405728620, + "name": "127.0.0.10:100/posts/:id", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [] + }, + { + "id": "39354428-48ed-3696-8c84-87bce170a377", + "headers": "authorization: vl:123456\nContent-Type: application/json\n", + "url": "127.0.0.10:100/user/:id/followers", + "preRequestScript": "", + "pathVariables": { + "id": "1439225345875" + }, + "method": "GET", + "data": [], + "dataMode": "params", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439414341991, + "name": "127.0.0.10:100/user/:id/followers", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [] + }, + { + "id": "8ab80377-2d4c-3f9c-097e-99a4df147cdc", + "headers": "authorization: vl:123456\nContent-Type: application/json\n", + "url": "127.0.0.10:100/user/:id/following", + "preRequestScript": "", + "pathVariables": { + "id": "1439315897737" + }, + "method": "GET", + "data": [], + "dataMode": "params", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439457875184, + "name": "127.0.0.10:100/user/:id/following", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [] + }, + { + "id": "939d616a-0028-1bfd-34c1-78a1e9b1f13d", + "headers": "authorization: vl:123456\n", + "url": "127.0.0.10:100/user", + "preRequestScript": "", + "pathVariables": {}, + "method": "GET", + "data": [], + "dataMode": "params", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439318933936, + "name": "127.0.0.10:100/user", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [] + }, + { + "id": "a8d5f618-7cc2-eb10-8332-4c2786daa29f", + "headers": "authorization: vl:123456\n", + "url": "127.0.0.10:100/posts/:id", + "pathVariables": { + "id": "1439404599850" + }, + "preRequestScript": "", + "method": "DELETE", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "data": [], + "dataMode": "params", + "name": "127.0.0.10:100/posts/:id", + "description": "", + "descriptionFormat": "html", + "time": 1439461994243, + "version": 2, + "responses": [], + "tests": "", + "currentHelper": "normal", + "helperAttributes": {} + }, + { + "id": "bda1c237-b1cd-a984-1fa2-123f19c63c19", + "headers": "authorization: vl:123456\n", + "url": "127.0.0.10:100/user/:id/wall", + "preRequestScript": "", + "pathVariables": { + "id": "1439225345875" + }, + "method": "GET", + "data": [], + "dataMode": "params", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439413547495, + "name": "127.0.0.10:100/user/:id/wall", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [] + }, + { + "id": "bef8ea8b-fe43-d350-f253-0bcc1c53e0e9", + "headers": "authorization: vl:123456\n", + "url": "127.0.0.10:100/post", + "pathVariables": {}, + "preRequestScript": "", + "method": "GET", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "data": [], + "dataMode": "params", + "name": "127.0.0.10:100/post", + "description": "", + "descriptionFormat": "html", + "time": 1439405042038, + "version": 2, + "responses": [], + "tests": "", + "currentHelper": "normal", + "helperAttributes": {} + }, + { + "id": "c6e0d4c7-0e38-a69a-b615-28dba23268b9", + "headers": "authorization: vl:123456\nContent-Type: application/json\n", + "url": "127.0.0.10:100/user/:id/follow", + "preRequestScript": "", + "pathVariables": { + "id": "1439225345875" + }, + "method": "DELETE", + "data": [], + "dataMode": "raw", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439413508410, + "name": "127.0.0.10:100/user/:id/follow", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [], + "rawModeData": "" + }, + { + "id": "c9a62701-3fe8-f06b-a18e-81289ba24f24", + "headers": "authorization: vl:123456\n", + "url": "127.0.0.10:100/user/:id", + "preRequestScript": "", + "pathVariables": { + "id": "1439222467622" + }, + "method": "GET", + "data": [], + "dataMode": "params", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439413561093, + "name": "127.0.0.10:100/user/:id", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [] + }, + { + "id": "e21a8734-8e8c-ad5a-3fe3-b53b12ed21b2", + "headers": "authorization: vl:333\n", + "url": "127.0.0.10:100/me", + "preRequestScript": "", + "pathVariables": {}, + "method": "GET", + "data": [], + "dataMode": "params", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439467598695, + "name": "127.0.0.10:100/me", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [] + }, + { + "id": "e9caca8e-1d55-1040-8392-7934708a38a4", + "headers": "authorization: vl:123456\nContent-Type: application/json\n", + "url": "127.0.0.10:100/user/:id/wall", + "preRequestScript": "", + "pathVariables": { + "id": "1439315897737" + }, + "method": "POST", + "data": [], + "dataMode": "raw", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439462636334, + "name": "127.0.0.10:100/user/:id/wall", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [], + "rawModeData": "{" + }, + { + "id": "ee8239e4-7996-5261-0d21-c0d8f06c0b75", + "headers": "authorization: vl:123456\nContent-Type: application/json\n", + "url": "127.0.0.10:100/user/:id/follow", + "preRequestScript": "", + "pathVariables": { + "id": "1439225345875" + }, + "method": "POST", + "data": [], + "dataMode": "raw", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439408388243, + "name": "127.0.0.10:100/user/:id/follow", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [], + "rawModeData": "" + }, + { + "id": "fbb2183d-5d75-2cf1-4f44-b395fdd43a7f", + "headers": "authorization: vl:123456\nContent-Type: application/json\n", + "url": "127.0.0.10:100/me", + "preRequestScript": "", + "pathVariables": {}, + "method": "PUT", + "data": [], + "dataMode": "raw", + "version": 2, + "tests": "", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1439468025878, + "name": "127.0.0.10:100/me", + "description": "", + "collectionId": "5e033b16-8151-c70f-c605-aae95c988855", + "responses": [], + "rawModeData": "{\n \"pwd\":\"123456\"\n}" + } + ] +} \ No newline at end of file diff --git a/app.js b/app.js index 5d9c063..36db20c 100644 --- a/app.js +++ b/app.js @@ -1,55 +1,61 @@ var express = require('express'); var bodyParser = require('body-parser'); GLOBAL._ = require('underscore'); -var fs= require('fs') var app = express(); -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(80) -}); -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'); +var MongoClient = require('mongodb').MongoClient; +GLOBAL.ObjectID = require('mongodb').ObjectID; +var url = 'mongodb://127.0.0.10:27017/socialNetwork'; - // intercept OPTIONS method - if ('OPTIONS' == req.method) { - res.send(200); - } - else { - next(); - } +MongoClient.connect(url, function (err, db) { + GLOBAL.DB = db; + GLOBAL.UsersCollection = DB.collection('users'); + GLOBAL.PostsCollection = DB.collection('posts'); + GLOBAL.FollowCollection = DB.collection('follow'); + + app.listen(100, '127.0.0.10'); + console.log("Connected to server correctly"); +}) + +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'){ + + if (req.originalUrl == '/register') { next(null); return; } if (!req.headers['authorization']) { - res.status(401).send({message: "No auth"}); + res.status(401).send({ message: "Authorization is required" }); return; } var parts = req.headers['authorization'].split(":") var nick = parts[0]; - var pwd = parts[1]; - DB.collection('users').find({nick:nick,pwd:pwd}).toArray(function(err,data){ - - if (data.length>0) { - req.currentUser =data[0]; - next(null); - return; - } - res.status(401).send({message: "invalid user or password"}) - - }) - + var pwd = require('crypto').createHash('md5').update(parts[1]).digest('hex'); + UsersCollection.find({ nick: nick, pwd: pwd }).toArray(function (err, data) { + if (data.length==0) { + res.status(401).send({ message: "Invalid user or password" }); + return; + } + req.currentUser = data[0]; + next(null); + }); }) require('./controllers/user')(app) require('./controllers/post')(app) +require('./controllers/follow')(app) diff --git a/controllers/follow.js b/controllers/follow.js new file mode 100644 index 0000000..f627490 --- /dev/null +++ b/controllers/follow.js @@ -0,0 +1,91 @@ + +module.exports = function (app) { + + app.post('/user/:id/follow', function (req, res) { + UsersCollection.findOne({ _id: new ObjectID(req.params.id) }, + function (err, user) { + if (!user) { + res.status(404).send({ message: "Invalid user ID" }); + return; + } + var follow = { + follower: req.currentUser._id.toString(), + following: user._id.toString() + }; + FollowCollection.findOne(follow, function (err, flw) { + if (flw) { + res.status(403).send({ message: "You are following this user already" }); + return; + } + FollowCollection.insert(follow, function (err, data) { + res.send(data); + }) + }) + }); + }) + + app.get('/user/:id/following', function (req, res) { + UsersCollection.findOne({ _id: new ObjectID(req.params.id) }, + function (err, user) { + if (!user) { + res.status(404).send({ message: "Invalid user ID" }); + return; + } + FollowCollection.find({ follower: req.params.id.toString() }).toArray(function (err, flw) { + if (flw.length === 0) { + res.status(422).send({ message: "This user does not follow anybody" }); + return; + } + var result = flw.map(function(item){ + item = item.following; + return item; + }) + res.send(result); + }) + }); + }) + + app.get('/user/:id/followers', function (req, res) { + UsersCollection.findOne({ _id: new ObjectID(req.params.id) }, + function (err, user) { + if (!user) { + res.status(404).send({ message: "Invalid user ID" }); + return; + } + FollowCollection.find({ following: req.params.id.toString() }).toArray(function (err, flw) { + if (flw.length === 0) { + res.status(422).send({ message: "Nobody is following this user" }); + return; + } + var result = flw.map(function (item) { + item = item.follower; + return item; + }) + res.send(result); + }) + }); + }) + + app.delete('/user/:id/follow', function (req, res) { + UsersCollection.findOne({ _id: new ObjectID(req.params.id) }, + function (err, user) { + if (!user) { + res.status(404).send({ message: "Invalid user ID" }); + return; + } + var follow = { + follower: req.currentUser._id.toString(), + following: user._id.toString() + }; + FollowCollection.findOne(follow, function (err, flw) { + if (!flw) { + res.status(404).send({ message: "You are not following this user" }); + return; + } + FollowCollection.remove(flw, function (err, data) { + res.send(data); + }) + }) + }); + }) +} \ No newline at end of file diff --git a/controllers/post.js b/controllers/post.js index 6a7fd74..a9bf10d 100644 --- a/controllers/post.js +++ b/controllers/post.js @@ -1,21 +1,91 @@ var ObjectId = require('mongodb').ObjectID -module.exports=function(app){ - app.post('/user/:id/wall',function(req,res){ +module.exports = function (app) { + + app.get('/user/:id/wall', function (req, res) { + PostsCollection.find({ ownerId: req.params.id.toString() }).toArray(function (err, posts) { + if (posts.length == 0) { + res.status(404).send({ message: "Not found" }); + return + } + res.send(posts); + }); + }) + + app.post('/user/:id/wall', function (req, res) { if(!req.body.content){ - res.status(400).send({message:'content required'}) + res.status(400).send({message:'Content required'}) return; } var post = { - content:req.body.content, - authorId:{$ref:"users",_id:req.currentUser._id}, - ownerId:{$ref:"users",_id:req.params.id} + content: req.body.content, + authorId: req.currentUser._id.toString(), //{ $ref: "users", _id: new ObjectID(req.currentUser.id)}, + ownerId: req.params.id.toString() //{ $ref: "users", _id: new ObjectID(req.params.id)} }; - DB.collection('posts').insert(post,function(err,data){ - res.send(data); - }) + PostsCollection.insert(post, function (err, data) { + res.send(data); + }) + }) + app.get('/post', function (req, res) { + PostsCollection.find({}).toArray(function (err, posts) { + if (posts.length == 0) { + res.status(404).send({ message: "Not found" }); + return; + } + res.send(posts); + }); + }) - }) + app.get('/posts/:id', function (req, res) { + PostsCollection.find({ _id: new ObjectID(req.params.id) }).toArray(function (err, posts) { + if (posts.length == 0) { + res.status(404).send({ message: "Not found" }); + return + } + res.send(posts[0]); + }); + }) + + app.put('/posts/:id', function (req, res) { + PostsCollection.find({ _id: new ObjectID(req.params.id) }).toArray(function (err, posts) { + if (posts.length == 0) { + res.status(404).send({ message: "Post not found" }); + return; + } + if (posts[0].authorId != req.currentUser._id.toString()) { + res.status(403).send({ message: "Privilege violation" }); + return; + } + if (req.body.id || req.body.authorId || req.body.ownerId) { + res.status(403).send({ message: "You can change content only" }); + return; + } + if (!req.body.content) { + res.status(404).send({ message: "New content not found" }); + return; + } + posts[0].content = req.body.content; + PostsCollection.update({ _id: new ObjectID(req.params.id) }, posts[0], function (err, answer) { + res.send(answer); + }); + }); + }) + app.delete('/posts/:id', function (req, res) { + var currentUserID = req.currentUser._id.toString(); + PostsCollection.find({ _id: new ObjectID(req.params.id) }).toArray(function (err, posts) { + if (posts.length == 0) { + res.status(404).send({ message: "Post not found" }); + return; + } + if (posts[0].authorId != currentUserID && posts[0].ownerId != currentUserID) { + res.status(403).send({ message: "Privilege violation" }); + return; + } + PostsCollection.remove({ _id: new ObjectID(req.params.id) }, function (err, answer) { + res.send(answer); + }) + }); + }) } \ No newline at end of file diff --git a/controllers/user.js b/controllers/user.js index 634e44c..ffb1367 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -1,80 +1,88 @@ var ObjectId = require('mongodb').ObjectID -var async = require('async') -module.exports = function (app) { - app.get('/me', function (req, res) { - res.send(req.currentUser); - }) - app.get('/user', function (req, res) { - 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); - }) +module.exports = function(app){ + app.post('/register', function (req, res) { + if (!req.body.email) { + res.status(400).send({ message: "Email is required" }) + return; + } else if (!req.body.nick) { + res.status(400).send({ message: "Nick is required" }) + return; + } else if (!req.body.pwd || !req.body.repeatPwd || req.body.pwd != req.body.repeatPwd) { + res.status(400).send({ message: "Passwords do not match" }) + return; + } + UsersCollection.find({}).toArray(function (err, users) { + var emailExists = false; + var nickExists = false; + _.some(users, function (user) { + emailExists = (user.email == req.body.email); + nickExists = (user.nick == req.body.nick); + return emailExists || nickExists; + }); - }) + if (emailExists) { + res.status(400).send({ message: "User with this email already exists" }); + } else if (nickExists) { + res.status(400).send({ message: "User with this nick already exists" }); + } else { + var user = { + email: req.body.email, + nick: req.body.nick, + pwd: require('crypto').createHash('md5').update(req.body.pwd).digest('hex'), + }; + UsersCollection.insert(user, function (err, user) { + delete user.pwd; + res.send(user); + }) + } + }); + }) + app.put('/me', function (req, res) { + if (req.body.nick || req.body.id) { + res.status(403).send({ message: "You cannot change your Nick and/or ID" }); + return; + } + req.currentUser = { + email: req.body.email || req.currentUser.email, + nick: req.currentUser.nick, + pwd: req.body.pwd ? require('crypto').createHash('md5').update(req.body.pwd).digest('hex') : req.currentUser.pwd + }; + UsersCollection.update({ nick: req.currentUser.nick }, req.currentUser, function (err, user) { + UsersCollection.find({ nick: req.currentUser.nick }).toArray(function (err, data) { + if (data.length == 0) { + res.status(401).send({ message: "Invalid user" }); + return; + } + res.send(data[0]); + }); + }); + }) + app.get('/me', function (req, res) { + res.send(req.currentUser); }) + app.get('/user', function (req, res) { + UsersCollection.find({}).toArray(function (err, users) { + res.send(users.map(function (user) { + delete user.pwd; + return user; + })); + }); + }) - app.post('/register', function (req, res) { - //проверить свободен ли ник и имейл - if (!req.body.email) { - res.status(400).send({message: "Email is required"}) - return; - } else if (!req.body.nick) { - res.status(400).send({message: "Nick is required"}) - return; - } else if (!req.body.pwd || !req.body.repeatPwd || req.body.pwd != req.body.repeatPwd) { - res.status(400).send({message: "Passwords do not match"}) - return; - } - var user = { - email: req.body.email, - nick: req.body.nick, - pwd: req.body.pwd - }; - - DB.collection('users').insert(user, function (err, data) { - delete user.pwd; - res.send(user) - }) - + app.get('/user/:id', function (req, res) { + UsersCollection.findOne({ _id: new ObjectID(req.params.id) }, + function (err, user) { + if (!user) { + res.status(404).send({ message: "Invalid user ID" }); + return; + } + delete user.pwd; + res.send(user); + }); }) } \ No newline at end of file diff --git a/db.json b/db.json index d7084f4..42fdb44 100644 --- a/db.json +++ b/db.json @@ -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"}]} \ No newline at end of file +{"users":[{"email":"gg@gg.gg","nick":"gena","pwd":"123","id":1439225345875},{"email":"am@dfg.com","nick":"vl","pwd":"123456","id":1439315897737}],"posts":[{"content":"Hellofrom gena","id":1439225509193,"authorId":1439225345875,"ownerId":"1439224467621"},{"content":"Hamsters. Full lifecycle from birth to death.","id":1439462753604,"authorId":1439315897737,"ownerId":1439315897737}],"follow":[]} \ No newline at end of file diff --git a/package.json b/package.json index b14a7c0..7f24d34 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,33 @@ { - "name": "application-name", + "name": "app.js", "version": "0.0.1", "dependencies": { "async": "^1.4.2", "body-parser": "^1.13.3", + "crypto": "0.0.3", "express": "^4.13.3", "mongodb": "^2.0.41", "underscore": "^1.8.3" - } + }, + "description": "Отрефакторить /registration запрос:", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/potravniy/SocialNetworkServer.git" + }, + "keywords": [ + "Social", + "Network", + "Learning", + "Project" + ], + "author": "Gena Moroz", + "license": "ISC", + "bugs": { + "url": "https://github.com/potravniy/SocialNetworkServer/issues" + }, + "homepage": "https://github.com/potravniy/SocialNetworkServer#readme" }