Showing
7 changed files
with
90 additions
and
7 deletions
... | @@ -5,6 +5,7 @@ var methodOverride = require('method-override'); | ... | @@ -5,6 +5,7 @@ var methodOverride = require('method-override'); |
5 | var flash = require('connect-flash'); | 5 | var flash = require('connect-flash'); |
6 | var session = require('express-session'); | 6 | var session = require('express-session'); |
7 | var passport = require('./config/passport'); | 7 | var passport = require('./config/passport'); |
8 | +var multer = require('multer'); | ||
8 | //require('./config/passport'); | 9 | //require('./config/passport'); |
9 | 10 | ||
10 | var app = express(); | 11 | var app = express(); | ... | ... |
This diff is collapsed. Click to expand it.
... | @@ -18,6 +18,7 @@ | ... | @@ -18,6 +18,7 @@ |
18 | "express-session": "^1.17.1", | 18 | "express-session": "^1.17.1", |
19 | "method-override": "^3.0.0", | 19 | "method-override": "^3.0.0", |
20 | "mongoose": "^5.12.8", | 20 | "mongoose": "^5.12.8", |
21 | + "multer": "^1.4.2", | ||
21 | "passport": "^0.4.1", | 22 | "passport": "^0.4.1", |
22 | "passport-local": "^1.0.0" | 23 | "passport-local": "^1.0.0" |
23 | } | 24 | } | ... | ... |
... | @@ -7,6 +7,32 @@ var util = require('../util'); | ... | @@ -7,6 +7,32 @@ var util = require('../util'); |
7 | 7 | ||
8 | // Post home | 8 | // Post home |
9 | router.get('/', function(req, res){ | 9 | router.get('/', function(req, res){ |
10 | + var page = Math.max(1, parseInt(req.query.page)); | ||
11 | + var limit = Math.max(1, parseInt(req.query.limit)); | ||
12 | + page = !isNaN(page)?page:1; | ||
13 | + limit = !isNaN(limit)?limit:10; | ||
14 | + | ||
15 | + var searchQuery = createSearchQuery(req.query); // 1 | ||
16 | + | ||
17 | + var skip = (page-1)*limit; | ||
18 | + var count = await Post.countDocuments(searchQuery); // 1-1 | ||
19 | + var maxPage = Math.ceil(count/limit); | ||
20 | + var posts = await Post.find(searchQuery) // 1-2 | ||
21 | + .populate('author') | ||
22 | + .sort('-createdAt') | ||
23 | + .skip(skip) | ||
24 | + .limit(limit) | ||
25 | + .exec(); | ||
26 | + | ||
27 | + res.render('posts/index', { | ||
28 | + posts:posts, | ||
29 | + currentPage:page, | ||
30 | + maxPage:maxPage, | ||
31 | + limit:limit, | ||
32 | + searchType:req.query.searchType, // 2 | ||
33 | + searchText:req.query.searchText // 2 | ||
34 | + }); | ||
35 | + }); | ||
10 | Post.find({}) | 36 | Post.find({}) |
11 | .populate('author') | 37 | .populate('author') |
12 | .sort('-createdAt') | 38 | .sort('-createdAt') |
... | @@ -14,7 +40,6 @@ router.get('/', function(req, res){ | ... | @@ -14,7 +40,6 @@ router.get('/', function(req, res){ |
14 | if(err){return res.json(err)}; | 40 | if(err){return res.json(err)}; |
15 | res.render('posts/index', {posts:posts}); | 41 | res.render('posts/index', {posts:posts}); |
16 | }); | 42 | }); |
17 | -}); | ||
18 | 43 | ||
19 | 44 | ||
20 | // Post new | 45 | // Post new |
... | @@ -34,10 +59,26 @@ router.post('/', util.isLoggedin, function(req, res){ | ... | @@ -34,10 +59,26 @@ router.post('/', util.isLoggedin, function(req, res){ |
34 | req.flash('errors', util.parseError(err)); | 59 | req.flash('errors', util.parseError(err)); |
35 | return res.redirect('/posts/new'); | 60 | return res.redirect('/posts/new'); |
36 | }; | 61 | }; |
37 | - res.redirect('/posts'); | 62 | + res.redirect('/posts'+res.locals.getPostQueryString(false, { page:1, searchText:'' })); |
38 | }); | 63 | }); |
39 | }); | 64 | }); |
40 | 65 | ||
66 | +function createSearchQuery(queries){ | ||
67 | + var searchQuery = {}; | ||
68 | + if(queries.searchType && queries.searchText && queries.searchText.length >= 3){ // 1 | ||
69 | + var searchTypes = queries.searchType.toLowerCase().split(','); | ||
70 | + var postQueries = []; | ||
71 | + if(searchTypes.indexOf('title')>=0){ | ||
72 | + postQueries.push({ title: { $regex: new RegExp(queries.searchText, 'i') } }); // 2 | ||
73 | + } | ||
74 | + if(searchTypes.indexOf('body')>=0){ | ||
75 | + postQueries.push({ body: { $regex: new RegExp(queries.searchText, 'i') } }); | ||
76 | + } | ||
77 | + if(postQueries.length > 0) searchQuery = {$or:postQueries}; // 3 | ||
78 | + } | ||
79 | + return searchQuery; | ||
80 | + } | ||
81 | + | ||
41 | 82 | ||
42 | // Post show | 83 | // Post show |
43 | router.get('/:id', util.isLoggedin, function(req, res){ | 84 | router.get('/:id', util.isLoggedin, function(req, res){ | ... | ... |
... | @@ -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 | +util.getPostQueryString = function(req, res, next){ | ||
37 | + res.locals.getPostQueryString = function(isAppended=false, overwrites={}){ | ||
38 | + var queryString = ''; | ||
39 | + var queryArray = []; | ||
40 | + var page = overwrites.page?overwrites.page:(req.query.page?req.query.page:''); | ||
41 | + var limit = overwrites.limit?overwrites.limit:(req.query.limit?req.query.limit:''); | ||
42 | + var searchType = overwrites.searchType?overwrites.searchType:(req.query.searchType?req.query.searchType:''); // 1 | ||
43 | + var searchText = overwrites.searchText?overwrites.searchText:(req.query.searchText?req.query.searchText:''); // 1 | ||
36 | 44 | ||
45 | + if(page) queryArray.push('page='+page); | ||
46 | + if(limit) queryArray.push('limit='+limit); | ||
47 | + if(searchType) queryArray.push('searchType='+searchType); // 1 | ||
48 | + if(searchText) queryArray.push('searchText='+searchText); // 1 | ||
49 | + | ||
50 | + if(queryArray.length>0) queryString = (isAppended?'&':'?') + queryArray.join('&'); | ||
51 | + | ||
52 | + return queryString; | ||
53 | + } | ||
54 | + next(); | ||
55 | +} | ||
56 | + | ||
57 | +module.exports = util; | ||
37 | 58 | ||
38 | -module.exports = util; | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -6,11 +6,10 @@ | ... | @@ -6,11 +6,10 @@ |
6 | </head> | 6 | </head> |
7 | <body> | 7 | <body> |
8 | <%- include('../partials/nav') %> | 8 | <%- include('../partials/nav') %> |
9 | - | 9 | + |
10 | <div class="container mb-3"> | 10 | <div class="container mb-3"> |
11 | <div> | 11 | <div> |
12 | - <h2 class="mb-3" style="font-weight: bold; font-style:italic; float:left;">Memory</h2> | 12 | + <h2 class="mb-3" style="font-weight: bold; font-style:italic; float:left;">Memory</h2> |
13 | - | ||
14 | <div style="float:right"> | 13 | <div style="float:right"> |
15 | <% if(isAuthenticated){ %> | 14 | <% if(isAuthenticated){ %> |
16 | <a class="btn btn-outline-primary" href="/posts/new">Write Memory</a> | 15 | <a class="btn btn-outline-primary" href="/posts/new">Write Memory</a> |
... | @@ -57,5 +56,26 @@ | ... | @@ -57,5 +56,26 @@ |
57 | 56 | ||
58 | 57 | ||
59 | </div> | 58 | </div> |
59 | + <form action="/posts" method="get" class="post-index-tool"> <!-- 1 --> | ||
60 | + <div class="form-row"> | ||
61 | + | ||
62 | + <div class="form-group col-9"> <!-- 2 --> | ||
63 | + <label>Search</label> | ||
64 | + <div class="input-group"> | ||
65 | + <select name="searchType" class="custom-select"> | ||
66 | + <option value="title,body" <% searchType=='title,body'?'selected':'' %>>Title, Body</option> | ||
67 | + <option value="title" <%= searchType=='title'?'selected':'' %>>Title</option> | ||
68 | + <option value="body" <%= searchType=='body'?'selected':''' %>>Body</option> | ||
69 | + </select> | ||
70 | + <input minLength="3" type="text" name="searchText" value="<%= searchText %>"> | ||
71 | + <div class="input-group-append"> | ||
72 | + <button class="btn btn-outline-primary" type="submit">search</button> | ||
73 | + </div> | ||
74 | + </div> | ||
75 | + </div> | ||
76 | + | ||
77 | + </div> | ||
78 | + </form> | ||
79 | + | ||
60 | </body> | 80 | </body> |
61 | </html> | 81 | </html> |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -48,7 +48,7 @@ | ... | @@ -48,7 +48,7 @@ |
48 | <%= errors.unhandled %> | 48 | <%= errors.unhandled %> |
49 | </div> | 49 | </div> |
50 | <% } %> | 50 | <% } %> |
51 | - | 51 | + |
52 | <div style="float:right"> | 52 | <div style="float:right"> |
53 | <a class="btn btn-outline-secondary" href="/posts">Back</a> | 53 | <a class="btn btn-outline-secondary" href="/posts">Back</a> |
54 | <button type="submit" class="btn btn-outline-primary">Submit</button> | 54 | <button type="submit" class="btn btn-outline-primary">Submit</button> | ... | ... |
-
Mentioned in commit 0fcb9e6c
-
Please register or login to post a comment