Showing
3 changed files
with
63 additions
and
4 deletions
... | @@ -12,11 +12,12 @@ router.get('/', async function(req, res){ | ... | @@ -12,11 +12,12 @@ router.get('/', async function(req, res){ |
12 | var limit = Math.max(1, parseInt(req.query.limit)); | 12 | var limit = Math.max(1, parseInt(req.query.limit)); |
13 | page = !isNaN(page)?page:1; | 13 | page = !isNaN(page)?page:1; |
14 | limit = !isNaN(limit)?limit:10; | 14 | limit = !isNaN(limit)?limit:10; |
15 | - | 15 | + |
16 | + var searchQuery = createSearchQuery(req.query); | ||
16 | var skip = (page-1)*limit; | 17 | var skip = (page-1)*limit; |
17 | - var count = await Post.countDocuments({}); | 18 | + var count = await Post.countDocuments(searchQuery); |
18 | var maxPage = Math.ceil(count/limit); | 19 | var maxPage = Math.ceil(count/limit); |
19 | - var posts = await Post.find({}) | 20 | + var posts = await Post.find(searchQuery) |
20 | .populate('author') | 21 | .populate('author') |
21 | .sort('-createdAt') | 22 | .sort('-createdAt') |
22 | .skip(skip) | 23 | .skip(skip) |
... | @@ -27,7 +28,9 @@ router.get('/', async function(req, res){ | ... | @@ -27,7 +28,9 @@ router.get('/', async function(req, res){ |
27 | posts:posts, | 28 | posts:posts, |
28 | currentPage:page, | 29 | currentPage:page, |
29 | maxPage:maxPage, | 30 | maxPage:maxPage, |
30 | - limit:limit | 31 | + limit:limit, |
32 | + searchType:req.query.searchType, | ||
33 | + searchText:req.query.searchText | ||
31 | }); | 34 | }); |
32 | }); | 35 | }); |
33 | 36 | ||
... | @@ -112,5 +115,23 @@ function checkPermission(req, res, next){ | ... | @@ -112,5 +115,23 @@ function checkPermission(req, res, next){ |
112 | }); | 115 | }); |
113 | } | 116 | } |
114 | 117 | ||
118 | + | ||
119 | +// Search function | ||
120 | +function createSearchQuery(queries){ | ||
121 | + var searchQuery = {}; | ||
122 | + if(queries.searchType && queries.searchText && queries.searchText.length >= 3){ | ||
123 | + var searchTypes = queries.searchType.toLowerCase().split(','); | ||
124 | + var postQueries = []; | ||
125 | + if(searchTypes.indexOf('title')>=0){ | ||
126 | + postQueries.push({ title: { $regex: new RegExp(queries.searchText, 'i') } }); | ||
127 | + } | ||
128 | + if(searchTypes.indexOf('body')>=0){ | ||
129 | + postQueries.push({ body: { $regex: new RegExp(queries.searchText, 'i') } }); | ||
130 | + } | ||
131 | + if(postQueries.length > 0) searchQuery = {$or:postQueries}; | ||
132 | + } | ||
133 | + return searchQuery; | ||
134 | +} | ||
135 | + | ||
115 | // Export module | 136 | // Export module |
116 | module.exports = router; | 137 | module.exports = router; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -33,6 +33,26 @@ util.noPermission = function(req, res){ | ... | @@ -33,6 +33,26 @@ util.noPermission = function(req, res){ |
33 | res.redirect('/login'); | 33 | res.redirect('/login'); |
34 | } | 34 | } |
35 | 35 | ||
36 | +// 검색 기능 추가 | ||
37 | +util.getPostQueryString = function(req, res, next){ | ||
38 | + res.locals.getPostQueryString = function(isAppended=false, overwrites={}){ | ||
39 | + var queryString = ''; | ||
40 | + var queryArray = []; | ||
41 | + var page = overwrites.page?overwrites.page:(req.query.page?req.query.page:''); | ||
42 | + var limit = overwrites.limit?overwrites.limit:(req.query.limit?req.query.limit:''); | ||
43 | + var searchType = overwrites.searchType?overwrites.searchType:(req.query.searchType?req.query.searchType:''); | ||
44 | + var searchText = overwrites.searchText?overwrites.searchText:(req.query.searchText?req.query.searchText:''); | ||
36 | 45 | ||
46 | + if(page) queryArray.push('page='+page); | ||
47 | + if(limit) queryArray.push('limit='+limit); | ||
48 | + if(searchType) queryArray.push('searchType='+searchType); | ||
49 | + if(searchText) queryArray.push('searchText='+searchText); | ||
50 | + | ||
51 | + if(queryArray.length>0) queryString = (isAppended?'&':'?') + queryArray.join('&'); | ||
52 | + | ||
53 | + return queryString; | ||
54 | + } | ||
55 | + next(); | ||
56 | +} | ||
37 | 57 | ||
38 | module.exports = util; | 58 | module.exports = util; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -71,6 +71,24 @@ | ... | @@ -71,6 +71,24 @@ |
71 | </li> | 71 | </li> |
72 | </ul> | 72 | </ul> |
73 | </nav> | 73 | </nav> |
74 | + | ||
75 | + | ||
76 | + <form action="/posts" method="get" style="float:right"> | ||
77 | + <div class="form-row"> | ||
78 | + <div class="form-group"> | ||
79 | + <div class="input-group"> | ||
80 | + <select name="searchType" class="custom-select"> | ||
81 | + <option value="title" <%= searchType=='title'?'selected':'' %>>Title</option> | ||
82 | + </select> | ||
83 | + <input minLength="3" type="text" name="searchText" value="<%= searchText %>"> | ||
84 | + <div class="input-group-append"> | ||
85 | + <button class="btn btn-outline-primary" type="submit">Search</button> | ||
86 | + </div> | ||
87 | + </div> | ||
88 | + </div> | ||
89 | + </div> | ||
90 | + </form> | ||
91 | + | ||
74 | </div> | 92 | </div> |
75 | </div> | 93 | </div> |
76 | </body> | 94 | </body> | ... | ... |
-
Please register or login to post a comment