Skip to content

Commit

Permalink
bug(article): Create and update an article with its read time
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie Mugenzi authored and Elie Mugenzi committed Jun 21, 2019
1 parent d4db10d commit dac4c68
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/api/controllers/articlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class articlesController {

const dataValues = await articles.createNewArticle(req);
const {
slug, title, description, body, tagList, author, updatedAt, createdAt
slug, title, description, body, tagList, author, updatedAt, createdAt, readtime
} = dataValues;

const result = {
slug, title, description, body, tagList, updatedAt, createdAt, author
// eslint-disable-next-line max-len
slug, title, description, body, tagList, updatedAt, createdAt, author, readtime
};
res.status(201).send({
article: result
Expand Down Expand Up @@ -83,7 +84,6 @@ class articlesController {
const oneArticle = await articles.getOneSlug(slug);
res.status(200).send({
status: 200,
readtime: readTime(oneArticle.body),
article: oneArticle
});
}
Expand Down Expand Up @@ -116,6 +116,7 @@ class articlesController {

// @generate an updated new slug
const newSlug = await articles.createSlug(updateSlug.title);
const newReadTime = readTime(updateSlug.body);

// @Updating the article's data in Database
await Article.update(
Expand All @@ -124,7 +125,8 @@ class articlesController {
title: updateSlug.title,
body: updateSlug.body,
description: updateSlug.description,
tagList: updateSlug.tagList
tagList: updateSlug.tagList,
readtime: newReadTime
},
{ where: { slug } }
);
Expand Down
10 changes: 7 additions & 3 deletions src/helpers/articlesHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import slug from 'slug';
import uniqid from 'uniqid';
import models from '../sequelize/models';
import readTime from './ReadTime.helper';

const { Article, User } = models;

Expand Down Expand Up @@ -30,13 +31,15 @@ class ArticlesHelper {
} = req.body;
const { id } = req.user;
const newSlug = this.createSlug(title);
const readtime = readTime(body);
const { dataValues } = await Article.create({
slug: newSlug,
title,
description,
body,
tagList: tagList.split(','),
authorId: parseInt(id, 10)
authorId: parseInt(id, 10),
readtime
});
const userInfo = await this.getUserInfo(id);
const { username, bio, image } = userInfo;
Expand All @@ -57,7 +60,8 @@ class ArticlesHelper {
model: User,
attributes: ['username', 'bio', 'image']
}],
attributes: ['slug', 'title', 'description', 'body', 'tagList', 'updatedAt', 'createdAt']
attributes: ['slug', 'title', 'description', 'readtime', 'body', 'tagList', 'updatedAt', 'createdAt'],
limit: 10
});
return result;
}
Expand All @@ -70,7 +74,7 @@ class ArticlesHelper {
model: User,
attributes: ['username', 'bio', 'image']
}],
attributes: ['slug', 'title', 'description', 'body', 'tagList', 'updatedAt', 'createdAt']
attributes: ['slug', 'title', 'description', 'readtime', 'body', 'tagList', 'updatedAt', 'createdAt']
});
return result;
}
Expand Down
4 changes: 4 additions & 0 deletions src/sequelize/migrations/20190613355309-create-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module.exports = {
allowNull: false,
type: Sequelize.STRING
},
readtime: {
allowNull: false,
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING,
allowNull: false
Expand Down
10 changes: 10 additions & 0 deletions src/sequelize/migrations/20190620145553-add-readtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn('Articles', 'readtime', Sequelize.STRING, {
after: 'title'
}),

// eslint-disable-next-line no-unused-vars
down: (queryInterface, Sequelize) => queryInterface.removeColumn('Articles', 'readtime')
};
4 changes: 4 additions & 0 deletions src/sequelize/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING,
allowNull: false
},
readtime: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: false
Expand Down
1 change: 1 addition & 0 deletions test/articles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ describe('Like/Unlike articles', () => {
tagList: ['reactjs', 'angularjs', 'expressjs'],
slug: 'lsug',
authorId: testUser.dataValues.id,
readtime: '1 min'
};

// create test article
Expand Down
2 changes: 2 additions & 0 deletions test/comments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('Comments', () => {
slug: '73H7812',
title: 'How to survive at Andela',
description: 'YoYo',
readtime: '1min',
body:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
image: '',
Expand All @@ -83,6 +84,7 @@ describe('Comments', () => {
slug: '73H99992',
title: 'Wow',
description: 'YoYo',
readtime: 'Less than a minute',
body:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
image: '',
Expand Down

0 comments on commit dac4c68

Please sign in to comment.