Skip to content

Commit

Permalink
#79 add editor.js in blog editor
Browse files Browse the repository at this point in the history
  • Loading branch information
kotte-sanya committed Aug 22, 2020
1 parent cfedf51 commit 1ce4adb
Show file tree
Hide file tree
Showing 16 changed files with 809 additions and 429 deletions.
113 changes: 111 additions & 2 deletions infrastructure/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions infrastructure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
"aws-sdk": "^2.724.0",
"axios": "^0.19.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dayjs": "^1.8.31",
"dotenv": "^8.2.0",
"editorjs-html": "^2.1.0",
"errorhandler": "^1.5.1",
"express": "^4.17.1",
"fast-json-stringify": "^1.21.0",
Expand All @@ -29,6 +31,7 @@
"markdown-it-mark": "^3.0.0",
"mongodb": "^3.6.0",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"nanoid": "^2.1.11",
"nodemailer": "^6.4.11",
"oauth": "^0.9.15",
Expand Down
20 changes: 12 additions & 8 deletions infrastructure/src/handlers/items/createBlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const api = require('../../utils/api.js');
const auth = require('../../utils/authenticate');
const logger = require('../../utils/logging');

const edjsHTML = require("editorjs-html");
const edjsParser = edjsHTML(api.edjsHtmlCustomParser());

module.exports = (app) => {
app.options('/v1/blog', auth.cors);

Expand All @@ -20,20 +23,20 @@ module.exports = (app) => {
logger.debug('Mongo connected');

const {
title, text,
title, source,
} = req.body;

if (!text) {
return api.sendBadRequest(res, api.createError('Missing parameter text', 'generic.internal-error'));
if (!source) {
return api.sendBadRequest(res, api.createError('Missing parameter source', 'generic.internal-error'));
}
const content = edjsParser.parse(source);

let user = auth.getIdentity(req.identity);


let publishDate = new Date();

const blogId = mongo.generateTimeId();
await insertItem(dbClient, blogId, title, text, user, publishDate);
await insertItem(dbClient, blogId, title, source, content, user, publishDate);
logger.debug('Blog inserted');

const pipeline = [mongo.stageId(blogId)];
Expand All @@ -48,14 +51,15 @@ module.exports = (app) => {
});
};

function insertItem(dbClient, blogId, title, text, user, publishDate) {
function insertItem(dbClient, blogId, title, source, content, user, publishDate) {
const blog = {
_id: blogId,
type: 'blog',
date: publishDate,
user: user.userId,
title,
text,
source,
content,
};

return dbClient.db().collection('blogs').insertOne(blog);
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/src/handlers/items/getBlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = (app) => {
const dbClient = await mongo.connectToDatabase();
logger.debug('Mongo connected');

const pipeline = [{ $sort: { 'date': -1 } }, mongo.stageLimit(1)];
const pipeline = [{ $sort: { 'date': -1 } }, mongo.stageLimit(1)];//TODO this is used only code check.

const blog = await getBlog(dbClient, pipeline);
logger.debug('Blog fetched');
Expand Down
46 changes: 46 additions & 0 deletions infrastructure/src/handlers/items/imageUpload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

const api = require('../../utils/api.js');
const auth = require('../../utils/authenticate');
const logger = require('../../utils/logging');

const bodyParser = require('body-parser');
const multer = require('multer');
const fs = require('fs');

const storage = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './src/dist/uploads';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
cb(null, dir);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
});

const upload = multer({ storage: storage });
const path = require('path');

module.exports = (app) => {
app.use(bodyParser.json());
app.options('/v1/uploadImage', auth.cors);

app.put('/v1/uploadImage', auth.cors, upload.single('image'), async (req, res) => {
logger.verbose('image upload handler starts');

try {
const body = {
url: `http://127.0.0.1:3000/uploads/${req.file.filename}`
}
return api.sendCreated(res, api.createResponse(body));

} catch (err) {
logger.error('Request failed', err);
return api.sendInternalError(res, api.createError('Failed to upload image', 'sign-in.something-went-wrong'));
}
});

};

17 changes: 11 additions & 6 deletions infrastructure/src/handlers/items/updateBlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const api = require('../../utils/api.js');
const auth = require('../../utils/authenticate');
const logger = require('../../utils/logging');

const edjsHTML = require("editorjs-html");
const edjsParser = edjsHTML(api.edjsHtmlCustomParser());

module.exports = (app) => {
app.options('/v1/blog/:blogId', auth.cors);

Expand All @@ -24,18 +27,19 @@ module.exports = (app) => {
logger.debug('Mongo connected');

const {
text, title, date,
source, title, date,
} = req.body;

if (!text) {
return api.sendBadRequest(res, api.createError('Missing parameter text', 'generic.internal-error'));
if (!source) {
return api.sendBadRequest(res, api.createError('Missing parameter source', 'generic.internal-error'));
}
const content = edjsParser.parse(source);

let user = auth.getIdentity(req.identity);

let publishDate = new Date();

const query = prepareUpdateQuery(text, user, title, publishDate);
const query = prepareUpdateQuery(source, content, user, title, publishDate);
await dbClient.db().collection('blogs').updateOne({ _id: blogId }, query);
logger.debug('Blog updated');

Expand All @@ -50,9 +54,10 @@ module.exports = (app) => {
});
};

function prepareUpdateQuery(text, author, title, date) {
function prepareUpdateQuery(source,content, author, title, date) {
const setters = {};
setters['text'] = text;
setters['source'] = source;
setters['content'] = content;
setters['title'] = title;
setters['user'] = author.userId;
setters['date'] = date;
Expand Down
1 change: 1 addition & 0 deletions infrastructure/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require('./handlers/items/tags')(app);
require('./handlers/items/createBlog')(app);
require('./handlers/items/updateBlog')(app);
require('./handlers/items/getBlog')(app);
require('./handlers/items/imageUpload')(app);
require('./handlers/misc/tagList')(app);
require('./handlers/comments/createComment')(app);
require('./handlers/comments/getComments')(app);
Expand Down
Loading

0 comments on commit 1ce4adb

Please sign in to comment.