diff --git a/src/app/controllers/FileController.js b/src/app/controllers/FileController.js new file mode 100644 index 00000000..fca30e34 --- /dev/null +++ b/src/app/controllers/FileController.js @@ -0,0 +1,16 @@ +import File from '../models/File'; + +class FileController { + async store(req, res) { + const { originalname: name, filename: path } = req.file; + + const file = await File.create({ + name, + path, + }); + + return res.json(file); + } +} + +export default new FileController(); diff --git a/src/app/models/File.js b/src/app/models/File.js new file mode 100644 index 00000000..a26f10ac --- /dev/null +++ b/src/app/models/File.js @@ -0,0 +1,19 @@ +import Sequelize, { Model } from 'sequelize'; + +class File extends Model { + static init(sequelize) { + super.init( + { + name: Sequelize.STRING, + path: Sequelize.STRING, + }, + { + sequelize, + } + ); + + return this; + } +} + +export default File; diff --git a/src/app/models/User.js b/src/app/models/User.js index 36d88243..51403508 100644 --- a/src/app/models/User.js +++ b/src/app/models/User.js @@ -25,6 +25,10 @@ class User extends Model { return this; } + static associate(models) { + this.belongsTo(models.File, { foreignKey: 'avatar_id' }); + } + checkPassword(password) { return bcrypt.compare(password, this.password_hash); } diff --git a/src/database/index.js b/src/database/index.js index 929a1667..28d0d82b 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -1,8 +1,9 @@ import Sequelize from 'sequelize'; import User from '../app/models/User'; +import File from '../app/models/File'; import databaseConfig from '../config/database'; -const models = [User]; +const models = [User, File]; class Database { constructor() { @@ -12,7 +13,9 @@ class Database { init() { this.connection = new Sequelize(databaseConfig); - models.map(model => model.init(this.connection)); + models + .map(model => model.init(this.connection)) + .map(model => model.associate && model.associate(this.connection.models)); } } diff --git a/src/database/migrations/20190724145702-create-files.js b/src/database/migrations/20190724145702-create-files.js new file mode 100644 index 00000000..807f3607 --- /dev/null +++ b/src/database/migrations/20190724145702-create-files.js @@ -0,0 +1,33 @@ +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('files', { + id: { + type: Sequelize.INTEGER, + allowNull: false, + autoIncrement: true, + primaryKey: true, + }, + name: { + type: Sequelize.STRING, + allowNull: false, + }, + path: { + type: Sequelize.STRING, + allowNull: false, + unique: true, + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + }, + }); + }, + + down: queryInterface => { + return queryInterface.dropTable('users'); + }, +}; diff --git a/src/database/migrations/20190724151158-add-avatar-field-to-users.js b/src/database/migrations/20190724151158-add-avatar-field-to-users.js new file mode 100644 index 00000000..bb3d5bba --- /dev/null +++ b/src/database/migrations/20190724151158-add-avatar-field-to-users.js @@ -0,0 +1,15 @@ +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.addColumn('users', 'avatar_id', { + type: Sequelize.INTEGER, + references: { model: 'files', key: 'id' }, + onUpdate: 'CASCADE', + onDelete: 'SET NULL', + allowNull: true, + }); + }, + + down: queryInterface => { + return queryInterface.removeColumn('users', 'avatar_id'); + }, +}; diff --git a/src/routes.js b/src/routes.js index 8970a562..8e3e7149 100644 --- a/src/routes.js +++ b/src/routes.js @@ -3,6 +3,7 @@ import multer from 'multer'; import multerConfig from './config/multer'; import UserController from './app/controllers/UserController'; import SessionController from './app/controllers/SessionController'; +import FileController from './app/controllers/FileController'; import authMiddleware from './app/middlewares/auth'; const routes = new Router(); @@ -14,8 +15,6 @@ routes.post('/sessions', SessionController.store); routes.use(authMiddleware); routes.put('/users', UserController.update); -routes.post('/files', upload.single('file'), (req, res) => { - return res.json({ ok: true }); -}); +routes.post('/files', upload.single('file'), FileController.store); export default routes; diff --git a/tmp/uploads/63abb0581c8297b2e24e8fd51bcc4701.png b/tmp/uploads/63abb0581c8297b2e24e8fd51bcc4701.png new file mode 100644 index 00000000..8cb0dd6d Binary files /dev/null and b/tmp/uploads/63abb0581c8297b2e24e8fd51bcc4701.png differ