Toggle navigation
Toggle navigation
This project
Loading...
Sign in
임태민
/
Mapmory
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
임태민
2021-06-04 16:19:23 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d0c912b71fd958a3cf3ec82521708484f7a9ef11
d0c912b7
1 parent
c057cbfd
Create search function
- 검색 기능을 추가하였습니다
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
3 deletions
routes/posts.js
util.js
views/posts/index.ejs
routes/posts.js
View file @
d0c912b
...
...
@@ -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
...
...
util.js
View file @
d0c912b
...
...
@@ -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
...
...
views/posts/index.ejs
View file @
d0c912b
...
...
@@ -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>
...
...
Please
register
or
login
to post a comment