Skip to content

Commit

Permalink
Merge 8a677c0 into e69a1f9
Browse files Browse the repository at this point in the history
  • Loading branch information
cvjude committed Aug 7, 2019
2 parents e69a1f9 + 8a677c0 commit 64e91f1
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 10 deletions.
1 change: 1 addition & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
require('@babel/register');

module.exports = {
'config': path.resolve('server/db/config', 'config.js'),
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"dotenv": "^6.0.0",
"errorhandler": "^1.5.0",
"express": "^4.16.3",
"faker": "^4.1.0",
"jsonwebtoken": "^8.3.0",
"morgan": "^1.9.1",
"multer": "^1.4.2",
Expand All @@ -68,6 +69,7 @@
"@babel/register": "^7.5.5",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"chai-integer": "^0.1.0",
"coveralls": "^3.0.5",
"eslint": "^6.1.0",
"eslint-config-airbnb-base": "^13.2.0",
Expand Down
5 changes: 4 additions & 1 deletion server/controllers/articleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ class ArticleController {
articleBody,
image
} = req.body.article;
const readTime = Math.floor(articleBody.split(' ').length / 200);

const article = await models.Article.create({
title,
description,
tagList,
articleBody,
uuid: uuid.v1().split('-')[0],
authorId: req.user.id,
image
image,
readTime
});
article.tagList = [...article.dataValues.tagList.split(' ')];
return utils.successStat(res, 201, 'articles', article);
Expand Down
15 changes: 15 additions & 0 deletions server/db/migrations/20190802091121-update-articleBody-row.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => queryInterface
.changeColumn('Articles', 'articleBody', {
type: Sequelize.TEXT,
allowNull: false
}),

down: (queryInterface, Sequelize) => queryInterface
.changeColumn('Articles', 'articleBody', {
type: Sequelize.TEXT,
allowNull: false
}),
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {

down: (queryInterface, Sequelize) => queryInterface
.changeColumn('Articles', 'articleBody', {
type: Sequelize.STRING,
type: Sequelize.TEXT,
allowNull: false
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
up: async (queryInterface, Sequelize) => [
await queryInterface.addColumn('Articles', 'readTime', {
type: Sequelize.INTEGER,
allowNull: false
}),
],

down: async queryInterface => [
await queryInterface.removeColumn('Articles', 'readTime'),
]
};
1 change: 1 addition & 0 deletions server/db/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING,
unique: true
},
readTime: DataTypes.INTEGER,
favorited: DataTypes.BOOLEAN,
favoriteCounts: DataTypes.INTEGER,
image: DataTypes.STRING,
Expand Down
47 changes: 47 additions & 0 deletions server/db/seeders/20190802082211-seed-article-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import faker from 'faker';

const tags = ['js', 'andela', 'react', 'culture', 'sequelize', 'homely', 'travel', 'anime', 'happy'];

const articles = [{
title: 'the title',
articleBody: 'test article body',
description: 'test description',
tagList: 'happy',
image: 'test image',
slug: 'the-title',
readTime: 3,
authorId: 1,
createdAt: new Date(),
updatedAt: new Date()
}, {
title: 'the title',
articleBody: 'test article body',
description: 'test description',
tagList: 'marriage',
image: 'test image',
slug: 'the-title-0337485',
readTime: 3,
authorId: 26,
createdAt: new Date(),
updatedAt: new Date()
}];
for (let i = 0; i < 20; i += 1) {
const title = faker.random.words();
articles.push({
title,
articleBody: faker.lorem.text(),
description: faker.lorem.sentence(),
tagList: faker.random.arrayElement(tags),
image: faker.random.image(),
slug: title.split(' ').join('-') || title,
authorId: faker.random.number({ min: 1, max: 20 }),
readTime: faker.random.number({ min: 1, max: 10 }),
createdAt: new Date(),
updatedAt: new Date()
});
}

module.exports = {
up: queryInterface => queryInterface.bulkInsert('Articles', articles, {}),
down: queryInterface => queryInterface.bulkDelete('Articles', null, {})
};
16 changes: 9 additions & 7 deletions server/docs/ah-commando-doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/errorResponse"

get:
tags:
- Articles
Expand Down Expand Up @@ -576,7 +576,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/errorResponse"
$ref: "#/components/schemas/errorResponse"
/profiles/{username}:
get:
tags:
Expand Down Expand Up @@ -752,9 +752,9 @@ components:
password:
type: object
properties:
status:
status:
type: integer
message:
message:
type: string
profile:
type: object
Expand All @@ -776,8 +776,8 @@ components:
followers:
type: object
followings:
type: object
article:
type: object
article:
type: object
properties:
slug:
Expand All @@ -800,11 +800,13 @@ components:
type: string
author:
type: object
readTime:
type: number
image:
type: object
properties:
image:
type: string
type: string
errorResponse:
type: object
properties:
Expand Down
7 changes: 6 additions & 1 deletion server/tests/article.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import chaiInteger from 'chai-integer';
import jwt from 'jsonwebtoken';
import path from 'path';
import dotenv from 'dotenv';
Expand All @@ -10,6 +11,8 @@ import articleData from './testData/article.data';
dotenv.config();
const { expect } = chai;
chai.use(chaiHttp);
chai.use(chaiInteger);

const baseUrl = '/api/v1';
const wrongToken = 'wrongtoken';

Expand Down Expand Up @@ -86,8 +89,10 @@ describe('Article test', () => {
.set('Authorization', `Bearer ${userToken}`)
.send(articleData[0])
.end((err, res) => {
const { status } = res.body;
const { status, articles } = res.body;
expect(status).to.equal(201);
expect(articles).to.have.property('readTime');
expect(articles.readTime).to.be.an.integer();
done();
});
});
Expand Down

0 comments on commit 64e91f1

Please sign in to comment.