Skip to content

Commit

Permalink
Merge cedf278 into e7e8466
Browse files Browse the repository at this point in the history
  • Loading branch information
henperi committed Dec 15, 2018
2 parents e7e8466 + cedf278 commit fd79625
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ node_modules
.vscode

# Vs code
.vscode
.vscode/
.vscode/settings.json


Expand Down
54 changes: 23 additions & 31 deletions controllers/ArticlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
pageInfo,
checkTitle,
checkUser,
createNewTags
createNewTags,
calcReadingTime
} from '../helpers/articleHelper';

const { articles: Article, tags: Tag } = models;
Expand All @@ -21,7 +22,6 @@ class ArticlesController {
* @returns {object} Returned object
*/
static async create(req, res) {
// const { userId } = res.locals.user;
const { userId } = req.app.locals.user;
const {
tags, body, title, description, image
Expand All @@ -33,38 +33,25 @@ class ArticlesController {
}
});
const articleSlug = checkTitle(req.body.title, articleTitle);
const readingTime = calcReadingTime(body);

const newArticle = await Article.create({
userId,
title,
description,
body,
image,
readingTime,
slug: articleSlug,
});

if (tags) {
const createTags = await createNewTags(tags);
await newArticle.addTags(createTags);
const createdTags = await createNewTags(tags);
await newArticle.addTags(createdTags);
newArticle.dataValues.tags = tags;
}

const createdArticle = await Article.findOne({
where: { id: newArticle.id },
include: {
model: Tag,
as: 'tags',
attributes: ['tagName'],
through: {
attributes: []
}
}
});

if (!createdArticle) {
const payload = { message: 'Article created' };
return StatusResponse.notfound(res, payload);
}
const payload = { article: createdArticle, message: 'Article successfully created' };
const payload = { article: newArticle, message: 'Article successfully created' };
return StatusResponse.created(res, payload);
} catch (error) {
return StatusResponse.internalServerError(res, {
Expand Down Expand Up @@ -160,6 +147,8 @@ class ArticlesController {
static async update(req, res) {
const { articles } = models;
const { userId } = req.app.locals.user;
const { body, title, tags } = req.body;

const paramsSlug = checkIdentifier(req.params.identifier);
try {
const article = await articles.findOne({
Expand All @@ -172,18 +161,23 @@ class ArticlesController {
message: 'Request denied'
});
}
if (title) {
req.body.slug = checkTitle(title, title);
}
if (body) {
req.body.readingTime = calcReadingTime(body);
}

const data = Object.keys(req.body);
const updatedArticle = await articles.update(req.body, {
where: { ...paramsSlug },
fields: data,
fields: ['title', 'body', 'readingTime', 'description', 'image', 'isPublished'],
returning: true,
plain: true
});
const { tags } = req.body;

if (tags) {
const createTags = await createNewTags(tags);
await updatedArticle.setTags(createTags);
const createdTags = await createNewTags(tags);
await article.setTags(createdTags);
updatedArticle['1']['0'].dataValues.tags = tags;
}

return StatusResponse.success(res, {
Expand Down Expand Up @@ -220,12 +214,10 @@ class ArticlesController {
}
const data = { isArchived: true };
await articles.update(data, {
where: { ...paramsSlug },
returning: true,
plain: true
where: { ...paramsSlug }
});
return StatusResponse.success(res, {
message: 'Article archived successfully'
message: 'Article deleted(archived) successfully'
});
} catch (error) {
return StatusResponse.internalServerError(res, {
Expand Down
12 changes: 11 additions & 1 deletion helpers/articleHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ const createNewTags = async (tags) => {
return tagIds;
};


const calcReadingTime = (bodyText) => {
const matches = bodyText.match(/\S+/g);
const numberOfWords = matches ? matches.length : 0;
const averageWPM = 225;
const readingTime = Math.ceil(numberOfWords / averageWPM);

return readingTime > 1 ? `${readingTime} mins read` : `${readingTime} min read`;
};

export {
checkIdentifier, pageInfo, checkTitle, checkUser, createNewTags
checkIdentifier, pageInfo, checkTitle, checkUser, createNewTags, calcReadingTime
};
3 changes: 3 additions & 0 deletions middlewares/articleMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const checkTags = (req, res, next) => {
const payload = { message: 'You can only add a maximum of 7 tags' };
return StatusResponse.badRequest(res, payload);
}

const tagString = tags.map(eachTag => eachTag.toString());
req.body.tags = tagString;
}
return next();
};
Expand Down
2 changes: 1 addition & 1 deletion test/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('Test for articles controller', () => {
res.status.should.equal(200);
res.body.should.have.a('object');
res.body.should.have.property('message');
res.body.message.should.equal('Article archived successfully');
res.body.message.should.equal('Article deleted(archived) successfully');
});
});
});
6 changes: 5 additions & 1 deletion test/helpers/articleHelpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chai from 'chai';
import chaiHttp from 'chai-http';

import { createNewTags } from '../../helpers/articleHelper';
import { createNewTags, calcReadingTime } from '../../helpers/articleHelper';

chai.use(chaiHttp);
chai.should();
Expand All @@ -11,4 +11,8 @@ describe('Article Helper tests', () => {
const result = await createNewTags(['food', 'rice', 'beans']);
result.should.be.an('array');
});
it('should calculate the reading time if a string is supplied', async () => {
const result = await calcReadingTime('I am a string of text');
result.should.be.equal('1 min read');
});
});
1 change: 0 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* This file will serve as the index to import all unit test and run them orderly
*/


import './middlewares/userValidation.test';
import './users';
import './passport';
Expand Down

0 comments on commit fd79625

Please sign in to comment.