Skip to content

Commit

Permalink
Merge pull request #157 from vinitkumar/fix/bugs
Browse files Browse the repository at this point in the history
fix some bugs in the app
  • Loading branch information
Vinit Kumar authored Oct 1, 2017
2 parents 57ee42a + 5a89bdb commit 701bc76
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 52 deletions.
79 changes: 41 additions & 38 deletions app/controllers/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,8 @@ const Tweet = mongoose.model("Tweet");
const qs = require('querystring')
const url = require('url')

exports.index = (req, res) => {
const page = (req.param("page") > 0 ? req.param("page") : 1) - 1;
const perPage = 10;
const options = {
perPage: perPage,
page: page
};

let analytics, pageViews, tweetCount, pagination;

Analytics.list(options)
.then(result => {
analytics = result;
return Analytics.count();
})
.then(result => {
pageViews = result;
pagination = createPagination(req, Math.ceil(pageViews / perPage), page+1)
return Tweet.countTotalTweets()
})
.then(result => {
tweetCount = result;
res.render("analytics/analytics", {
title: "List of users",
analytics: analytics,
pageViews: pageViews,
tweetCount: tweetCount,
pagination: pagination,
pages: Math.ceil(pageViews / perPage)
});
})
.catch(error => {
console.log(error);
return res.render("500");
});
};

function createPagination (req, pages, page) {
exports.createPagination = (req, pages, page) => {
let params = qs.parse(url.parse(req.url).query);
let str = '';
let pageNumberClass;
Expand All @@ -67,7 +31,7 @@ function createPagination (req, pages, page) {
if (page > 2) {
str += '<li class="no page-item"><a class="page-link" href="?page=1">1</a></li>';
if (page > 3) {
str += '<li class="out-of-range">...</li>';
str += '<li class="out-of-range">...</li>';
}
}
// Determine how many pages to show after the current page index
Expand Down Expand Up @@ -112,3 +76,42 @@ function createPagination (req, pages, page) {
// Return the pagination string to be outputted in the pug templates
return str;
}


exports.index = (req, res) => {
let createPagination = exports.createPagination;
const page = (req.param("page") > 0 ? req.param("page") : 1) - 1;
const perPage = 10;
const options = {
perPage: perPage,
page: page
};

let analytics, pageViews, tweetCount, pagination;

Analytics.list(options)
.then(result => {
analytics = result;
return Analytics.count();
})
.then(result => {
pageViews = result;
pagination = createPagination(req, Math.ceil(pageViews / perPage), page+1)
return Tweet.countTotalTweets()
})
.then(result => {
tweetCount = result;
res.render("analytics/analytics", {
title: "List of users",
analytics: analytics,
pageViews: pageViews,
tweetCount: tweetCount,
pagination: pagination,
pages: Math.ceil(pageViews / perPage),
});
})
.catch(error => {
console.log(error);
return res.render("500");
});
};
18 changes: 12 additions & 6 deletions app/controllers/tweets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ## Tweet Controller
const createPagination = require('./analytics').createPagination;
const mongoose = require("mongoose");
const Tweet = mongoose.model("Tweet");
const Analytics = mongoose.model("Analytics");
Expand Down Expand Up @@ -47,7 +48,7 @@ exports.create = (req, res) => {
tweet.user = req.user;
tweet.uploadAndSave({}, err => {
if (err) {
res.render("500");
res.render("500", {error: err});
} else {
res.redirect("/");
}
Expand All @@ -70,7 +71,7 @@ exports.update = (req, res) => {
tweet = _.extend(tweet, {"body": req.body.tweet});
tweet.uploadAndSave({}, (err) => {
if (err) {
return res.render("500");
return res.render("500", {error: err});
}
res.redirect("/");
});
Expand All @@ -91,20 +92,23 @@ exports.destroy = (req, res) => {
exports.index = (req, res) => {
logAnalytics(req);
const page = (req.param("page") > 0 ? req.param("page") : 1) - 1;
const perPage = 10;
const options = {
perPage: 10,
perPage: perPage,
page: page
};
let followingCount = req.user.following.length;
let followerCount = req.user.followers.length;
let tweets, tweetCount, analytics;
let tweets, tweetCount, pageViews, analytics;
Tweet.list(options)
.then(result => {
tweets = result;
return Tweet.countUserTweets(req.user._id)
return Tweet.countTotalTweets()
})
.then(result => {
tweetCount = result;
pageViews = result;
pagination = createPagination(req, Math.ceil(pageViews/ perPage), page+1);
return Analytics.list({ perPage: 15 })
})
.then(result => {
Expand All @@ -115,8 +119,10 @@ exports.index = (req, res) => {
analytics: analytics,
page: page + 1,
tweetCount: tweetCount,
pagination: pagination,
followerCount: followerCount,
followingCount: followingCount
followingCount: followingCount,
pages: Math.ceil(pageViews / perPage),
});
})
.catch(error => {
Expand Down
4 changes: 2 additions & 2 deletions app/models/tweets.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const setTags = tags => tags.split(",");

// Tweet Schema
const TweetSchema = new Schema({
body: { type: String, default: "", trim: true },
body: { type: String, default: "", trim: true, maxlength: 140},
user: { type: Schema.ObjectId, ref: "User" },
comments: [
{
body: { type: String, default: "" },
body: { type: String, default: "", maxlength: 140},
user: { type: Schema.ObjectId, ref: "User" },
commenterName: { type: String, default: "" },
createdAt: { type: Date, default: Date.now }
Expand Down
2 changes: 1 addition & 1 deletion app/views/500.pug
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ block main
block content
#error-message-box
#error-stack-trace
pre
pre.pre-scrollable
code!=error


6 changes: 6 additions & 0 deletions app/views/tweets/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ block content
include ../analytics/recent-visits
.col-xl-6.col-lg-8.second-column
include tweets
- if (pages > 1)
nav(aria-label='Page navigation example').pagination-container
ul.pagination
!= pagination


.col-xl-3.col-lg-4.third-column
.recent-visits.col-xs-6.col-md-12
include ../analytics/recent-visits
Expand Down
3 changes: 2 additions & 1 deletion app/views/tweets/tweets.pug
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ each tweet in tweets
.tweet(data-tweetId = tweet._id)
.row
.col-1
img(class='tweet-image', src=tweet.user.github.avatar_url)
- if (tweet.user.github !== undefined)
img(class='tweet-image', src=tweet.user.github.avatar_url)
.col-11.tweet-description
span.name-date-group
span.name
Expand Down
11 changes: 7 additions & 4 deletions app/views/users/profile.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ block content
.row#profile-page
.col-lg-4
.profile-summary
img(class="profile-image", src=user.github.avatar_url)
- if (user.github !== undefined)
img(class="profile-image", src=user.github.avatar_url)
div.user-info
span.profile-handle
a(href=user.github.html_url, target='_blank') @#{user.github.login}
- if (user.github !== undefined)
a(href=user.github.html_url, target='_blank') @#{user.github.login}
ul
li Github followers: #{user.github.followers}
li Public repositories: #{user.github.public_repos}
- if (user.github !== undefined)
li Github followers: #{user.github.followers}
li Public repositories: #{user.github.public_repos}
.row
.col-12.user-stats
ul
Expand Down
18 changes: 18 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,21 @@ footer{

/*Extra large devices (large desktops)*/
/*No media query since the extra-large breakpoint has no upper bound on its width*/

pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}

pre code {
color: black;
}

#error-stack-trace {
background: yellow;
padding: 20px;
margin-top: 20px;
}

0 comments on commit 701bc76

Please sign in to comment.