Skip to content

Commit

Permalink
Merge pull request #24 from andela/ft-create-article-model-#163359366
Browse files Browse the repository at this point in the history
Ft create article model #163359366
  • Loading branch information
codrex committed Jan 22, 2019
2 parents f97af2a + 3f3b5fd commit a098147
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 73 deletions.
5 changes: 1 addition & 4 deletions database/migrations/20190114132910-create-user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable no-unused-vars */


module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', {
id: {
Expand Down Expand Up @@ -34,5 +31,5 @@ module.exports = {
type: Sequelize.DATE
}
}),
down: (queryInterface, Sequelize) => queryInterface.dropTable('Users')
down: queryInterface => queryInterface.dropTable('Users')
};
61 changes: 61 additions & 0 deletions database/migrations/20190121213602-articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable no-unused-vars */


module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Articles', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4
},
title: {
type: Sequelize.STRING,
allowNull: false
},
description: {
type: Sequelize.STRING,
allowNull: false
},
content: {
type: Sequelize.TEXT,
allowNull: false,
},
featuredImageUrl: {
type: Sequelize.STRING
},
averageRating: {
type: Sequelize.FLOAT,
defaultValue: 0
},
readTime: {
type: Sequelize.INTEGER,
},
slug: {
type: Sequelize.STRING,
allowNull: false
},
authorId: {
type: Sequelize.UUID,
onDelete: 'CASCADE',
foreignKey: true,
references: {
model: 'Users',
key: 'id'
}
},
ispublished: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface, Sequelize) => queryInterface.dropTable('Articles')
};
74 changes: 74 additions & 0 deletions database/models/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module.exports = (sequelize, DataTypes) => {
const Articles = sequelize.define(
'Articles',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4
},
title: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: {
args: [5, 400],
msg: 'Title must be between 5 and 400 characters'
}
}
},
description: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: {
args: [5, 200],
msg: 'Title must be between 5 and 200 characters'
}
}
},
content: {
type: DataTypes.TEXT,
allowNull: false,
validate: {
min: 250,
}
},
featuredImageUrl: {
type: DataTypes.STRING,
validate: {
is: ['(http(s?):)([/|.|[A-Za-z0-9_-]| |-])*.(?:jpg|gif|png)', 'i']
}
},
averageRating: {
type: DataTypes.FLOAT,
validate: {}
},
slug: {
type: DataTypes.STRING,
validate: {}
},
readTime: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
ispublished: {
type: DataTypes.BOOLEAN,
defaultValue: false,
validate: {
isBoolean: {
args: [true, false]
}
}
}
},
{}
);
Articles.associate = (models) => {
Articles.belongsTo(models.User, {
foreignKey: 'id',
as: 'authorId',
});
};
return Articles;
};
4 changes: 4 additions & 0 deletions database/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'UserId',
as: 'userprofile'
});
User.hasMany(models.Articles, {
foreignKey: 'id',
as: 'articles'
});
};
return User;
};
Loading

0 comments on commit a098147

Please sign in to comment.