From ad50714af3938d02c36888b9c2e4f23038ce3723 Mon Sep 17 00:00:00 2001 From: Danilo Vieira Date: Wed, 30 Sep 2020 18:11:11 -0300 Subject: [PATCH] Atualizando avatar --- .../src/config/upload.ts | 6 ++- .../src/models/User.ts | 3 ++ .../src/routes/users.routes.ts | 22 +++++++++- .../src/services/UpdateUserAvatarService.ts | 40 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 nivel-02/02-iniciando-back-end-do-app/src/services/UpdateUserAvatarService.ts diff --git a/nivel-02/02-iniciando-back-end-do-app/src/config/upload.ts b/nivel-02/02-iniciando-back-end-do-app/src/config/upload.ts index 090875f..f2d3fa8 100644 --- a/nivel-02/02-iniciando-back-end-do-app/src/config/upload.ts +++ b/nivel-02/02-iniciando-back-end-do-app/src/config/upload.ts @@ -2,9 +2,13 @@ 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: path.resolve(__dirname, '..', '..', 'tmp'), + destination: tmpFolder, filename(request, file, callback) { const filehash = crypto.randomBytes(10).toString('hex'); const fileName = `${filehash}-${file.originalname}`; diff --git a/nivel-02/02-iniciando-back-end-do-app/src/models/User.ts b/nivel-02/02-iniciando-back-end-do-app/src/models/User.ts index a67f9c7..31da55b 100644 --- a/nivel-02/02-iniciando-back-end-do-app/src/models/User.ts +++ b/nivel-02/02-iniciando-back-end-do-app/src/models/User.ts @@ -20,6 +20,9 @@ class User { @Column() password: string; + @Column() + avatar: string; + @CreateDateColumn() created_at: Date; diff --git a/nivel-02/02-iniciando-back-end-do-app/src/routes/users.routes.ts b/nivel-02/02-iniciando-back-end-do-app/src/routes/users.routes.ts index c741ec2..40c3c34 100644 --- a/nivel-02/02-iniciando-back-end-do-app/src/routes/users.routes.ts +++ b/nivel-02/02-iniciando-back-end-do-app/src/routes/users.routes.ts @@ -3,6 +3,7 @@ import multer from 'multer'; import uploadConfig from '../config/upload'; import CreateUserService from '../services/CreateUserService'; +import UpdateUserAvatarService from '../services/UpdateUserAvatarService'; import ensureAuthenticated from '../middlewares/ensureAuthenticated'; @@ -41,7 +42,26 @@ usersRouter.patch( ensureAuthenticated, upload.single('avatar'), async (request, response) => { - return response.json({ ok: true }); + try { + const updateUserAvatar = new UpdateUserAvatarService(); + + const user = await updateUserAvatar.execute({ + user_id: request.user.id, + avatarFilename: request.file.filename, + }); + + const userWithoutPassword = { + id: user.id, + name: user.name, + email: user.email, + created_at: user.created_at, + updated_at: user.updated_at, + }; + + return response.json(userWithoutPassword); + } catch (err) { + return response.status(400).json({ error: err.message }); + } }, ); export default usersRouter; diff --git a/nivel-02/02-iniciando-back-end-do-app/src/services/UpdateUserAvatarService.ts b/nivel-02/02-iniciando-back-end-do-app/src/services/UpdateUserAvatarService.ts new file mode 100644 index 0000000..54db5a3 --- /dev/null +++ b/nivel-02/02-iniciando-back-end-do-app/src/services/UpdateUserAvatarService.ts @@ -0,0 +1,40 @@ +import { getRepository } from 'typeorm'; +import path from 'path'; +import fs from 'fs'; + +import uploadConfig from '../config/upload'; +import User from '../models/User'; + +interface Request { + user_id: string; + avatarFilename: string; +} + +class UpdateUserAvatarService { + public async execute({ user_id, avatarFilename }: Request): Promise { + const usersRepository = getRepository(User); + + const user = await usersRepository.findOne(user_id); + + if (!user) { + throw new Error('Only authenticated user can change avatar'); + } + + if (user.avatar) { + const userAvatarFilePath = path.join(uploadConfig.directory, user.avatar); + const userAvatarFileExists = await fs.promises.stat(userAvatarFilePath); + + if (userAvatarFileExists) { + await fs.promises.unlink(userAvatarFilePath); + } + } + + user.avatar = avatarFilename; + + await usersRepository.save(user); + + return user; + } +} + +export default UpdateUserAvatarService;