From 41442a540fb937b3b53e9f05df89eead82844f30 Mon Sep 17 00:00:00 2001 From: Nathan Leniz Date: Thu, 5 Mar 2015 19:21:26 +0900 Subject: [PATCH] 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