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
|
storyController.search
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.post(
|
||||||
|
'/stories/search',
|
||||||
|
storyController.getStories
|
||||||
|
);
|
||||||
|
|
||||||
app.get(
|
app.get(
|
||||||
'/stories/:storyName',
|
'/stories/:storyName',
|
||||||
storyController.returnIndividualStory
|
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) {
|
exports.returnIndividualStory = function(req, res, next) {
|
||||||
var dashedName = req.params.storyName;
|
var dashedName = req.params.storyName;
|
||||||
@ -90,7 +84,7 @@ exports.returnIndividualStory = function(req, res, next) {
|
|||||||
link: story.link,
|
link: story.link,
|
||||||
author: story.author,
|
author: story.author,
|
||||||
description: story.description,
|
description: story.description,
|
||||||
rank: story.rank,
|
rank: story.upVotes.length,
|
||||||
upVotes: story.upVotes,
|
upVotes: story.upVotes,
|
||||||
comments: story.comments,
|
comments: story.comments,
|
||||||
id: story._id,
|
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) {
|
exports.upvote = function(req, res, next) {
|
||||||
var data = req.body.data;
|
var data = req.body.data;
|
||||||
Story.find({'_id': data.id}, function(err, story) {
|
Story.find({'_id': data.id}, function(err, story) {
|
||||||
@ -149,8 +155,8 @@ exports.storySubmission = function(req, res, next) {
|
|||||||
timePosted: Date.now(),
|
timePosted: Date.now(),
|
||||||
link: link,
|
link: link,
|
||||||
description: data.description,
|
description: data.description,
|
||||||
rank: 0,
|
rank: 1,
|
||||||
upVotes: 0,
|
upVotes: data.upVotes,
|
||||||
author: data.author,
|
author: data.author,
|
||||||
comments: [],
|
comments: [],
|
||||||
image: data.image,
|
image: data.image,
|
||||||
|
@ -153,6 +153,10 @@ $(document).ready(function() {
|
|||||||
var data = $('#story-submission-form :input');
|
var data = $('#story-submission-form :input');
|
||||||
var link = $(data[0]).val();
|
var link = $(data[0]).val();
|
||||||
var headline = $(data[1]).val();
|
var headline = $(data[1]).val();
|
||||||
|
var userDataForUpvote = {
|
||||||
|
upVotedBy: user._id,
|
||||||
|
upVotedByUsername: user.profile.username
|
||||||
|
};
|
||||||
$('#story-submit').unbind('click');
|
$('#story-submit').unbind('click');
|
||||||
$.post('/stories/submit',
|
$.post('/stories/submit',
|
||||||
{
|
{
|
||||||
@ -162,8 +166,8 @@ $(document).ready(function() {
|
|||||||
timePosted: Date.now(),
|
timePosted: Date.now(),
|
||||||
description: 'TODO',
|
description: 'TODO',
|
||||||
|
|
||||||
rank: 0,
|
rank: 1,
|
||||||
upVotes: 0,
|
upVotes: [userDataForUpvote],
|
||||||
author: {
|
author: {
|
||||||
picture: user.profile.picture,
|
picture: user.profile.picture,
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
| Search
|
| Search
|
||||||
script.
|
script.
|
||||||
$('label').on('click', function() {
|
$('label').on('click', function() {
|
||||||
console.log('clicked');
|
|
||||||
window.location = ($(this).children('input').attr('href'));
|
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