Skip to content

Commit

Permalink
user avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobarboza7 committed Jul 24, 2019
1 parent 566848d commit 6374c90
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/app/controllers/FileController.js
Original file line number Diff line number Diff line change
@@ -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();
19 changes: 19 additions & 0 deletions src/app/models/File.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions src/app/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 5 additions & 2 deletions src/database/index.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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));
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/database/migrations/20190724145702-create-files.js
Original file line number Diff line number Diff line change
@@ -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');
},
};
Original file line number Diff line number Diff line change
@@ -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');
},
};
5 changes: 2 additions & 3 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Binary file added tmp/uploads/63abb0581c8297b2e24e8fd51bcc4701.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6374c90

Please sign in to comment.