-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cb6bb0b
Showing
14 changed files
with
5,291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "airbnb-base" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.