Skip to content

Commit

Permalink
feat(set-article-price): set article price
Browse files Browse the repository at this point in the history
- update create article methods to set price
- update update article controllers
- update model and migration files
- add validation methods for price input
- add test for associated edge cases

[Delivers #159568537]
  • Loading branch information
Oloyedesinmiloluwa committed Aug 8, 2018
1 parent e875c01 commit 3f324d1
Show file tree
Hide file tree
Showing 10 changed files with 1,997 additions and 1,859 deletions.
1 change: 1 addition & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { } from 'dotenv/config';

module.exports = {
development: {
username: process.env.DB_USER,
Expand Down
8 changes: 5 additions & 3 deletions controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class ArticleController {
*/
static createArticle(req, res) {
const {
title, description, body, tagList, imageUrl,
title, description, body, tagList, imageUrl, isPaidFor, price
} = req.body.article;

const { userId } = req;

const articleObject = {
title, description, body, tagList, imageUrl, userId
title, description, body, tagList, imageUrl, isPaidFor, price, userId
};
/**
* check if image was provided in the request
Expand Down Expand Up @@ -119,13 +119,15 @@ class ArticleController {
* requests that fail
*/
static editArticle(req, res) {
const { title, description, body } = req.body.article;
const { title, description, body, isPaidFor, price } = req.body.article;
const { count } = req;
const { slug } = req.params;
return Article.update({
title,
description,
body,
isPaidFor,
price,
updatedCount: Utilities.increaseCount(count)
}, {
where: {
Expand Down
7 changes: 5 additions & 2 deletions helpers/createArticleHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { Article, User } from '../models';
*/

const createArticleHelper = (res, articleObject, imageUrl = null) => {
// articleObject.price = articleObject.price.toFixed(2);
articleObject.price = (articleObject.price) ? articleObject.price.toFixed(2) : undefined;
const {
title, description, body, tagList, userId
title, description, body, tagList, userId, isPaidFor, price,
} = articleObject;

return Article
.create({
slug: generateUniqueSlug(title),
Expand All @@ -24,6 +25,8 @@ const createArticleHelper = (res, articleObject, imageUrl = null) => {
userId,
tagList,
imageUrl,
isPaidFor,
price
})
.then(article => Article.findById(article.id, {
include: [{
Expand Down
32 changes: 32 additions & 0 deletions middlewares/validatePrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const validatePrice = (req, res, next) => {
if (req.body.article.price && typeof req.body.article.price !== 'number') {

return res.status(400).json({
errors: {
body: [
'Article price must be in figure'
]
}
});
}
if (req.body.article.isPaidFor && !req.body.article.price) {
return res.status(400).json({
errors: {
body: [
'Article is to be paid for, but price is not set'
]
}
});
}
if (req.body.article.price <= 0.28 || req.body.article.price >= 5.53) {
return res.status(400).json({
errors: {
body: [
'Price can only be between $0.28 to $5.53'
]
}
});
}
next();
};
export default validatePrice;
6 changes: 6 additions & 0 deletions migrations/20180730142739-create-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ module.exports = {
as: 'userId',
},
},
isPaidFor: {
type: Sequelize.BOOLEAN
},
price: {
type: Sequelize.DECIMAL
}
}),
down: (queryInterface/* , Sequelize */) => {
queryInterface.dropTable('Articles');
Expand Down
8 changes: 7 additions & 1 deletion models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ module.exports = (sequelize, DataTypes) => {
tagList: DataTypes.ARRAY(DataTypes.STRING),
favorited: DataTypes.BOOLEAN,
favoritesCount: DataTypes.INTEGER,
imageUrl: DataTypes.STRING
imageUrl: DataTypes.STRING,
isPaidFor: {
type: DataTypes.BOOLEAN
},
price: {
type: DataTypes.DECIMAL
}
}, {});

Article.associate = (models) => {
Expand Down
Loading

0 comments on commit 3f324d1

Please sign in to comment.