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-02 16:44:27 +0900
1
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
7b452e4196be147209a62e9c3527c3a4fc085563
7b452e41
1 parent
86cd439a
Update project directory
search 기능 구현
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
86 additions
and
3 deletions
Project/index.js
Project/package-lock.json
Project/package.json
Project/routes/posts.js
Project/util.js
Project/views/posts/index.ejs
Project/views/posts/new.ejs
Project/index.js
View file @
7b452e4
...
...
@@ -5,6 +5,7 @@ var methodOverride = require('method-override');
var
flash
=
require
(
'connect-flash'
);
var
session
=
require
(
'express-session'
);
var
passport
=
require
(
'./config/passport'
);
var
multer
=
require
(
'multer'
);
//require('./config/passport');
var
app
=
express
();
...
...
Project/package-lock.json
View file @
7b452e4
This diff is collapsed. Click to expand it.
Project/package.json
View file @
7b452e4
...
...
@@ -18,6 +18,7 @@
"express-session"
:
"^1.17.1"
,
"method-override"
:
"^3.0.0"
,
"mongoose"
:
"^5.12.8"
,
"multer"
:
"^1.4.2"
,
"passport"
:
"^0.4.1"
,
"passport-local"
:
"^1.0.0"
}
...
...
Project/routes/posts.js
View file @
7b452e4
...
...
@@ -7,6 +7,32 @@ var util = require('../util');
// Post home
router
.
get
(
'/'
,
function
(
req
,
res
){
var
page
=
Math
.
max
(
1
,
parseInt
(
req
.
query
.
page
));
var
limit
=
Math
.
max
(
1
,
parseInt
(
req
.
query
.
limit
));
page
=
!
isNaN
(
page
)?
page
:
1
;
limit
=
!
isNaN
(
limit
)?
limit
:
10
;
var
searchQuery
=
createSearchQuery
(
req
.
query
);
// 1
var
skip
=
(
page
-
1
)
*
limit
;
var
count
=
await
Post
.
countDocuments
(
searchQuery
);
// 1-1
var
maxPage
=
Math
.
ceil
(
count
/
limit
);
var
posts
=
await
Post
.
find
(
searchQuery
)
// 1-2
.
populate
(
'author'
)
.
sort
(
'-createdAt'
)
.
skip
(
skip
)
.
limit
(
limit
)
.
exec
();
res
.
render
(
'posts/index'
,
{
posts
:
posts
,
currentPage
:
page
,
maxPage
:
maxPage
,
limit
:
limit
,
searchType
:
req
.
query
.
searchType
,
// 2
searchText
:
req
.
query
.
searchText
// 2
});
});
Post
.
find
({})
.
populate
(
'author'
)
.
sort
(
'-createdAt'
)
...
...
@@ -14,7 +40,6 @@ router.get('/', function(req, res){
if
(
err
){
return
res
.
json
(
err
)};
res
.
render
(
'posts/index'
,
{
posts
:
posts
});
});
});
// Post new
...
...
@@ -34,10 +59,26 @@ router.post('/', util.isLoggedin, function(req, res){
req
.
flash
(
'errors'
,
util
.
parseError
(
err
));
return
res
.
redirect
(
'/posts/new'
);
};
res
.
redirect
(
'/posts'
);
res
.
redirect
(
'/posts'
+
res
.
locals
.
getPostQueryString
(
false
,
{
page
:
1
,
searchText
:
''
})
);
});
});
function
createSearchQuery
(
queries
){
var
searchQuery
=
{};
if
(
queries
.
searchType
&&
queries
.
searchText
&&
queries
.
searchText
.
length
>=
3
){
// 1
var
searchTypes
=
queries
.
searchType
.
toLowerCase
().
split
(
','
);
var
postQueries
=
[];
if
(
searchTypes
.
indexOf
(
'title'
)
>=
0
){
postQueries
.
push
({
title
:
{
$regex
:
new
RegExp
(
queries
.
searchText
,
'i'
)
}
});
// 2
}
if
(
searchTypes
.
indexOf
(
'body'
)
>=
0
){
postQueries
.
push
({
body
:
{
$regex
:
new
RegExp
(
queries
.
searchText
,
'i'
)
}
});
}
if
(
postQueries
.
length
>
0
)
searchQuery
=
{
$or
:
postQueries
};
// 3
}
return
searchQuery
;
}
// Post show
router
.
get
(
'/:id'
,
util
.
isLoggedin
,
function
(
req
,
res
){
...
...
Project/util.js
View file @
7b452e4
...
...
@@ -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
:
''
);
// 1
var
searchText
=
overwrites
.
searchText
?
overwrites
.
searchText
:(
req
.
query
.
searchText
?
req
.
query
.
searchText
:
''
);
// 1
if
(
page
)
queryArray
.
push
(
'page='
+
page
);
if
(
limit
)
queryArray
.
push
(
'limit='
+
limit
);
if
(
searchType
)
queryArray
.
push
(
'searchType='
+
searchType
);
// 1
if
(
searchText
)
queryArray
.
push
(
'searchText='
+
searchText
);
// 1
if
(
queryArray
.
length
>
0
)
queryString
=
(
isAppended
?
'&'
:
'?'
)
+
queryArray
.
join
(
'&'
);
return
queryString
;
}
next
();
}
module
.
exports
=
util
;
...
...
Project/views/posts/index.ejs
View file @
7b452e4
...
...
@@ -10,7 +10,6 @@
<div
class=
"container mb-3"
>
<div>
<h2
class=
"mb-3"
style=
"font-weight: bold; font-style:italic; float:left;"
>
Memory
</h2>
<div
style=
"float:right"
>
<
% if(isAuthenticated){ %>
<a
class=
"btn btn-outline-primary"
href=
"/posts/new"
>
Write Memory
</a>
...
...
@@ -57,5 +56,26 @@
</div>
<form
action=
"/posts"
method=
"get"
class=
"post-index-tool"
>
<!-- 1 -->
<div
class=
"form-row"
>
<div
class=
"form-group col-9"
>
<!-- 2 -->
<label>
Search
</label>
<div
class=
"input-group"
>
<select
name=
"searchType"
class=
"custom-select"
>
<option
value=
"title,body"
<%
searchType=
='title,body'?'selected':''
%
>
>Title, Body
</option>
<option
value=
"title"
<%=
searchType=
='title'?'selected':''
%
>
>Title
</option>
<option
value=
"body"
<%=
searchType=
='body'?'selected':'''
%
>
>Body
</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>
</body>
</html>
\ No newline at end of file
...
...
Project/views/posts/new.ejs
View file @
7b452e4
홍지윤
@2017101322
2021-06-02 09:27:00 UTC
Mentioned in commit
0fcb9e6c
Please
register
or
login
to post a comment