Skip to content

Commit f7bb5a6

Browse files
committed
most of core structure in place, minus db/models
1 parent 11f6efb commit f7bb5a6

File tree

8 files changed

+109
-0
lines changed

8 files changed

+109
-0
lines changed

config/config.js

Whitespace-only changes.

controllers/blog-post.controller.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { blogService } = require('../services');
2+
3+
const { requireAuthentication } = auth;
4+
const { createBlogpost } = blogService;
5+
6+
function postBlogpost(user, content) {
7+
requireAuthentication(user);
8+
return createBlogpost(user, content);
9+
}
10+
11+
module.exports = {
12+
postBlogpost
13+
};

controllers/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const blogpost = require('./blog-post.controller');
2+
3+
module.exports = {
4+
blogpost
5+
};

models/blog-post.model.js

Whitespace-only changes.

routes/index.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const express = require('express');
2+
3+
const controllers = require('../controllers');
4+
5+
const router = express.Router();
6+
const {
7+
auth,
8+
home,
9+
publication,
10+
} = controllers;
11+
12+
/**
13+
* Handles controller execution and responds to user (API version).
14+
* This way controllers are not attached to the API.
15+
* Web socket has a similar handler implementation.
16+
* @param promise Controller Promise.
17+
* @param params (req) => [params, ...].
18+
*/
19+
const controllerHandler = (promise, params) => async (req, res, next) => {
20+
const boundParams = params ? params(req, res, next) : [];
21+
try {
22+
const result = await promise(...boundParams);
23+
return res.json(result || { message: 'OK' });
24+
} catch (error) {
25+
return res.status(500) && next(error);
26+
}
27+
};
28+
const c = controllerHandler;
29+
30+
/**
31+
* Auth.
32+
*/
33+
router.post('/signin', c(auth.signin, (req, res, next) => [req, res, next]));
34+
router.post('/signup', c(auth.signup, (req, res, next) => [req, res, next]));
35+
36+
/**
37+
* Home.
38+
*/
39+
router.get('/', c(home.hello));
40+
router.get('/greet/:name', c(home.getGreeting, req => [req.params.name]));
41+
router.post('/greet/', c(home.postGreeting, req => [req.body.name]));
42+
43+
/**
44+
* Publications.
45+
*/
46+
router.get('/publications', c(publication.getPublications));
47+
router.post('/publications', c(publication.postPublication, req => [req.user, req.body.content]));
48+
49+
/**
50+
* Error-handler.
51+
*/
52+
router.use((err, req, res, _next) => {
53+
// Expected errors always throw ServerError.
54+
// Unexpected errors will either throw unexpected stuff or crash the application.
55+
if (Object.prototype.isPrototypeOf.call(ServerError.prototype, err)) {
56+
return res.status(err.status || 500).json({ error: err.message });
57+
}
58+
59+
log.error('~~~ Unexpected error exception start ~~~');
60+
log.error(req);
61+
log.error(err);
62+
log.error('~~~ Unexpected error exception end ~~~');
63+
64+
65+
return res.status(500).json({ error: '⁽ƈ ͡ (ुŏ̥̥̥̥םŏ̥̥̥̥) ु' });
66+
});
67+
68+
module.exports = router;

server.js

Whitespace-only changes.

services/blog-post.service.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Post = require('../models/Post');
2+
3+
const createBlogpost = async (user, content) => {
4+
const post = new Post();
5+
post.user = user;
6+
post.content = content;
7+
8+
try {
9+
await post.validate();
10+
return post.save();
11+
} catch (e) {
12+
throw new Error(e.message);
13+
}
14+
}
15+
16+
module.exports = {
17+
createBlogpost
18+
};

services/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const blogService = require('./blog-post.service');
2+
3+
module.exports = {
4+
blogService
5+
};

0 commit comments

Comments
 (0)