web: get all thoughts, user page
nau idoctnef@openmailbox.org
Sun, 17 Jul 2016 20:44:45 +0200
57 files changed,
283 insertions(+),
5 deletions(-)
jump to
M
controllers/thoughtController.js
→
controllers/thoughtController.js
@@ -2,13 +2,53 @@ //File: controllers/tvshows.js
var mongoose = require('mongoose'); var thoughtModel = mongoose.model('thoughtModel'); +var userModel = mongoose.model('userModel'); + //GET - Return all tvshows in the DB exports.findAllThoughts = function(req, res) { + thoughtModel.find(function(err, thoughts) { - if(err) res.send(500, err.message); + if(err) res.send(500, err.message); + + console.log(thoughts.length); + thoughts=JSON.parse(JSON.stringify(thoughts)); + + thoughts.forEach(function(thought, index, array){ + +array=JSON.parse(JSON.stringify(array)); + + userModel.find({ + username: thought.authorname + }, function(err, user) { + + if (err) throw err; + + if (!user) { - console.log('GET /thoughts'); - res.status(200).jsonp(thoughts); + } else if (user) { + + /*console.log(thought); + console.log(user);*/ + array[index].usericon=user[0].icon; + + console.log("icon: " + array[index].usericon); + //thought.set('icon', user.icon); + //console.log(thought); + + + } + + if(index === array.length-1) + { + console.log('GET /thoughts'); + console.log(array); + res.status(200).jsonp(array); + } + }); + + + }); + }); };
M
controllers/userController.js
→
controllers/userController.js
@@ -22,7 +22,7 @@ };
//GET - Return a TVShow with specified ID exports.findById = function(req, res) { - ActivityModel.findById(req.params.id, function(err, user) { + userModel.findById(req.params.id, function(err, user) { if(err) return res.send(500, err.message); console.log('GET /users/' + req.params.id);@@ -30,6 +30,25 @@ res.status(200).jsonp(user);
}); }; +exports.findUserByUsername = function(req, res) { + userModel.find({ + username: req.params.username + }, function(err, user) { + + if (err) throw err; + + if (!user) { + res.json({ success: false, message: 'no user found' }); + } else if (user) { + console.log(user); + // return the information including token as JSON + res.jsonp(user); + + + } + + }); +}; //POST - Insert a new TVShow in the DB exports.addUser = function(req, res) {
M
server.js
→
server.js
@@ -46,6 +46,10 @@
apiRoutes.route('/users') .get(userCtrl.findAllUsers) .post(userCtrl.addUser); +apiRoutes.route('/users/:id') + .get(userCtrl.findById); +apiRoutes.route('/users/byusername/:username') + .get(userCtrl.findUserByUsername); apiRoutes.route('/thoughts/user/:userid') .get(thoughtCtrl.findAllThoughtsFromUsername);@@ -91,7 +95,6 @@ }
}); apiRoutes.route('/users/:id') - .get(userCtrl.findById) .put(userCtrl.updateActivity) .delete(userCtrl.deleteActivity);
A
web/index.html
@@ -0,0 +1,64 @@
+<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> + <title>thoughts - main</title> + + <!-- Bootstrap --> + <link href="css/bootstrap.min.css" rel="stylesheet"> + + + + <link href="own.css" rel="stylesheet"> + + <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> + <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> + <!--[if lt IE 9]> + <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> + <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> + <![endif]--> + </head> + <body onload='OnLoadIndex()'> + + + <nav class="navbar navbar-default"> + <div class="container-fluid"> + <div class="navbar-header"> + <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> + <span class="icon-bar"></span> + <span class="icon-bar"></span> + <span class="icon-bar"></span> + </button> + <a class="navbar-brand" href="index.html">Thoughts</a> + </div> + <div class="collapse navbar-collapse" id="myNavbar"> + <ul class="nav navbar-nav"> + <li class="active"><a href="index.html">Home</a></li> + <li><a href="crearActivity.html">Users</a></li> + <li><a href="editActivities.html">Contact</a></li> + </ul> + <ul class="nav navbar-nav navbar-right"> + <li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li> + <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li> + </ul> + </div> + </div> + </nav> + + + <div class='container'> + <div id='listThoughtsHtml'></div> + + </div> + + <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> + <!-- Include all compiled plugins (below), or include individual files as needed --> + <script src="js/bootstrap.min.js"></script> + + <script src="index.js"></script> + </body> +</html>
A
web/index.js
@@ -0,0 +1,89 @@
+var listThoughts; + + +var urlapi = "http://localhost:3000/api"; + +function OnLoadIndex(){ + listThoughts=getAllThoughts(); +} +function getAllThoughts(){ + $.ajax({ + type: "GET", + dataType: "json", + url: urlapi + "/thoughts", + success: function(data){ + listThoughts=data; + + document.getElementById('listThoughtsHtml').innerHTML=generateHtmlListThoughts(); + } + }); +} +function generateHtmlListThoughts(){ + var html=""; + html+="<div class='list'>"; + html+="</div>"; + html+="<ul class='list-group'>"; + for(var i=0; i<listThoughts.length; i++) + { + html+=" <li class='list-group-item'>"; + html+="<img src='"+listThoughts[i].usericon+"' width='30px' />"; + html+="<a href='/userpage.html?value="+listThoughts[i].authorname+"'>" + listThoughts[i].authorname + ":</a>"; + html+="<br>"; + html+= listThoughts[i].content; + html+="</li>"; + } + + html+="</ul>"; + + + return(html); +} + + + + + + + + + + + + + + + + + + +/* +############################## +userpage.html +############################## +*/ + +function OnLoadUserPage(){ + var username=window.location.href.split("?value=")[1]; + $.ajax({ + type: "GET", + dataType: "json", + url: urlapi + "/users/byusername/" + username, + success: function(data){ + + document.getElementById('userpagehtml').innerHTML=generateHtmlUserPage(data[0]); + } + }); +} +function generateHtmlUserPage(user){ + var html=""; + html+="<div class='well'>"; + html+="<h3>" + user.username + "</h3>"; + html+="description: " + user.description; + + html+="<br>mail: " + user.mail; + html+=""; + html+=""; + html+=""; + html+="</div>"; + return(html); +}
A
web/userpage.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> + <title>thoughts - main</title> + + <!-- Bootstrap --> + <link href="css/bootstrap.min.css" rel="stylesheet"> + + + + <link href="own.css" rel="stylesheet"> + + <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> + <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> + <!--[if lt IE 9]> + <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> + <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> + <![endif]--> + </head> + <body onload='OnLoadUserPage()'> + + <nav class="navbar navbar-default"> + <div class="container-fluid"> + <div class="navbar-header"> + <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> + <span class="icon-bar"></span> + <span class="icon-bar"></span> + <span class="icon-bar"></span> + </button> + <a class="navbar-brand" href="index.html">Thoughts</a> + </div> + <div class="collapse navbar-collapse" id="myNavbar"> + <ul class="nav navbar-nav"> + <li><a href="index.html">Home</a></li> + <li><a href="crearActivity.html">Users</a></li> + <li><a href="editActivities.html">Contact</a></li> + </ul> + <ul class="nav navbar-nav navbar-right"> + <li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li> + <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li> + </ul> + </div> + </div> + </nav> + + <div class='container'> + <div id='userpagehtml'></div> + + </div> + + <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> + <!-- Include all compiled plugins (below), or include individual files as needed --> + <script src="js/bootstrap.min.js"></script> + + <script src="index.js"></script> + </body> +</html>