From 41442a540fb937b3b53e9f05df89eead82844f30 Mon Sep 17 00:00:00 2001 From: Nathan Leniz Date: Thu, 5 Mar 2015 19:21:26 +0900 Subject: [PATCH 1/3] Need to work on story redirect after submission, fix url link in stories/show --- app.js | 5 ++++ controllers/story.js | 50 ++++++++++++++++++++++++++++++++++++-- models/Story.js | 10 +++++++- public/css/main.less | 13 ++++++++++ public/js/main.js | 39 ++++++++++++++++++++++++++++- seed_data/stories.json | 38 ++++++++++++++++++----------- views/stories/index.jade | 5 ++-- views/stories/stories.jade | 4 +-- views/stories/submit.jade | 27 ++++++++++++-------- 9 files changed, 159 insertions(+), 32 deletions(-) diff --git a/app.js b/app.js index bbff557451..552e0d6e0c 100644 --- a/app.js +++ b/app.js @@ -294,6 +294,11 @@ app.get( storyController.comments ); +app.post( + '/stories/submit', + storyController.storySubmission +); + app.get( '/stories/:storyName', storyController.returnIndividualStory diff --git a/controllers/story.js b/controllers/story.js index 36ae133107..581bb5e50b 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -56,13 +56,14 @@ exports.returnIndividualStory = function(req, res, next) { title: story.headline, link: story.link, author: story.author, - body: story.body, + description: story.description, rank: story.rank, upVotes: story.upVotes, comments: story.comments, id: story._id, user: req.user, - timeAgo: moment(story.timePosted).fromNow() + timeAgo: moment(story.timePosted).fromNow(), + image: story.image }); }); }; @@ -96,3 +97,48 @@ exports.comments = function(req, res, next) { return res.send(comment); }); }; + +/* + + author: {}, + comments: { + type: Array, + default: [] + }, + image: + */ + +exports.storySubmission = function(req, res, next) { + var data = req.body.data; + var storyLink = data.headline + .replace(/\'/g, '') + .replace(/\"/g, '') + .replace(/,/g, '') + .replace(/[^a-z0-9]/gi, ' ') + .replace(/\s+/g, ' ') + .toLowerCase(); + var story = new Story({ + headline: data.headline, + timePosted: Date.now(), + link: data.link, + description: data.description, + rank: 0, + upVotes: 0, + author: data.author, + comments: [], + image: data.image, + storyLink: storyLink + }); + + debug('this is a story', story); + + story.save(function(err, data) { + if (err) { + throw err; + } + res.send(JSON.stringify({ + storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase() + })); + }); + +}; diff --git a/models/Story.js b/models/Story.js index 79e41d774e..902ff2c893 100644 --- a/models/Story.js +++ b/models/Story.js @@ -14,7 +14,7 @@ var storySchema = new mongoose.Schema({ type: String, unique: false }, - body: { + description: { type: String, unique: false }, @@ -30,6 +30,14 @@ var storySchema = new mongoose.Schema({ comments: { type: Array, default: [] + }, + image: { + type: String, + default: '' + }, + storyLink: { + type: String, + default: '' } }); diff --git a/public/css/main.less b/public/css/main.less index 83c26034e6..55dbdd2332 100644 --- a/public/css/main.less +++ b/public/css/main.less @@ -764,6 +764,19 @@ iframe.iphone { text-align: center; } +.control-label .control-label-story-submission { + telt-align: left; +} + +.img-story-post { + max-width: 110px; + max-height: 110px; +} + +.spacer { + padding: 15px 0 15px 0; +} + //uncomment this to see the dimensions of all elements outlined in red //* { // border-color: red; diff --git a/public/js/main.js b/public/js/main.js index d6d5516306..0b3dee6fb0 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -55,7 +55,7 @@ $(document).ready(function() { if (res) { window.location.href = '/bonfires' } - }) + }); } } @@ -148,6 +148,43 @@ $(document).ready(function() { } }; $('#upvote').on('click', upvoteHandler); + + var storySubmitButtonHandler = function storySubmitButtonHandler() { + var data = $('#story-submission-form :input'); + var link = $(data[0]).val(); + var headline = $(data[1]).val(); + $('#story-submit').unbind('click'); + $.post('/stories/submit', + { + data: { + link: link, + headline: headline, + timePosted: Date.now(), + description: 'TODO', + + rank: 0, + upVotes: 0, + author: { + picture: user.profile.picture, + userId: user._id, + username: user.profile.username + }, + comments: [], + image: 'http://rossmounce.co.uk/wp-content/uploads/2014/11/grumpy-cat-no-1.jpg' + } + }) + .fail(function (xhr, textStatus, errorThrown) { + $('#story-submit').bind('click', storySubmitButtonHandler); + }) + .done(function (data, textStatus, xhr) { + window.dataReturn = data; + console.log(data); + window.location = '/stories/' + data.storyLink; + }); + + }; + + $('#story-submit').on('click', storySubmitButtonHandler); }); var profileValidation = angular.module('profileValidation',['ui.bootstrap']); diff --git a/seed_data/stories.json b/seed_data/stories.json index ccaedb7bd7..246950ec44 100644 --- a/seed_data/stories.json +++ b/seed_data/stories.json @@ -1,8 +1,9 @@ [ { "headline": "Cat sits on keyboard 0", + "storyLink": "cat sits on keyboard 0", "link": "http://kotaku.com/5991046/why-cats-love-sitting-on-keyboards", - "body": "cats love keyboards you know", + "description": "cats love keyboards you know", "rank": 0, "upVotes": [], "timePosted": 1425429660000, @@ -11,12 +12,14 @@ "userId": "a2ad135e2aa27c14fc73ee11", "picture": "http://www.cutecatgifs.com/wp-content/uploads/2013/11/little_guy_sleeping_in_lap.gif" }, - "comments": [] - }, + "comments": [], + "image": "http://rossmounce.co.uk/wp-content/uploads/2014/11/grumpy-cat-no-1.jpg" +}, { "headline": "Cat sits on keyboard 1", + "storyLink": "cat sits on keyboard 1", "link": "http://kotaku.com/5991046/why-cats-love-sitting-on-keyboards", - "body": "cats love keyboards you know", + "description": "cats love keyboards you know", "rank": 1, "upVotes": [], "timePosted": 1425429660000, @@ -25,12 +28,14 @@ "userId": "a2ad135e2aa27c14fc73ee22", "picture": "http://www.cutecatgifs.com/wp-content/uploads/2013/11/little_guy_sleeping_in_lap.gif" }, - "comments": [] - }, + "comments": [], + "image": "http://rossmounce.co.uk/wp-content/uploads/2014/11/grumpy-cat-no-1.jpg" +}, { "headline": "Cat sits on keyboard 3", + "storyLink": "cat sits on keyboard 3", "link": "http://kotaku.com/5991046/why-cats-love-sitting-on-keyboards", - "body": "cats love keyboards you know", + "description": "cats love keyboards you know", "rank": 3, "upVotes": [], "timePosted": 1425429660000, @@ -39,12 +44,14 @@ "userId": "a2ad135e2aa27c14fc73ee33", "picture": "http://www.cutecatgifs.com/wp-content/uploads/2013/11/little_guy_sleeping_in_lap.gif" }, - "comments": [] - }, + "comments": [], + "image": "http://rossmounce.co.uk/wp-content/uploads/2014/11/grumpy-cat-no-1.jpg" +}, { "headline": "Cat sits on keyboard 20", + "storyLink": "cat sits on keyboard 20", "link": "http://kotaku.com/5991046/why-cats-love-sitting-on-keyboards", - "body": "ipsizzle dolizzle sit amet, ghetto adipiscing elit. Nullam fo shizzle velizzle, aliquet volutpizzle, suscipizzle shiz, gravida vizzle, arcu. Pellentesque izzle tortor. Sizzle doggy. Boom shackalack izzle dolizzle dapibizzle ass tempizzle tellivizzle. Ma nizzle we gonna chung shiz izzle tellivizzle. Vestibulum dizzle tortor. Pellentesque pimpin' rhoncus you son of a bizzle. In dizzle habitasse platea dictumst. Donec dapibizzle. Curabitizzle pot yippiyo, pretizzle da bomb, mattis izzle, da bomb vitae, nunc. Ass suscipizzle. Cool sempizzle bow wow wow sed purus.", + "description": "ipsizzle dolizzle sit amet, ghetto adipiscing elit. Nullam fo shizzle velizzle, aliquet volutpizzle, suscipizzle shiz, gravida vizzle, arcu. Pellentesque izzle tortor. Sizzle doggy. Boom shackalack izzle dolizzle dapibizzle ass tempizzle tellivizzle. Ma nizzle we gonna chung shiz izzle tellivizzle. Vestibulum dizzle tortor. Pellentesque pimpin' rhoncus you son of a bizzle. In dizzle habitasse platea dictumst. Donec dapibizzle. Curabitizzle pot yippiyo, pretizzle da bomb, mattis izzle, da bomb vitae, nunc. Ass suscipizzle. Cool sempizzle bow wow wow sed purus.", "rank": 20, "upVotes": [], "timePosted": 1425429660000, @@ -55,12 +62,14 @@ }, "comments": [ "54f61b0e43f0c2b90f162ec4" - ] - }, + ], + "image": "http://rossmounce.co.uk/wp-content/uploads/2014/11/grumpy-cat-no-1.jpg" +}, { "headline": "Cat sits on keyboard 2", + "storyLink": "cat sits on keyboard 2", "link": "http://kotaku.com/5991046/why-cats-love-sitting-on-keyboards", - "body": "cats love keyboards you know", + "description": "cats love keyboards you know", "rank": 2, "upVotes": [], "timePosted": 1425429660000, @@ -69,6 +78,7 @@ "userId": "a2ad135e2aa27c14fc73ee55", "picture": "http://www.cutecatgifs.com/wp-content/uploads/2013/11/little_guy_sleeping_in_lap.gif" }, - "comments": [] + "comments": [], + "image": "http://rossmounce.co.uk/wp-content/uploads/2014/11/grumpy-cat-no-1.jpg" } ] \ No newline at end of file diff --git a/views/stories/index.jade b/views/stories/index.jade index 39636b3d4c..f3051e4197 100644 --- a/views/stories/index.jade +++ b/views/stories/index.jade @@ -2,8 +2,9 @@ extends ../layout block content script. var challengeName = 'Camper News'; + var user = !{JSON.stringify(user)}; .panel.panel-info .panel-heading.text-center Camper News - .panel-body.hug-top - include ./stories + .panel-body include ./submit + include ./stories diff --git a/views/stories/stories.jade b/views/stories/stories.jade index 0cb846a83c..9122d66c37 100644 --- a/views/stories/stories.jade +++ b/views/stories/stories.jade @@ -14,7 +14,7 @@ h3 function(data) { for (var i = 0; i < data.length; i++) { var li = document.createElement('li'); - var linkedName = getLinkedName(data[i].headline); + var linkedName = getLinkedName(data[i].storyLink); var rank = data[i].rank; $(li).html("
" + @@ -35,7 +35,7 @@ h3 "
" + "" + "
" + diff --git a/views/stories/submit.jade b/views/stories/submit.jade index ad3259afc3..9dacdc0e55 100644 --- a/views/stories/submit.jade +++ b/views/stories/submit.jade @@ -1,11 +1,18 @@ .col-xs-12 - form.form-horizontal - .form-group.col-xs-12.text-left - .col-xs-1.col-xs-offset-2 - label.control-label(for='name') Link - .col-xs-9 - input.form-control(placeholder='Paste your link here', name='Link') - .form-group - label.col-xs-1.col-xs-offset-2.control-label(for='name') Title - .col-xs-9 - input.form-control(placeholder='Type a headline for your link here', name='Title') + form.form-horizontal.control-label-story-submission#story-submission-form + .col-xs-2.img-story-post + img.img-responsive(src='#{user.profile.picture}') + .col-xs-10 + .form-group + .col-xs-1 + label.control-label.control-label-story-submission(for='name') Link + .col-xs-11 + input.form-control(placeholder='Paste your link here', name='Link') + .form-group + .col-xs-1 + label.control-label.control-label-story-submission(for='name') Title + .col-xs-11 + input.form-control(placeholder='Type a headline for your link here', name='Title') + .form-group + .btn.btn-large.btn-block.btn-primary#story-submit Submit +.spacer \ No newline at end of file From 40a42e29c2322510545cfb74f7408c3f8ea0b37e Mon Sep 17 00:00:00 2001 From: Nathan Leniz Date: Thu, 5 Mar 2015 19:40:44 +0900 Subject: [PATCH 2/3] Fixed url redirect after submission, upvoting now works --- controllers/story.js | 9 +++++---- public/js/main.js | 4 +--- views/stories/show.jade | 1 + 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/controllers/story.js b/controllers/story.js index 581bb5e50b..0e7744071a 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -29,9 +29,11 @@ exports.index = function(req, res, next) { exports.returnIndividualStory = function(req, res, next) { var dashedName = req.params.storyName; - storyName = dashedName.replace(/\-/g, ' '); + var storyName = dashedName.replace(/\-/g, ' '); - Story.find({'headline' : new RegExp(storyName, 'i')}, function(err, story) { + debug('looking for %s', storyName); + + Story.find({'storyLink' : new RegExp(storyName, 'i')}, function(err, story) { if (err) { next(err); } @@ -134,11 +136,10 @@ exports.storySubmission = function(req, res, next) { story.save(function(err, data) { if (err) { - throw err; + return res.status(500); } res.send(JSON.stringify({ storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase() })); }); - }; diff --git a/public/js/main.js b/public/js/main.js index 0b3dee6fb0..8a5f8216c3 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -177,9 +177,7 @@ $(document).ready(function() { $('#story-submit').bind('click', storySubmitButtonHandler); }) .done(function (data, textStatus, xhr) { - window.dataReturn = data; - console.log(data); - window.location = '/stories/' + data.storyLink; + window.location = '/stories/' + JSON.parse(data).storyLink; }); }; diff --git a/views/stories/show.jade b/views/stories/show.jade index 9024bf9711..c02ee316d5 100644 --- a/views/stories/show.jade +++ b/views/stories/show.jade @@ -5,6 +5,7 @@ block content var storyId = !{JSON.stringify(id)}; var comments = !{JSON.stringify(comments)}; var upVotes = !{JSON.stringify(upVotes)}; + var user = !{JSON.stringify(user)}; .panel.panel-info .panel-heading.text-center Camper News From 8552a06ac641f12578f4cde1307dabeeeb2a5383 Mon Sep 17 00:00:00 2001 From: Nathan Leniz Date: Thu, 5 Mar 2015 19:51:56 +0900 Subject: [PATCH 3/3] Add url correcting in case user forgets http:// or https:// so url works properly in show view, remove upvote arrows from index view --- controllers/story.js | 6 +++++- views/stories/stories.jade | 5 ----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/controllers/story.js b/controllers/story.js index 0e7744071a..83c64ec94b 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -119,10 +119,14 @@ exports.storySubmission = function(req, res, next) { .replace(/[^a-z0-9]/gi, ' ') .replace(/\s+/g, ' ') .toLowerCase(); + var link = data.link; + if (link.search(/^https?:\/\//g) === -1) { + link = 'http://' + link; + } var story = new Story({ headline: data.headline, timePosted: Date.now(), - link: data.link, + link: link, description: data.description, rank: 0, upVotes: 0, diff --git a/views/stories/stories.jade b/views/stories/stories.jade index 9122d66c37..c3dcc96caa 100644 --- a/views/stories/stories.jade +++ b/views/stories/stories.jade @@ -19,11 +19,6 @@ h3 $(li).html("
" + "
" + - "
" + - "
" + - "" + - "
" + - "
" + "
" + "" + rank + "" + "
" +