Skip to content

Commit

Permalink
Add cascade effect when delete user
Browse files Browse the repository at this point in the history
  • Loading branch information
martins-rafael committed Nov 14, 2020
1 parent 37933d5 commit 6cb526a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 0 additions & 3 deletions src/app/controllers/ChefController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/app/controllers/RecipeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
24 changes: 23 additions & 1 deletion src/app/controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/app/services/LoadRecipeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down

0 comments on commit 6cb526a

Please sign in to comment.