{"size": 1261, "ext": "js", "max_stars_repo_path": "src/models/image.js", "max_stars_repo_name": "martuwilliner/grupo-1", "max_stars_repo_stars_event_min_datetime": "2021-08-02T03:52:36.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-02T03:52:36.000Z", "max_issues_repo_path": "src/models/image.js", "max_issues_repo_name": "martuwilliner/grupo-1", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/models/image.js", "max_forks_repo_name": "martuwilliner/grupo-1", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.1842105263, "max_line_length": 85, "alphanum_fraction": 0.6090404441}
const path = require("path");
const fs = require("fs");

module.exports = {
    directory: path.resolve(__dirname, "../data", "images.json"),
    all: function(){
        const file = fs.readFileSync(this.directory);
        return JSON.parse(file);
    },//para leer todo el archivo categories.json como ob literal
    one: function(id){
        return this.all().find(element => element.id == id);
    },//para buscar por el id q estoy buscando
    new: function (data) {
        let imagenes = this.all();
        let nuevo = {
            id: imagenes.length > 0 ? imagenes[imagenes.length -1].id + 1: 1,
            url: "img/products/" + data.filename, //filename es lo q viene del MULTER
        }
        imagenes.push(nuevo)
        fs.writeFileSync(this.directory,JSON.stringify(imagenes,null,2));
        return nuevo;  
    },
    delete: function(id){
        const directory = path.resolve(__dirname, '../data', 'images.json')
        let imagenes = this.all();
        let deleted = this.one(id);

        imagenes = imagenes.filter(imagen => imagen.id != deleted.id )
        fs.writeFileSync(directory,JSON.stringify(imagenes,null,2));

        return true;
    }
}




//VER PARA QUE DEVUELVA EN CASO DE SER NECESARIO MAS DE UNA IMAGEN (ARRAY)