{"size": 2260, "ext": "js", "max_stars_repo_path": "src/app/validators/recipe.js", "max_stars_repo_name": "robsonsvicero/foodfy", "max_stars_repo_stars_event_min_datetime": "2020-06-19T14:35:56.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-24T03:15:35.000Z", "max_issues_repo_path": "src/app/validators/recipe.js", "max_issues_repo_name": "robsonsvicero/foodfy", "max_issues_repo_issues_event_min_datetime": "2021-02-10T03:03:12.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-13T17:18:47.000Z", "max_forks_repo_path": "src/app/validators/recipe.js", "max_forks_repo_name": "robsonsvicero/foodfy", "max_forks_repo_forks_event_min_datetime": "2020-11-14T16:51:42.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-22T16:54:43.000Z"}
{"max_stars_count": 6, "max_issues_count": 3, "max_forks_count": 2, "avg_line_length": 28.6075949367, "max_line_length": 114, "alphanum_fraction": 0.5907079646}
const User = require("../models/User");
const Recipe = require("../models/Recipe");

async function edit(req, res, next) {

    let results = await Recipe.all();
    let recipes = results.rows;

    for (let index = 0; index < recipes.length; index++) {
        results = await Recipe.files(recipes[index].id);
        const files = results.rows.map(file => ({
            ...file,
            src: `${req.protocol}://${req.headers.host}${file.path.replace("public", "")}`
        }));

        if(files[0]) {
            recipes[index].image = files[0].src;
        } else {
            recipes[index].image = "//placehold.it/500x360";
        }
    }

    results = await Recipe.find(req.params.id);
    const recipe = results.rows[0];

    results = await User.isAdmin(req.session.userId);
    const sessionIsAdmin = results.rows[0].is_admin;

    if (!sessionIsAdmin && recipe.user_id != req.session.userId) return res.render("private-access/recipe/list", {
        error: "Ação permitida apenas para administradores ou para o criador da receita",
        recipes,
        session: req.session,
        sessionIsAdmin
    });

    next();
}

async function putOrDelete(req, res, next) {

    let results = await Recipe.all();
    let recipes = results.rows;

    for (let index = 0; index < recipes.length; index++) {
        results = await Recipe.files(recipes[index].id);
        const files = results.rows.map(file => ({
            ...file,
            src: `${req.protocol}://${req.headers.host}${file.path.replace("public", "")}`
        }));

        if(files[0]) {
            recipes[index].image = files[0].src;
        } else {
            recipes[index].image = "//placehold.it/500x360";
        }
    }

    results = await Recipe.find(req.body.id);
    const recipe = results.rows[0];

    results = await User.isAdmin(req.session.userId);
    const sessionIsAdmin = results.rows[0].is_admin;

    if (!sessionIsAdmin && recipe.user_id != req.session.userId) return res.render("private-access/recipe/list", {
        error: "Ação permitida apenas para administradores ou para o criador da receita",
        recipes,
        session: req.session,
        sessionIsAdmin
    });

    next();
}



module.exports = {
    edit,
    putOrDelete
}