From 6cb526aa12f6570fbdda243e0684351101b4c1f4 Mon Sep 17 00:00:00 2001 From: Rafael Martins Date: Fri, 13 Nov 2020 21:58:13 -0300 Subject: [PATCH] Add cascade effect when delete user --- src/app/controllers/ChefController.js | 3 --- src/app/controllers/RecipeController.js | 3 --- src/app/controllers/UserController.js | 24 +++++++++++++++++++++++- src/app/services/LoadRecipeService.js | 1 + 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/app/controllers/ChefController.js b/src/app/controllers/ChefController.js index 4a75c2e..31bde70 100644 --- a/src/app/controllers/ChefController.js +++ b/src/app/controllers/ChefController.js @@ -75,11 +75,8 @@ module.exports = { async delete(req, res) { try { await Chef.delete({ id: req.body.id }); - const file = await File.findOne({ where: { id: req.body.file_id } }); - await File.delete({ id: file.id }); unlinkSync(file.path); - return res.redirect('/admin/chefs'); } catch (err) { console.error(err); diff --git a/src/app/controllers/RecipeController.js b/src/app/controllers/RecipeController.js index 41476ca..c73fad2 100644 --- a/src/app/controllers/RecipeController.js +++ b/src/app/controllers/RecipeController.js @@ -133,10 +133,7 @@ module.exports = { async delete(req, res) { try { const files = await Recipe.files(req.body.id); - const deletedFilesPromise = files.map(file => { - RecipeFile.delete({ file_id: file.file_id }); - File.delete({ id: file.file_id }); unlinkSync(file.path); }); diff --git a/src/app/controllers/UserController.js b/src/app/controllers/UserController.js index fcabd8c..00c2efa 100644 --- a/src/app/controllers/UserController.js +++ b/src/app/controllers/UserController.js @@ -1,13 +1,23 @@ const crypto = require('crypto'); const { hash } = require('bcryptjs'); +const { unlinkSync } = require('fs'); const User = require('../models/User'); +const loadRecipeService = require('../services/LoadRecipeService'); const mailer = require('../../lib/mailer'); const { emailTemplate } = require('../../lib/utils'); module.exports = { async list(req, res) { const users = await User.all(); + const { success } = req.session; + + if (success) { + res.render('users/list', { users, success }); + req.session.success = ''; + return + } + return res.render('users/list', { users }); }, registerForm(req, res) { @@ -71,7 +81,8 @@ module.exports = { user.is_admin = user.is_admin.toString(); const { success } = req.session; - if(success) { + + if (success) { res.render('users/edit', { user, success }); req.session.success = ''; return @@ -107,7 +118,18 @@ module.exports = { }, async delete(req, res) { try { + const recipes = await loadRecipeService.load('userRecipes', req.body.id); + const deletedFilesPromise = recipes.map(recipe => { + recipe.files.map(file => { + unlinkSync(file.path); + }); + }); + + await Promise.all(deletedFilesPromise); await User.delete({ id: req.body.id }); + + req.session.success = 'Usuário excluido com sucesso!'; + return res.redirect('/admin/users'); } catch (err) { console.error(err); diff --git a/src/app/services/LoadRecipeService.js b/src/app/services/LoadRecipeService.js index c4c9abf..039d14b 100644 --- a/src/app/services/LoadRecipeService.js +++ b/src/app/services/LoadRecipeService.js @@ -38,6 +38,7 @@ const loadService = { const recipes = await Recipe.userRecipes(this.filter); const recipesPromise = recipes.map(async recipe => { const files = await getImages(recipe.id); + recipe.files = files; recipe.image = files[0].src; return recipe; });