-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract-crud.js
More file actions
156 lines (118 loc) · 3.99 KB
/
Copy pathabstract-crud.js
File metadata and controls
156 lines (118 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const express = require('express');
module.exports = (db, table, render_pages) => {
const response = (req,res,data,pref) => {
if (req.accepts('html', 'json') === 'json') {
return res.json(data);
} else if (render_pages) {
const renders = {};
renders[table.name] = data;
return res.render(table.name + pref, renders);
} else {
return res.json(data);
}
}
const create = (req, res) => {
const newEntry = req.body;
let sql_columns = ' ';
let sql_values = ' ';
let values = [];
let count = 1;
for (let i = 0; i < table.columns.length; i++) {
const label = table.columns[i];
const value = newEntry[label];
if (value != null) {
sql_columns += '"'+label + '",';
values.push(value);
sql_values += '$' + (count++) + ",";
}
}
sql_columns = sql_columns.substring(0, sql_columns.length - 1);
sql_values = sql_values.substring(0, sql_values.length - 1);
const sql = 'INSERT INTO "' + table.name + '"(' + sql_columns + ') VALUES (' + sql_values + ') RETURNING ' + table.primary_key;
//console.log(sql);
//console.log(values);
db.one(sql, values)
.then(data => {
newEntry[table.primary_key] = data[table.primary_key];
return res.json(newEntry);
})
.catch(error => {
console.log(error);
return res.status(500).send('500: Internal Server Error');
});
};
const readMany = (req, res) => {
db.any('SELECT * FROM "' + table.name+'"', [])
.then(function (data) {
return response(req,res,data,"-show");
})
.catch(function (error) {
console.log(error);
return res.status(500).send('500: Internal Server Error');
});
};
const readOne = (req, res) => {
const { _id } = req.params;
db.any('SELECT * FROM "' + table.name + '" WHERE "' + table.primary_key + '" = $1 ', [_id])
.then(function (data) {
return response(req,res,data,"-edit");
})
.catch(function (error) {
console.log(error);
return res.status(500).send('500: Internal Server Error');
});
};
const update = (req, res) => {
const newEntry = req.body;
const { _id } = req.params;
if (_id == null) return res.status(500).send('500: Invalid ID');
let sql_set = ' ';
let values = [];
let count = 1;
for (let i = 0; i < table.columns.length; i++) {
const label = table.columns[i];
const value = newEntry[label];
if (value != null) {
sql_set += '"'+label + '" = $' + (count++) + ',';
values.push(value)
}
}
sql_set = sql_set.substring(0, sql_set.length - 1);
values.push(_id);
const sql = 'UPDATE "' + table.name + '" SET ' + sql_set + ' WHERE ' + table.primary_key + ' = $' + count;
//console.log(sql);
//console.log(values);
db.result(sql, values)
.then(result => {
//return res.json(result.rowCount);
return readOne(req, res);
})
.catch(error => {
console.log(error);
return res.status(500).send('500: Internal Server Error');
});
};
const remove = (req, res) => {
const { _id } = req.params;
db.result('DELETE FROM "' + table.name + '" WHERE "' + table.primary_key + '" = $1 ', [_id])
.then(result => {
return res.json("deleted_"+ result.rowCount + "_records");
})
.catch(error => {
console.log(error);
return res.status(500).send('500: Internal Server Error');
});
};
let router = express.Router();
router.post('/', create);
router.get('/', readMany);
router.get('/:_id', readOne);
router.put('/:_id', update);
router.delete('/:_id', remove);
if (render_pages) {
router.get('/del/:_id', remove);
router.post('/upd/:_id', update);
}
return router;
}