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..83c64ec94b 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); } @@ -56,13 +58,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 +99,51 @@ 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 link = data.link; + if (link.search(/^https?:\/\//g) === -1) { + link = 'http://' + link; + } + var story = new Story({ + headline: data.headline, + timePosted: Date.now(), + link: 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) { + return res.status(500); + } + 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..8a5f8216c3 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,41 @@ $(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.location = '/stories/' + JSON.parse(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/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 diff --git a/views/stories/stories.jade b/views/stories/stories.jade index 0cb846a83c..c3dcc96caa 100644 --- a/views/stories/stories.jade +++ b/views/stories/stories.jade @@ -14,16 +14,11 @@ 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("