임태민

Create search function

- 검색 기능을 추가하였습니다
......@@ -13,10 +13,11 @@ router.get('/', async function(req, res){
page = !isNaN(page)?page:1;
limit = !isNaN(limit)?limit:10;
var searchQuery = createSearchQuery(req.query);
var skip = (page-1)*limit;
var count = await Post.countDocuments({});
var count = await Post.countDocuments(searchQuery);
var maxPage = Math.ceil(count/limit);
var posts = await Post.find({})
var posts = await Post.find(searchQuery)
.populate('author')
.sort('-createdAt')
.skip(skip)
......@@ -27,7 +28,9 @@ router.get('/', async function(req, res){
posts:posts,
currentPage:page,
maxPage:maxPage,
limit:limit
limit:limit,
searchType:req.query.searchType,
searchText:req.query.searchText
});
});
......@@ -112,5 +115,23 @@ function checkPermission(req, res, next){
});
}
// Search function
function createSearchQuery(queries){
var searchQuery = {};
if(queries.searchType && queries.searchText && queries.searchText.length >= 3){
var searchTypes = queries.searchType.toLowerCase().split(',');
var postQueries = [];
if(searchTypes.indexOf('title')>=0){
postQueries.push({ title: { $regex: new RegExp(queries.searchText, 'i') } });
}
if(searchTypes.indexOf('body')>=0){
postQueries.push({ body: { $regex: new RegExp(queries.searchText, 'i') } });
}
if(postQueries.length > 0) searchQuery = {$or:postQueries};
}
return searchQuery;
}
// Export module
module.exports = router;
\ No newline at end of file
......
......@@ -33,6 +33,26 @@ util.noPermission = function(req, res){
res.redirect('/login');
}
// 검색 기능 추가
util.getPostQueryString = function(req, res, next){
res.locals.getPostQueryString = function(isAppended=false, overwrites={}){
var queryString = '';
var queryArray = [];
var page = overwrites.page?overwrites.page:(req.query.page?req.query.page:'');
var limit = overwrites.limit?overwrites.limit:(req.query.limit?req.query.limit:'');
var searchType = overwrites.searchType?overwrites.searchType:(req.query.searchType?req.query.searchType:'');
var searchText = overwrites.searchText?overwrites.searchText:(req.query.searchText?req.query.searchText:'');
if(page) queryArray.push('page='+page);
if(limit) queryArray.push('limit='+limit);
if(searchType) queryArray.push('searchType='+searchType);
if(searchText) queryArray.push('searchText='+searchText);
if(queryArray.length>0) queryString = (isAppended?'&':'?') + queryArray.join('&');
return queryString;
}
next();
}
module.exports = util;
\ No newline at end of file
......
......@@ -71,6 +71,24 @@
</li>
</ul>
</nav>
<form action="/posts" method="get" style="float:right">
<div class="form-row">
<div class="form-group">
<div class="input-group">
<select name="searchType" class="custom-select">
<option value="title" <%= searchType=='title'?'selected':'' %>>Title</option>
</select>
<input minLength="3" type="text" name="searchText" value="<%= searchText %>">
<div class="input-group-append">
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</body>
......