Skip to content

Commit

Permalink
Fix delete methods
Browse files Browse the repository at this point in the history
  • Loading branch information
martins-rafael committed Nov 19, 2020
1 parent 90ec16b commit f13e2c6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/app/controllers/ChefController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion src/app/controllers/RecipeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/app/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -128,16 +129,16 @@ 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);
}
});
});

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) {
Expand Down

0 comments on commit f13e2c6

Please sign in to comment.