|
1 | | -import low from 'lowdb' |
2 | | -import FileSync from 'lowdb/adapters/FileSync.js' |
3 | 1 | import { App } from '@tinyhttp/app' |
4 | 2 | import { urlencoded } from 'milliparsec' |
| 3 | +import { Low, JSONFile } from 'lowdb' |
5 | 4 |
|
6 | 5 | const app = new App() |
7 | | -const adapter = new FileSync('db.json') |
8 | | -const db = low(adapter) |
9 | | - |
10 | | -// get all posts |
| 6 | +const adapter = new JSONFile('db.json') |
| 7 | +const db = new Low(adapter) |
| 8 | +await db.read() |
| 9 | +const { posts } = db.data |
11 | 10 |
|
12 | 11 | app.use(urlencoded()) |
13 | 12 |
|
| 13 | +// get all posts |
14 | 14 | app.get('/', (_, res) => { |
15 | | - res.send(db.getState().posts) |
| 15 | + res.send(posts) |
16 | 16 | }) |
17 | 17 |
|
18 | 18 | // get post by id |
19 | 19 | app.get('/:id', (req, res) => { |
20 | | - res.send( |
21 | | - db |
22 | | - .get('posts') |
23 | | - // @ts-ignore |
24 | | - .find({ id: parseInt(req.params.id) }) |
25 | | - .value() |
26 | | - ) |
| 20 | + const currentPost = posts.find(post => post.id === parseInt(req.params.id)) |
| 21 | + if (currentPost) { |
| 22 | + res.send(currentPost) |
| 23 | + } else { |
| 24 | + res.send({ msg: `A post with an id ${req.params.id} is not found` }) |
| 25 | + } |
27 | 26 | }) |
28 | 27 |
|
29 | 28 | // add a post |
30 | 29 | app.post('/', (req, res) => { |
31 | 30 | if (req.body.title) { |
32 | | - // @ts-ignore |
33 | | - db.get('posts').push({ id: Date.now(), title: req.body.title, likes: 0 }).write() |
| 31 | + posts.push({ id: Date.now(), title: req.body.title, likes: 0 }) |
| 32 | + db.write() |
34 | 33 | res.send({ msg: `Post with title of "${req.body.title}" is successfully added` }) |
35 | 34 | } else { |
36 | | - res.send('Post title missing') |
| 35 | + res.send({ msg: 'Post title missing' }) |
37 | 36 | } |
38 | 37 | }) |
39 | 38 |
|
40 | 39 | // like a post |
41 | 40 | app.put('/:id', (req, res) => { |
42 | | - const currentPost = db |
43 | | - .get('posts') |
44 | | - |
45 | | - // @ts-ignore |
46 | | - .find({ id: parseInt(req.params.id) }) |
47 | | - .value() |
48 | | - currentPost.likes += 1 |
49 | | - db.write() |
50 | | - res.send({ msg: `You liked a post with a title of ${currentPost.title}` }) |
| 41 | + const currentPost = posts.find(post => post.id === parseInt(req.params.id)) |
| 42 | + if (currentPost) { |
| 43 | + currentPost.likes += 1 |
| 44 | + db.write() |
| 45 | + res.send({ msg: `You liked a post with a title of ${currentPost.title}` }) |
| 46 | + } else { |
| 47 | + res.send({ msg: `A post with an id ${req.params.id} is not found` }) |
| 48 | + } |
51 | 49 | }) |
52 | 50 |
|
53 | 51 | // delete a post |
54 | 52 | app.delete('/:id', (req, res) => { |
55 | | - db.get('posts') |
56 | | - |
57 | | - //@ts-ignore |
58 | | - .remove({ id: parseInt(req.params.id) }) |
59 | | - .write() |
60 | | - res.send({ msg: `A post with an id of ${req.params.id} has been deleted` }) |
| 53 | + const currentPost = posts.filter(post => post.id !== parseInt(req.params.id)) |
| 54 | + if (posts.length > currentPost.length) { |
| 55 | + db.data.posts = currentPost |
| 56 | + db.write() |
| 57 | + res.send({ msg: `A post with an id of ${req.params.id} has been deleted` }) |
| 58 | + } else { |
| 59 | + res.send({ msg: `A post with an id ${req.params.id} is not found` }) |
| 60 | + } |
61 | 61 | }) |
62 | 62 |
|
63 | 63 | app.listen(3000, () => console.log('Server is connected on http://localhost:3000')) |
0 commit comments