Skip to content

Commit

Permalink
Merge 2927dbe into 44bf265
Browse files Browse the repository at this point in the history
  • Loading branch information
kagabof committed Jul 22, 2019
2 parents 44bf265 + 2927dbe commit 4dfb72a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 163 deletions.
24 changes: 7 additions & 17 deletions src/controllers/articleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ import searchArticlesHelper from './helpers/searchArticlesHelper';
import eventEmitter from '../template/notifications/EventEmitter';
import findUser from '../helpers/FindUser';
import readTime from './helpers/read_time';
import createSlug from './helpers/createSluge';
import { getAllArticles, articlePagination } from './helpers/getAllArticlesHelper';

const { Articles, User } = model;

const createSlug = (text) => {
let gen = `${text} ${(Math.floor(Math.random() * Math.floor(100000)))}`;
while (gen.match(/ /g)) {
gen = gen.replace(' ', '-');
}
return gen;
};

class articleContoller {
static async createArticle(req, res) {
const {
Expand Down Expand Up @@ -72,15 +66,6 @@ class articleContoller {
}
}

static async getAllArticles(req, res) {
try {
const articles = await Articles.findAll();
articles && res.status(200).json({ articles });
} catch (error) {
return res.status(404).json({ error: 'No article found' });
}
}

static async getOneArticle(req, res) {
try {
const article = await Articles.findOne({ where: { id: req.params.id } });
Expand Down Expand Up @@ -132,5 +117,10 @@ class articleContoller {

return data.length ? res.status(200).json({ data }) : res.status(404).json({ message: 'No data found' });
}


static async getArticles(req, res) {
(req.query.offset && req.query.limit) ? articlePagination(req, res) : getAllArticles(req, res);
}
}
export default articleContoller;
10 changes: 10 additions & 0 deletions src/controllers/helpers/createSluge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

const createSlug = (text) => {
let gen = `${text} ${(Math.floor(Math.random() * Math.floor(100000)))}`;
while (gen.match(/ /g)) {
gen = gen.replace(' ', '-');
}
return gen;
};

export default createSlug;
27 changes: 27 additions & 0 deletions src/controllers/helpers/getAllArticlesHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import model from '../../models';

const { Articles } = model;
const getAllArticles = async (req, res) => {
try {
const articles = await Articles.findAll();
return Articles.findAll() && res.status(200).json({ articles });
} catch (error) {
return res.status(404).json({ error: 'No article found' });
}
};

const articlePagination = async (req, res) => {
try {
const { offset, limit } = req.query;
const articles = await Articles.findAll({ offset, limit });
return articles.length ? res.status(200).json({ articles }) : res.status(404).json({ message: 'page not found' });
} catch (error) {
return res.status(404).json({ error: 'No article found' });
}
};

export {
getAllArticles,
articlePagination
};
5 changes: 2 additions & 3 deletions src/routes/articlesRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const articles = express.Router();

articles.post('/articles', verifyToken, bodyValidationArticle, articlesController.createArticle);
articles.patch('/articles/:id', verifyToken, authenticateUser.checkUserArticle, articlesController.updateArticle);
articles.get('/articles', articlesController.getAllArticles);
articles.get('/articles', articlesController.getArticles);
articles.get('/articles/:id', articlesController.getOneArticle);
articles.delete('/articles/:id', verifyToken, authenticateUser.checkUserArticle, articlesController.deleteArticle);
articles.get('/article/search', articlesController.searchArticles);
Expand All @@ -27,8 +27,7 @@ articles.get('/articles/:slug/share/email', verifyToken, slugExist, shareArticle

articles.post('/articles/:id/rating', Auth.verifyToken, ratingsController.createRatings);
articles.get('/articles/:id/ratings', ratingsController.getAllRatings);
articles.get('/articles', verifyToken, articlesController.getAllArticles);
articles.get('/articles/:id', verifyToken, articlesController.getOneArticle);
articles.delete('/articles/:id', verifyToken, authenticateUser.checkUserArticle, articlesController.deleteArticle);


export default articles;
54 changes: 30 additions & 24 deletions src/test/articlesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let userObject, articleObject, testUser, testArticle, tokenGen, tokens;

describe('Article', () => {
it('A new user who filled all required data should be registered in order to create an article', (done) => {
const user = { username: 'ffff',
password: 'Fofo1@hjsd',
email: 'faustinkagabo1@gmail.com' };
const user = { username: 'princia',
password: 'Princia@2006',
email: 'kagaboprince@gmail.com' };
chai.request(app)
.post('/api/users')
.send(user)
Expand All @@ -32,8 +32,9 @@ describe('Article', () => {
});

it('User should be able to sign in', (done) => {
const user = { email: 'faustinkagabo1@gmail.com',
password: 'Fofo1@hjsd' };
const user = { username: 'princia',
password: 'Princia@2006',
email: 'kagaboprince@gmail.com' };
chai.request(app)
.post('/api/users/login')
.send(user)
Expand All @@ -48,24 +49,6 @@ describe('Article', () => {
});
});

it('login for creating article', (done) => {
const user = { username: 'ffff',
password: 'Fofo1@hjsd',
email: 'faustinkagabo1@gmail.com' };
chai.request(app)
.post('/api/users/login')
.send(user)
.end((req, res) => {
res.should.have.status(200);
// res.body.should.be.an('object');
// res.body.should.have.property('username');
// res.body.should.have.property('email');
// res.body.should.have.property('token');
tokens = res.body.data.token;
done();
});
});

it('it should create an article', (done) => {
const article = { title: 'hello man, how was the night',
body: 'hello man, how was the night',
Expand Down Expand Up @@ -208,6 +191,17 @@ describe('Article', () => {
chai.request(app)
.get('/api/articles')
.set('token', tokens)
.end((req, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
done();
});
});

it('It should get all articles on a given pageNumber and page size', (done) => {
chai.request(app)
.get('/api/articles?offset=0&limit=2')
.set('token', tokens)
.end((req, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
Expand All @@ -216,6 +210,18 @@ describe('Article', () => {
});
});

it('It should not get an article with bad page number', (done) => {
chai.request(app)
.get('/api/articles?offset=20&limit=5')
.set('token', tokens)
.end((req, res) => {
res.should.have.status(404);
res.body.should.be.an('object');
res.body.should.have.property('message').eql('page not found');
done();
});
});

it('It should get all articles', (done) => {
chai.request(app)
.get('/api/articles/1')
Expand Down Expand Up @@ -252,7 +258,7 @@ describe('Article', () => {

it('should find all articles with author name', (done) => {
chai.request(app)
.get('/api/article/search?authorName=ffff')
.get('/api/article/search?authorName=princ')
.end((req, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
Expand Down
156 changes: 37 additions & 119 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,15 @@
"description": "user should be able to view all articles",
"parameters": [
{
"name": "token",
"in": "header",
"description": "The user's token",
"name": "offset",
"in": "query",
"description": "offset",
"required": true
},
{
"name": "limit",
"in": "query",
"description": "article limit",
"required": true
}
],
Expand Down Expand Up @@ -1394,74 +1400,37 @@
}
}
},
"/articles/{id}/rating": {
"post": {
"tags": [
"Articles"
],
"description": "Allows other users to rate an article",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Article id",
"required": true
},
{
"name": "token",
"in": "header",
"description": "User's token",
"required": true
},
{
"name": "rating",
"in": "body",
"description": "User is allowed rate article",
"schema": {
"$ref": "#/definitions/ratings"
}
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "rated the article"
}
}
}
},
"/articles/{id}/ratings": {
"get": {
"tags": [
"Articles"
],
"description": "Fetches ratings of an article",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Fetch ratings",
"require": true,
"/articles/{id}/ratings": {
"get": {
"tags": [
"Articles"
],
"description": "Fetches ratings of an article",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Fetch ratings",
"require": true,
"schema": {
"$ref": "#/definitions/fecthratings"
}
}
],
"produces":[
"application/json"
],
"responses": {
"200": {
"description": "Ratings",
"schema": {
"$ref": "#/definitions/fecthratings"
}
}
],
"produces":[
"application/json"
],
"responses": {
"200": {
"description": "Ratings",
"schema": {
"$ref": "#/definitions/fecthratings"
}
}
}
}
}
},"/bookmarks/{bookmarkId}": {
},
"/bookmarks/{bookmarkId}": {
"get": {
"tags": [
"Bookmark article"
Expand Down Expand Up @@ -1516,59 +1485,8 @@
}
}
}
},
"/bookmarks/{articleId}": {
"post": {
"tags": [
"Bookmark article"
],
"description": "Create bookmark",
"parameters": [
{
"name": "token",
"in": "header",
"description": "logged in user token"
},
{
"name": "articleId",
"in": "path",
"description": "Article Id",
"required": true
}
],
"produces": [
"application/json"
],
"responses": {
"201": {
"description": "bookmark data"
}
}
}
},
"/bookmarks": {
"get": {
"tags": [
"Bookmark article"
],
"description": "Get bookmarks",
"parameters": [
{
"name": "token",
"in": "header",
"description": "logged in user token"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "bookmark data"
}
}
}
}
}
}
}


0 comments on commit 4dfb72a

Please sign in to comment.