홍지윤

Revert "Update project directory"

This reverts commit 7b452e41.
......@@ -5,7 +5,6 @@ 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();
......
This diff is collapsed. Click to expand it.
......@@ -18,7 +18,6 @@
"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"
}
......
......@@ -7,32 +7,6 @@ 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')
......@@ -40,6 +14,7 @@ router.get('/', function(req, res){
if(err){return res.json(err)};
res.render('posts/index', {posts:posts});
});
});
// Post new
......@@ -59,26 +34,10 @@ router.post('/', util.isLoggedin, function(req, res){
req.flash('errors', util.parseError(err));
return res.redirect('/posts/new');
};
res.redirect('/posts'+res.locals.getPostQueryString(false, { page:1, searchText:'' }));
res.redirect('/posts');
});
});
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){
......
......@@ -33,26 +33,6 @@ 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;
\ No newline at end of file
......
......@@ -10,6 +10,7 @@
<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>
......@@ -56,26 +57,5 @@
</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
......