Skip to content

Commit 36a47f6

Browse files
committed
added utils folder, auto prepend api to all routes, fleshing out db and controller
1 parent d7370c2 commit 36a47f6

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

app.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ app.use(bodyParser.urlencoded({ extended: true }));
88

99
app.get('/', (req, res) => res.send('App is working'))
1010

11-
app.use(routes)
11+
app.use('/api', routes)
1212

1313
app.listen(3000, () => console.log('Example app listening on port 3000!'))
1414

1515
module.exports = {
1616
app
17-
}
17+
}

controllers/blog-post.controller.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
const { blogService } = require('../services')
22

3-
// const { requireAuthentication } = auth
43
const { createBlogpost } = blogService
54

6-
// in route, split off the req, or just pass req, res in here?
7-
function postBlogpost(user, content) {
8-
// requireAuthentication(user)
9-
return createBlogpost(user, content)
5+
/*
6+
* call other services, or service functions here if you need to
7+
*/
8+
const postBlogpost = async (req, res, next) => {
9+
const {user, content} = req.body
10+
try {
11+
await createBlogpost(user, content)
12+
res.sendStatus(201)
13+
next()
14+
} catch(e) {
15+
console.log(e.message)
16+
res.send(500) && next(error)
17+
}
1018
}
1119

1220
module.exports = {

db/blog-post.db.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const blogpostDb = (user, content) => {
2+
/*
3+
* put code to call database here
4+
* this can be either an ORM model or code to call the database through a driver or querybuilder
5+
* i.e.-
6+
INSERT INTO blogposts (user_name, blogpost_body)
7+
VALUES (user, content);
8+
*/
9+
return 1 //just a dummy return as we aren't calling db right now
10+
}
11+
12+
module.exports = {
13+
blogpostDb
14+
}

db/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const blogpostDb = require('./blog-post.db')
1+
const { blogpostDb } = require('./blog-post.db')
22

33
module.exports = {
44
blogpostDb

services/blog-post.service.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
const { blogpostDb } = require('../db')
22

3+
/*
4+
* if you need to make calls to additional tables, data stores (Redis, for example),
5+
* or call another endpoint as part of creating the blogpost, add them to this service
6+
*/
37
const createBlogpost = async (user, content) => {
4-
const post = new Post()
5-
post.user = user
6-
post.content = content
7-
88
try {
9-
await post.validate()
10-
return post.save()
11-
} catch (e) {
9+
return await blogpostDb(user, content)
10+
} catch(e) {
1211
throw new Error(e.message)
1312
}
1413
}

utils/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// utility functions common to the project go here

0 commit comments

Comments
 (0)