--- lib/bookmark.js
+++ lib/bookmark.js
... | ... | @@ -29,6 +29,16 @@ |
29 | 29 |
return row |
30 | 30 |
} |
31 | 31 |
|
32 |
+async function findAll(userId) { |
|
33 |
+ await sequelize.sync() |
|
34 |
+ const rows = await Bookmark.findAll({ |
|
35 |
+ where: { |
|
36 |
+ userId: userId, |
|
37 |
+ }, |
|
38 |
+ }) |
|
39 |
+ return rows |
|
40 |
+} |
|
41 |
+ |
|
32 | 42 |
async function remove(id) { |
33 | 43 |
await sequelize.sync() |
34 | 44 |
const row = await Bookmark.destroy({ |
... | ... | @@ -42,5 +52,6 @@ |
42 | 52 |
module.exports = { |
43 | 53 |
create, |
44 | 54 |
get, |
55 |
+ findAll, |
|
45 | 56 |
remove, |
46 | 57 |
} |
--- routes/user.js
+++ routes/user.js
... | ... | @@ -1,6 +1,8 @@ |
1 | 1 |
const express = require('express') |
2 | 2 |
const router = express.Router() |
3 | 3 |
const user_service = require('../services/user-service') |
4 |
+const bookmark = require('../lib/bookmark') |
|
5 |
+const dayjs = require('dayjs') |
|
4 | 6 |
|
5 | 7 |
router.get('/signup', function (req, res) { |
6 | 8 |
res.render('user/signup', {}) |
... | ... | @@ -166,7 +168,16 @@ |
166 | 168 |
|
167 | 169 |
router.get('/mypage', async function (req, res) { |
168 | 170 |
if (req.session.user) { |
169 |
- res.render('user/mypage', { user: req.session.user }) |
|
171 |
+ const bookmarks = await bookmark.findAll(req.session.userId) |
|
172 |
+ bookmarks.forEach((bookmark) => { |
|
173 |
+ bookmark.created = dayjs(bookmark.createdAt).format( |
|
174 |
+ 'YYYY-MM-DD HH:mm:ss' |
|
175 |
+ ) |
|
176 |
+ }) |
|
177 |
+ res.render('user/mypage', { |
|
178 |
+ user: req.session.user, |
|
179 |
+ bookmarks: bookmarks || [], |
|
180 |
+ }) |
|
170 | 181 |
} else { |
171 | 182 |
res.redirect('/user/login') |
172 | 183 |
} |
--- tests/bookmark.test.js
+++ tests/bookmark.test.js
... | ... | @@ -11,13 +11,29 @@ |
11 | 11 |
bookmark.remove(result.dataValues.id) |
12 | 12 |
}) |
13 | 13 |
it('combined with user', async () => { |
14 |
- data.userId = (await user.create(userData)).dataValues.id; |
|
14 |
+ data.userId = (await user.create(userData)).dataValues.id |
|
15 | 15 |
const result = await bookmark.create(data) |
16 | 16 |
// get bookmark |
17 | 17 |
const row = await bookmark.get(result.dataValues.id) |
18 | 18 |
expect(row.pathname).toBe(data.pathname) |
19 | 19 |
expect(row.userId).toBe(data.userId) |
20 | 20 |
await bookmark.remove(result.dataValues.id) |
21 |
- user.remove(data.userId) |
|
21 |
+ await user.remove(data.userId) |
|
22 |
+ }) |
|
23 |
+ |
|
24 |
+ it('combined with user and get all bookmark by id', async () => { |
|
25 |
+ data.userId = (await user.create(userData)).dataValues.id |
|
26 |
+ const result = await bookmark.create(data) |
|
27 |
+ data.pathname = '/mib/nginx2' |
|
28 |
+ const result2 = await bookmark.create(data) |
|
29 |
+ // get bookmark |
|
30 |
+ const rows = await bookmark.findAll(data.userId) |
|
31 |
+ expect(rows.length).toBe(2) |
|
32 |
+ expect(rows[1].pathname).toBe(data.pathname) |
|
33 |
+ expect(rows[0].pathname).toBe(result.dataValues.pathname) |
|
34 |
+ expect(rows[0].userId).toBe(data.userId) |
|
35 |
+ await bookmark.remove(result2.dataValues.id) |
|
36 |
+ await bookmark.remove(result.dataValues.id) |
|
37 |
+ await user.remove(data.userId) |
|
22 | 38 |
}) |
23 | 39 |
}) |
--- views/user/mypage.ejs
+++ views/user/mypage.ejs
... | ... | @@ -74,6 +74,15 @@ |
74 | 74 |
<div id="msg"></div> |
75 | 75 |
</form> |
76 | 76 |
<hr /> |
77 |
+ <section> |
|
78 |
+ <h2>bookmark</h2> |
|
79 |
+ <ul style="text-align: left;"> |
|
80 |
+ <% for (let i = 0; i < bookmarks.length; i++) { %> |
|
81 |
+ <li><a href="<%= bookmarks[i].pathname %>" title="<%= bookmarks[i].created %>"> |
|
82 |
+ <%= bookmarks[i].pathname %></a></li> |
|
83 |
+ <% } %> |
|
84 |
+ </ul> |
|
85 |
+ </section> |
|
77 | 86 |
<a href="/user/logout">로그아웃</a> |
78 | 87 |
<!-- Logo --> |
79 | 88 |
<div id="logo"> |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?