Skip to content

Commit

Permalink
feature(get single article):get single article
Browse files Browse the repository at this point in the history
get single article
[finishes #167222364]
  • Loading branch information
Cavdy committed Jul 22, 2019
1 parent adf3832 commit b2d0019
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 129 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ lib

.nyc_output/
lcov.info

# Ignore index.html
src/test.html
60 changes: 58 additions & 2 deletions src/controllers/articleController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Article, Tag, User } from '../db/models';
import { Article, Tag, User, Category } from '../db/models';
import slugGen from '../helpers/slugGen';
import urlExtractor from '../helpers/urlExtractor';
import tagExtractor from '../helpers/tagExtractor';
import searchHelper, { searchOutcome } from '../helpers/searchHelper';

/**
* Article controller class
* @description Article Controller
* @class ArticleController
*/
class ArticleController {
Expand Down Expand Up @@ -153,6 +153,62 @@ class ArticleController {
});
}
}

/**
* @description - Get a single article
* @static
* @async
* @param {object} req - request
* @param {object} res - response
* @returns {object} article
*
*/
static async getArticle(req, res) {
try {
const { slug } = req.params;

const getArticle = await Article.findOne({
where: {
slug
},
attributes: {
exclude: ['createdAt', 'updatedAt']
},
include: [
{
model: Category,
attributes: ['id', 'name']
},
{
model: User,
attributes: ['id', 'firstName', 'lastName', 'userName', 'bio', 'imageUrl']
}
]
});

const getTags = await Tag.findAll({
where: {
articleId: getArticle.id
}
})

const allTags = [];
getTags.map(tag => allTags.push(tag.tagName))

const article = {};
article.article = getArticle;
article.tag = allTags;
return res.status(200).json({
status: 200,
message: [article]
});
} catch (error) {
return res.status(500).json({
status: 500,
message: error.message
});
}
}
}

export default ArticleController;
2 changes: 0 additions & 2 deletions src/controllers/Auth.js → src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,10 @@ class AuthController {
}
}


/**
*
*@description logs in autheticated user
* @static
*@description logs in autheticated user
* @param {object} req the request body
* @param {object} res the response body
* @returns {object} res
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/ratingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RatingController {
static async rateArticle(req, res) {
const value = Number(req.body.value);
const id = req.user;
const {article} = res.locals;
const { article } = res.locals;
const articleId = article.id

try {
Expand Down Expand Up @@ -71,7 +71,7 @@ class RatingController {
* @memberof RatingController
*/
static async getAllArticlesRating(req, res){
const {article} = res.locals;
const { article } = res.locals;
const articleId = article.id
try{
let offset = 0;
Expand Down
74 changes: 37 additions & 37 deletions src/db/migrations/20190716060554-create-report.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Reports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
body: {
type: Sequelize.TEXT,
required: true,
allowNull: false
},
reportType: {
type: Sequelize.ENUM(['article', 'comment']),
required: true,
allowNull: false
},
reportTypeId: {
type: Sequelize.INTEGER,
required: true,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Reports');
}
};
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Reports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
body: {
type: Sequelize.TEXT,
required: true,
allowNull: false
},
reportType: {
type: Sequelize.ENUM(['article', 'comment']),
required: true,
allowNull: false
},
reportTypeId: {
type: Sequelize.INTEGER,
required: true,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Reports');
}
};
2 changes: 1 addition & 1 deletion src/db/migrations/30181008085213-create-commentLike.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ module.exports = {
}
}),
down: queryInterface => queryInterface.dropTable('CommentLikes'),
};
};
78 changes: 39 additions & 39 deletions src/db/migrations/30181008085230-create-bookmark.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Bookmarks', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
required: true,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'userId'
}
},
articleId: {
type: Sequelize.INTEGER,
required: true,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Articles',
key: 'id',
as: 'articleId'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
up: (queryInterface, Sequelize) => queryInterface.createTable('Bookmarks', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
required: true,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'userId'
}
}),
down: queryInterface => queryInterface.dropTable('Bookmarks'),
};
},
articleId: {
type: Sequelize.INTEGER,
required: true,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Articles',
key: 'id',
as: 'articleId'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Bookmarks'),
};
46 changes: 23 additions & 23 deletions src/db/models/bookmark.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
module.exports = (sequelize, DataTypes) => {
const Bookmark = sequelize.define('Bookmark', {
userId: {
type: DataTypes.INTEGER,
required: true,
},
articleId: {
type: DataTypes.INTEGER,
required: true,
}
}, {});
Bookmark.associate = (models) => {
Bookmark.belongsTo(models.Article, {
foreignKey: 'articleId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Bookmark.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Bookmark;
const Bookmark = sequelize.define('Bookmark', {
userId: {
type: DataTypes.INTEGER,
required: true,
},
articleId: {
type: DataTypes.INTEGER,
required: true,
}
}, {});
Bookmark.associate = (models) => {
Bookmark.belongsTo(models.Article, {
foreignKey: 'articleId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Bookmark.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Bookmark;
};
16 changes: 6 additions & 10 deletions src/db/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ const config = require(__dirname + '/../config/config.js')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config,
);
}
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config,
);

fs.readdirSync(__dirname)
.filter(file => {
Expand Down
16 changes: 8 additions & 8 deletions src/db/models/report.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = (sequelize, DataTypes) => {
const Report = sequelize.define('Report', {
body: DataTypes.TEXT,
reportType: DataTypes.ENUM(['article', 'comment']),
reportTypeId: DataTypes.INTEGER
}, {});
Report.associate = function(models) {
};
return Report;
const Report = sequelize.define('Report', {
body: DataTypes.TEXT,
reportType: DataTypes.ENUM(['article', 'comment']),
reportTypeId: DataTypes.INTEGER
}, {});
Report.associate = function(models) {
};
return Report;
};
2 changes: 1 addition & 1 deletion src/middleware/tokenGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TokenGenerator {
email
}
});
next();
return next();
}
return res.status(400).json({
status: 400,
Expand Down
Loading

0 comments on commit b2d0019

Please sign in to comment.