Skip to content

Commit bac4e91

Browse files
authored
Merge pull request #80 from Human-Connection/77-implement-delete-entry-method
Implement deleteEntry method & getEntry method
2 parents 8ec2b90 + c10bf8f commit bac4e91

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

core/db.js

+46
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ exports.getEntries = function(filter, callback){
8282
});
8383
};
8484

85+
exports.getEntry = function(id, callback){
86+
pool.getConnection(function(error, connection) {
87+
if(error) {
88+
console.log(error);
89+
callback(true);
90+
return;
91+
}
92+
93+
let sql = 'SELECT * FROM entries WHERE id = ?';
94+
95+
connection.query(sql, [id], function(error, result) {
96+
connection.release();
97+
if(error) {
98+
callback(result, error);
99+
return;
100+
}
101+
if (!result || !result.length) {
102+
callback(null, 'Entry with the specified id does not exist');
103+
return;
104+
}
105+
callback(result, false);
106+
});
107+
});
108+
};
109+
85110
exports.getUserByHash = function(hash, callback){
86111
pool.getConnection(function(err, connection) {
87112
if (err) { console.log(err); callback(true); return; }
@@ -113,6 +138,27 @@ exports.verifyEntry = function(hash, callback){
113138
});
114139
};
115140

141+
exports.deleteEntry = function(id, callback){
142+
pool.getConnection(function(error, connection) {
143+
if(error) {
144+
console.log(error);
145+
callback(error);
146+
return;
147+
}
148+
let sql = "DELETE FROM entries WHERE id = ?";
149+
150+
// make the query
151+
connection.query(sql, [id], function(error) {
152+
connection.release();
153+
if(error) {
154+
callback(error);
155+
return;
156+
}
157+
callback(false);
158+
});
159+
});
160+
};
161+
116162
exports.getCount = function(callback){
117163
pool.getConnection(function(err, connection) {
118164
if(err) { console.log(err); callback(true); return; }

core/entryController.js

+21
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ exports.toggleStatus = function(req, res){
7878
});
7979
};
8080

81+
exports.deleteEntry = function(request, response){
82+
if(request.params.id && request.params.id > 0){
83+
db.getEntry(request.params.id, function(result, error){
84+
if(!error){
85+
db.deleteEntry(request.params.id, function(error){
86+
if(!error) {
87+
console.log("!error");
88+
response.status(200).json({success: true});
89+
} else{
90+
response.status(400).json({error: error});
91+
}
92+
});
93+
} else {
94+
response.status(400).json({error: error});
95+
}
96+
});
97+
} else {
98+
response.status(400).json({error: "No entry id specified"});
99+
}
100+
};
101+
81102
exports.getCount = function(req, res) {
82103
db.getCount(function(results, err){
83104
if(!err){

core/restapi.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = function(app) {
88
// non-auth routes
99
// TODO: tmp until path can be changed
1010
app.route('/cube.php').get(entryController.getCount);
11+
app.route('/count').get(entryController.getCount);
1112
app.route('/entries/verify/:k').get(entryController.verifyEntry);
1213

1314
// intercept request and check for api key
@@ -27,4 +28,5 @@ module.exports = function(app) {
2728
app.route('/entries/toggle').post(entryController.toggleStatus);
2829
app.route('/entries').get(entryController.getAll);
2930
app.route('/countries').get(entryController.getCountries);
31+
app.route('/delete/:id').get(entryController.deleteEntry);
3032
};

0 commit comments

Comments
 (0)