-
-
Save aryeharmon/aaf0fb11f48805135bc40b2a9e961c23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var models = require('../models'), | |
express = require('express'), | |
winston = require('winston'), | |
router = express.Router(), | |
Middleware = require("../lib/Middleware"), | |
fs = require('fs'), | |
pluralize = require('pluralize'); | |
var obj = JSON.parse(fs.readFileSync('././config/.db.state.json', 'utf8')); | |
for (var model_name in obj){ | |
(function(model_name) { | |
winston.info('Model Name ==> ',model_name); | |
//Create | |
router.post(`${model_name}/create`, function(req, res) { | |
fs.readFile( __dirname + '/../config/.db.state.json', function (err, data) { | |
if (err) { | |
throw err; | |
} | |
var data = JSON.parse(data); | |
var columns = data[`${model_name}`].columns; | |
var fields = {}; | |
for (var col in columns) { | |
var field_name = columns[col].field_name | |
if(columns[col].layouts.create !== "Hidden"){ | |
fields[field_name] = `req.body.${field_name}`; | |
} | |
} | |
models[`${model_name}`].create(fields) | |
.then(function() { | |
req.flash("success","{{name_model}} successfully created"); | |
res.redirect(`/api/${model_name}`); | |
}).catch(function(err) { | |
req.flash("error", `Error Create ${model_name}. `); | |
res.redirect(`/api/${model_name}/0`); | |
}); | |
}); | |
}); | |
//Edit | |
router.post(`${model_name}/:id/edit`, Middleware.mustLoggedIn, function(req, res) { | |
var id = req.params.result_id; | |
fs.readFile( __dirname + '/../config/.db.state.json', function (err, data) { | |
if (err) { | |
throw err; | |
} | |
var data = JSON.parse(data); | |
var columns = data[model_name].columns; | |
var fields = {}; | |
for (var col in columns) { | |
var field_name = columns[col].field_name | |
if(columns[col].layouts.list !== "Hidden"){ | |
fields[field_name] = `req.body.${field_name}`; | |
} | |
} | |
models["{{name_model}}"].update(req.fields, { | |
where: {id: id} | |
}).then(function () { | |
req.flash("success","{{name_model}} successfully Edit"); | |
var nameView = pluralize('{{name_model}}').toLowerCase(); | |
res.redirect(`api/${model_name}`); | |
}).catch(function(response) { | |
req.flash("error", " Error Edit User .......... "); | |
var nameView = name_model.toLowerCase(); | |
res.redirect(`/api/${model_name}/${id}/edit`); | |
}); | |
}); | |
}); | |
//Get All | |
router.get(`/${model_name}/`, Middleware.mustLoggedIn, function (req, res) { | |
var obj = JSON.parse(fs.readFileSync('././config/.db.state.json', 'utf8')); | |
var layouts = obj[`${model_name}`].layouts.dropzones.list.Default; | |
var fields = {}; | |
for(var field in layouts){ | |
var Field = layouts[field]; | |
if(Field.type === "container"){ | |
Field.columns.forEach(function(col){ | |
col.forEach(function(item){ | |
fields[item.field_name] = item; | |
}); | |
}); | |
}else { | |
fields[Field.field_name] = Field; | |
} | |
} | |
models[`${model_name}`].findAll({}).then(function(results) { | |
var Results = []; | |
var Result = {}; | |
results.forEach(function(result, index){ | |
result = result.toJSON(); | |
var RES = {"ID": result.id}; | |
for (var field in fields){ | |
RES[field] = result[field]; | |
RES["name_model"] = model_name | |
} | |
Results.push(RES); | |
}); | |
res.render('list-view', { | |
results: Results, | |
fields : fields, | |
name_model : model_name | |
}); | |
}); | |
}); | |
//Remove | |
router.get(`/${model_name}/:id/destroy`, Middleware.mustLoggedIn, function(req, res) { | |
models[`${model_name}`].destroy({ | |
where: { | |
id: req.params.id | |
} | |
}).then(function() { | |
//var nameView = pluralize('{{name_model}}').toLowerCase(); | |
res.redirect(`api/${model_name}`); | |
}); | |
}); | |
// Get By Id | |
router.get(`/${model_name}/:id`, Middleware.mustLoggedIn, function (req, res) { | |
var obj = JSON.parse(fs.readFileSync('././config/.db.state.json', 'utf8')); | |
var id = req.params.id; | |
var layouts = obj[`${model_name}`].layouts.dropzones.create.Default; | |
if(id !== "0"){ | |
layouts = obj[`${model_name}`].layouts.dropzones.edit.Default; | |
} | |
var fields = []; | |
for(var field in layouts){ | |
var Field = layouts[field]; | |
if(Field.type === "container"){ | |
fields.push({"type" : "container", subContainer1 : Field.columns[0], subContainer2: Field.columns[1]}); | |
}else { | |
fields.push(Field) ; | |
} | |
} | |
models[`${model_name}`].find({where: {id: id}}).then(function(result) { | |
var layout = []; | |
if(!result){ | |
result= fields | |
} | |
else{ | |
fields.forEach(function(field){ | |
if(field.type === "container"){ | |
field.subContainer1.forEach(function(item){ | |
item["value"] = result[item.field_name]; | |
}); | |
field.subContainer2.forEach(function(item){ | |
item["value"] = result[item.field_name]; | |
}); | |
}else { | |
field["value"] = result[field.field_name] | |
} | |
}); | |
result = fields | |
} | |
res.render("create-add-view", { | |
result: result | |
}); | |
}); | |
}); | |
)(model_name); | |
} | |
module.exports = router; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment