Skip to content

Commit

Permalink
chore(backend): create avatar - upload and update
Browse files Browse the repository at this point in the history
  • Loading branch information
João Paulo committed May 12, 2020
1 parent 17dd756 commit 54ddbd1
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 6 deletions.
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_*
/node_modules
/dist
/dist
/tmp/*
!/tmp/.gitkeep
136 changes: 132 additions & 4 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"date-fns": "^2.12.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"multer": "^1.4.2",
"pg": "^8.0.3",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.2.24",
Expand All @@ -25,6 +26,7 @@
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.6",
"@types/jsonwebtoken": "^8.3.9",
"@types/multer": "^1.4.3",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"eslint": "^6.8.0",
Expand Down
19 changes: 19 additions & 0 deletions backend/src/config/upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path from 'path';
import crypto from 'crypto';
import multer from 'multer';

const tmpFolder = path.resolve(__dirname, '..', '..', 'tmp');

export default {
directory: tmpFolder,

storage: multer.diskStorage({
destination: tmpFolder,
filename(request, file, callback) {
const fileHash = crypto.randomBytes(10).toString('HEX');
const fileName = `${fileHash}-${file.originalname}`;

return callback(null, fileName);
},
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

export default class AddAvatarFieldToUser1589247025644
implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumn(
'users',
new TableColumn({
name: 'avatar',
type: 'varchar',
isNullable: true,
}),
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumn('users', 'avatar');
}
}
3 changes: 3 additions & 0 deletions backend/src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class User {
@Column()
password: string;

@Column()
avatar: string;

@CreateDateColumn()
created_at: Date;

Expand Down
30 changes: 30 additions & 0 deletions backend/src/routes/users.routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Router } from 'express';
import multer from 'multer';

import uploadConfig from '../config/upload';

import CreateUserService from '../services/CreateUserService';
import UpdateUserAvatarService from '../services/UpdateUserAvatarService';

import ensureAuthenticated from '../middlewares/ensureAuthenticated';

const usersRouter = Router();

const upload = multer(uploadConfig);

usersRouter.post('/', async (req, res) => {
try {
const { name, email, password } = req.body;
Expand All @@ -24,4 +32,26 @@ usersRouter.post('/', async (req, res) => {
}
});

usersRouter.patch(
'/avatar',
ensureAuthenticated,
upload.single('avatar'),
async (req, res) => {
try {
const updateUserAvatar = new UpdateUserAvatarService();

const user = await updateUserAvatar.execute({
user_id: req.user.id,
avatarFilename: req.file.filename,
});

delete user.password;

return res.json(user);
} catch (err) {
return res.status(400).json({ error: err.message });
}
},
);

export default usersRouter;
4 changes: 3 additions & 1 deletion backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import 'reflect-metadata';
import express from 'express';
import routes from './routes';

import uploadConfig from './config/upload';

import './database';

const app = express();

app.use(express.json());

app.use('/files', express.static(uploadConfig.directory));
app.use(routes);

app.listen(3333, () => {
Expand Down
Loading

0 comments on commit 54ddbd1

Please sign in to comment.