Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HigoRibeiro committed Dec 5, 2018
0 parents commit cb6bb0b
Show file tree
Hide file tree
Showing 14 changed files with 5,291 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "airbnb-base"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
8 changes: 8 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = {
'config': path.resolve('config', 'database.js'),
'models-path': path.resolve('app','models'),
'seeders-path': path.resolve('database', 'seeders'),
'migrations-path': path.resolve('database', 'migrations'),
};
7 changes: 7 additions & 0 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
username: 'rocketseat',
password: '',
database: 'blog',
host: '127.0.0.1',
dialect: 'mysql',
};
32 changes: 32 additions & 0 deletions database/migrations/20180814215401-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
up: (queryInterface, DataTypes) => {
queryInterface.createTable('Posts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
title: {
allowNull: false,
type: DataTypes.STRING,
},
content: {
allowNull: false,
type: DataTypes.TEXT,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
},
});
},

down: (queryInterface) => {
queryInterface.dropTable('Posts');
},
};
28 changes: 28 additions & 0 deletions database/migrations/20180814233730-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
up: (queryInterface, DataTypes) => {
queryInterface.createTable('Tags', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
title: {
allowNull: false,
type: DataTypes.STRING,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
},
});
},

down: (queryInterface) => {
queryInterface.dropTable('Tags');
},
};
36 changes: 36 additions & 0 deletions database/migrations/20180814233818-tags_posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = {
up: (queryInterface, DataTypes) => {
queryInterface.createTable('TagPosts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
PostId: {
type: DataTypes.INTEGER,
references: { model: 'Posts', key: 'id' },
onDelete: 'CASCADE',
allowNull: false,
},
TagId: {
type: DataTypes.INTEGER,
references: { model: 'Tags', key: 'id' },
onDelete: 'CASCADE',
allowNull: false,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
},
});
},

down: (queryInterface) => {
queryInterface.dropTable('TagPosts');
},
};
69 changes: 69 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const app = require('express')();
const bodyParser = require('body-parser');

const { Post, Tag } = require('./models');

app.use(bodyParser.json());

app.get('/posts', async (req, res) => {
try {
const posts = await Post.findAll({
include: [
{
model: Tag,
as: 'tags',
through: { attributes: [] },
},
],
});

return res.status(200).json(posts);
} catch (err) {
return res.status(500).json({ err });
}
});

app.post('/posts', async (req, res) => {
try {
const { tags, ...data } = req.body;
const post = await Post.create(data);

if (tags && tags.length > 0) {
post.setTags(tags);
}

return res.status(200).json(post);
} catch (err) {
return res.status(500).json({ err });
}
});

app.put('/posts/:id', async (req, res) => {
try {
const { id } = req.params;
const post = await Post.findById(id);

const { tags, ...data } = req.body;
post.update(data);

if (tags && tags.length > 0) {
post.setTags(tags);
}

return res.status(200).json(post);
} catch (err) {
return res.status(500).json({ err });
}
});

app.post('/tags', async (req, res) => {
try {
const tag = await Tag.create(req.body);

return res.status(200).json(tag);
} catch (err) {
return res.status(500).json({ err });
}
});

app.listen(3000);
16 changes: 16 additions & 0 deletions models/Post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = (sequelize, DataTypes) => {
const Post = sequelize.define('Post', {
title: DataTypes.STRING,
content: DataTypes.TEXT,
});

Post.associate = (models) => {
Post.belongsToMany(models.Tag, {
through: 'TagPosts',
as: 'tags',
foreignKey: 'PostId',
});
};

return Post;
};
15 changes: 15 additions & 0 deletions models/Tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = (sequelize, DataTypes) => {
const Tag = sequelize.define('Tag', {
title: DataTypes.STRING,
});

Tag.associate = (models) => {
Tag.belongsToMany(models.Post, {
through: 'TagPosts',
as: 'posts',
foreignKey: 'TagId',
});
};

return Tag;
};
28 changes: 28 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

const config = require('../config/database.js');

const db = {};
const sequelize = new Sequelize(config);

fs.readdirSync(__dirname)
.filter(
file => file.indexOf('.') !== 0 && file !== path.basename(__filename) && file.slice(-3) === '.js',
)
.forEach((file) => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
Loading

0 comments on commit cb6bb0b

Please sign in to comment.