diff --git a/src/app/controllers/ChefController.js b/src/app/controllers/ChefController.js index 0a325af..a8228d8 100644 --- a/src/app/controllers/ChefController.js +++ b/src/app/controllers/ChefController.js @@ -15,6 +15,15 @@ module.exports = { chefs.length == 0 ? pagination.total = 1 : pagination.total = Math.ceil(chefs[0].total / params.limit); + + const { success } = req.session; + + if (success) { + res.render('admin/chefs/index', { chefs, pagination, success }); + req.session.success = ''; + return + } + return res.render('admin/chefs/index', { chefs, pagination }); } catch (err) { console.error(err); @@ -83,7 +92,12 @@ module.exports = { try { await Chef.delete({ id: req.body.id }); const file = await File.findOne({ where: { id: req.body.file_id } }); - unlinkSync(file.path); + await File.delete({id: file.id}); + if (file.path != 'public/images/chef_placeholder.png') { + unlinkSync(file.path); + } + + req.session.success = 'Chef excluído com sucesso!'; 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 a3c2ba8..12df015 100644 --- a/src/app/controllers/RecipeController.js +++ b/src/app/controllers/RecipeController.js @@ -16,6 +16,14 @@ module.exports = { recipes.length == 0 ? pagination.total = 1 : pagination.total = Math.ceil(recipes[0].total / params.limit); + + const { success } = req.session; + + if (success) { + res.render('admin/recipes/index', { recipes, pagination, success }); + req.session.success = ''; + return + } return res.render('admin/recipes/index', { recipes, pagination }); } catch (err) { @@ -151,11 +159,15 @@ module.exports = { try { const files = await Recipe.files(req.body.id); const deletedFilesPromise = files.map(file => { - unlinkSync(file.path); + File.delete({ id: file.file_id }); + if (file.path != 'public/images/recipe_placeholder.png') { + unlinkSync(file.path); + } }); await Promise.all(deletedFilesPromise); await Recipe.delete({ id: req.body.id }); + req.session.success = 'Receita excluída com sucesso!'; return res.redirect('/admin'); } catch (err) { diff --git a/src/app/controllers/UserController.js b/src/app/controllers/UserController.js index 2978452..833274c 100644 --- a/src/app/controllers/UserController.js +++ b/src/app/controllers/UserController.js @@ -3,6 +3,7 @@ const { hash } = require('bcryptjs'); const { unlinkSync } = require('fs'); const User = require('../models/User'); +const File = require('../models/File'); const loadRecipeService = require('../services/LoadRecipeService'); const mailer = require('../../lib/mailer'); const { emailTemplate, getParams } = require('../../lib/utils'); @@ -128,7 +129,8 @@ module.exports = { const recipes = await loadRecipeService.load('userRecipes', req.body.id); const deletedFilesPromise = recipes.map(recipe => { recipe.files.map(file => { - if (file.path != 'public/images/chef_placeholder.png' && file.path != 'public/images/recipe_placeholder.png') { + File.delete({ id: file.file_id }); + if (file.path != 'public/images/recipe_placeholder.png') { unlinkSync(file.path); } }); @@ -136,8 +138,7 @@ module.exports = { await Promise.all(deletedFilesPromise); await User.delete({ id: req.body.id }); - - req.session.success = 'Usuário excluido com sucesso!'; + req.session.success = 'Usuário excluído com sucesso!'; return res.redirect('/admin/users'); } catch (err) {