add basic text search for post headlines
This commit is contained in:
5
app.js
5
app.js
@ -334,6 +334,11 @@ app.get(
|
||||
storyController.search
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/stories/search',
|
||||
storyController.getStories
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/stories/:storyName',
|
||||
storyController.returnIndividualStory
|
||||
|
@ -50,12 +50,6 @@ exports.recent = function(req, res, next) {
|
||||
});
|
||||
};
|
||||
|
||||
exports.specificView = function(req, res, next) {
|
||||
var whichView = req.params.type;
|
||||
res.render('stories/index', {
|
||||
page: whichView
|
||||
});
|
||||
};
|
||||
|
||||
exports.returnIndividualStory = function(req, res, next) {
|
||||
var dashedName = req.params.storyName;
|
||||
@ -90,7 +84,7 @@ exports.returnIndividualStory = function(req, res, next) {
|
||||
link: story.link,
|
||||
author: story.author,
|
||||
description: story.description,
|
||||
rank: story.rank,
|
||||
rank: story.upVotes.length,
|
||||
upVotes: story.upVotes,
|
||||
comments: story.comments,
|
||||
id: story._id,
|
||||
@ -101,6 +95,18 @@ exports.returnIndividualStory = function(req, res, next) {
|
||||
});
|
||||
};
|
||||
|
||||
exports.getStories = function(req, res, next) {
|
||||
debug('this is data', req.body.data.searchValue);
|
||||
Story.find({'headline': new RegExp(req.body.data.searchValue, 'i')}, function(err, results) {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
}
|
||||
debug('results are', results);
|
||||
|
||||
res.json(results);
|
||||
});
|
||||
};
|
||||
|
||||
exports.upvote = function(req, res, next) {
|
||||
var data = req.body.data;
|
||||
Story.find({'_id': data.id}, function(err, story) {
|
||||
@ -149,8 +155,8 @@ exports.storySubmission = function(req, res, next) {
|
||||
timePosted: Date.now(),
|
||||
link: link,
|
||||
description: data.description,
|
||||
rank: 0,
|
||||
upVotes: 0,
|
||||
rank: 1,
|
||||
upVotes: data.upVotes,
|
||||
author: data.author,
|
||||
comments: [],
|
||||
image: data.image,
|
||||
|
@ -153,6 +153,10 @@ $(document).ready(function() {
|
||||
var data = $('#story-submission-form :input');
|
||||
var link = $(data[0]).val();
|
||||
var headline = $(data[1]).val();
|
||||
var userDataForUpvote = {
|
||||
upVotedBy: user._id,
|
||||
upVotedByUsername: user.profile.username
|
||||
};
|
||||
$('#story-submit').unbind('click');
|
||||
$.post('/stories/submit',
|
||||
{
|
||||
@ -162,8 +166,8 @@ $(document).ready(function() {
|
||||
timePosted: Date.now(),
|
||||
description: 'TODO',
|
||||
|
||||
rank: 0,
|
||||
upVotes: 0,
|
||||
rank: 1,
|
||||
upVotes: [userDataForUpvote],
|
||||
author: {
|
||||
picture: user.profile.picture,
|
||||
userId: user._id,
|
||||
|
@ -14,7 +14,6 @@
|
||||
| Search
|
||||
script.
|
||||
$('label').on('click', function() {
|
||||
console.log('clicked');
|
||||
window.location = ($(this).children('input').attr('href'));
|
||||
});
|
||||
|
||||
|
@ -0,0 +1,58 @@
|
||||
input#searchArea(type=text)
|
||||
button#searchbutton
|
||||
#story-list
|
||||
ul#stories
|
||||
script.
|
||||
$('#searchbutton').on('click', function() {
|
||||
$('#stories').empty();
|
||||
var searchTerm = $('#searchArea').val();
|
||||
var getLinkedName = function getLinkedName(name) {
|
||||
return name.toLowerCase().replace(/\s/g, '-');
|
||||
}
|
||||
$.post('/stories/search',
|
||||
{
|
||||
data: {
|
||||
searchValue: searchTerm
|
||||
}
|
||||
})
|
||||
.fail(function (xhr, textStatus, errorThrown) {
|
||||
console.log('failure');
|
||||
//$('#comment-button').bind('click', commentSubmitButtonHandler);
|
||||
})
|
||||
.done(function (data, textStatus, xhr) {
|
||||
console.log(data);
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
var li = document.createElement('li');
|
||||
var linkedName = getLinkedName(data[i].storyLink);
|
||||
var rank = data[i].rank;
|
||||
|
||||
$(li).html(
|
||||
"<div class='row text-left'>" +
|
||||
"<div class='col-xs-3 col-sm-1'>" +
|
||||
"<div class='story-up-votes'>" +
|
||||
"<span>" + rank + "</span>" +
|
||||
"</div>" +
|
||||
"</div>" +
|
||||
"<div class='col-xs-2 col-sm-1'>" +
|
||||
"<img src='" + data[i].author.picture + "' class='img-responsive'/>" +
|
||||
"</div>" +
|
||||
"<div class='col-xs-7 col-sm-10'>" +
|
||||
"<div class='row'>" +
|
||||
"<div class='col-xs-12'>" +
|
||||
"<a href='/stories/" + linkedName + "'>"
|
||||
+ data[i].storyLink +
|
||||
"</a>" +
|
||||
"</div>" +
|
||||
"<div class='col-xs-12'>" +
|
||||
"<span>Posted " +
|
||||
moment(data[i].timePosted).fromNow() +
|
||||
" by <a href='/" + data[i].author.username + "'>@" + data[i].author.username + "</a> " +
|
||||
"</div>" +
|
||||
"</div>" +
|
||||
"</div>" +
|
||||
"</div>" +
|
||||
"</li>");
|
||||
$(li).appendTo($('#stories'));
|
||||
}
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user