151 lines
5.7 KiB
JavaScript
151 lines
5.7 KiB
JavaScript
var connection = require("./connection");
|
|
var dbSermons = this;
|
|
|
|
exports.getSermon = function(sermonId, callback, includeDeleted){
|
|
var query = "SELECT * FROM Sermons WHERE Id = ? AND DeletedDate IS NULL;";
|
|
if (includeDeleted === true){
|
|
query = "SELECT * FROM Sermons WHERE Id = ?;";
|
|
}
|
|
connection.query(query,[sermonId],function(error,rows,fields){
|
|
if (error){
|
|
console.log(error);
|
|
callback(error);
|
|
return;
|
|
}
|
|
|
|
var sermon = null;
|
|
if (rows.length > 0){
|
|
sermon = {};
|
|
sermon.id = rows[0].Id;
|
|
sermon.date = rows[0].Date;
|
|
sermon.title = rows[0].Title;
|
|
sermon.author = rows[0].Author;
|
|
sermon.description = rows[0].Description;
|
|
sermon.file = rows[0].File;
|
|
}
|
|
callback(null,sermon);
|
|
});
|
|
}
|
|
|
|
|
|
exports.getSermons = function(pageSize, pageNumber,callback){
|
|
var offset = (pageNumber - 1) * pageSize;
|
|
connection.query("SELECT * FROM Sermons WHERE DeletedDate IS NULL ORDER BY Date DESC LIMIT ?,?;",[offset,pageSize],function(err,rows,fields){
|
|
if (err){
|
|
console.log(err);
|
|
callback(err);
|
|
return;
|
|
}
|
|
var sermons = [];
|
|
for (var i = 0; i < rows.length; i++){
|
|
var sermon = {};
|
|
sermon.id = rows[i].Id;
|
|
sermon.date = rows[i].Date;
|
|
sermon.title = rows[i].Title;
|
|
sermon.author = rows[i].Author;
|
|
sermon.description = rows[i].Description;
|
|
sermon.file = rows[i].File;
|
|
sermons.push(sermon);
|
|
}
|
|
callback(null,sermons);
|
|
});
|
|
};
|
|
|
|
exports.searchSermons = function(pageSize, pageNumber, searchTerm, callback){
|
|
var offset = (pageNumber - 1) * pageSize;
|
|
searchTerm = '%' + searchTerm + '%';
|
|
connection.query("SELECT * FROM Sermons WHERE DeletedDate IS NULL AND (Title LIKE ? OR Author LIKE ? OR Description LIKE ? OR Date LIKE ?) ORDER BY Id DESC LIMIT ?,?;",[searchTerm,searchTerm,searchTerm,searchTerm,offset,pageSize],function(err,rows,fields){
|
|
if (err){
|
|
console.log(err);
|
|
callback(err);
|
|
return;
|
|
}
|
|
var sermons = [];
|
|
for (var i = 0; i < rows.length; i++){
|
|
var sermon = {};
|
|
sermon.id = rows[i].Id;
|
|
sermon.date = rows[i].Date;
|
|
sermon.title = rows[i].Title;
|
|
sermon.author = rows[i].Author;
|
|
sermon.description = rows[i].Description;
|
|
sermon.file = rows[i].File;
|
|
sermons.push(sermon);
|
|
}
|
|
callback(null,sermons);
|
|
});
|
|
}
|
|
|
|
exports.insertSermon = function(dateOrSermonObject, titleOrCallbackFunction, timezoneOffset, author, description, file, callback){
|
|
if (typeof title === 'function'){
|
|
insertSermonFromObject(dateOrSermonObject,titleOrCallbackFunction);
|
|
return;
|
|
}
|
|
//Date is passed in as milliseconds from 1970 so we will convert it here
|
|
//Wrap it in another Date object to getTime so that we can make sure the date is in UTC format
|
|
dateOrSermonObject = new Date(new Date(+dateOrSermonObject).getTime());
|
|
connection.query("INSERT INTO Sermons (CreatedDate,ModifiedDate,Id,Date,Title,Author,Description,File) VALUES(?,CURRENT_TIMESTAMP,NULL,?,?,?,?,?)",[new Date,dateOrSermonObject,titleOrCallbackFunction,author,description,file],function(error,result){
|
|
if (error){
|
|
console.log(error);
|
|
callback(error);
|
|
return;
|
|
}
|
|
dbSermons.getSermon(result.insertId,function(error,sermon){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null,sermon);
|
|
});
|
|
});
|
|
}
|
|
|
|
function insertSermonFromObject(sermon, callback){
|
|
dbSermons.insertSermon(sermon.date,sermon.title,sermon.timezoneOffset,sermon.author,sermon.description,sermon.file,callback);
|
|
}
|
|
|
|
exports.updateSermon = function(sermonIdOrSermonObject, dateOrCallbackFunction, title, author, description, callback){
|
|
if (typeof dateOrCallbackFunction == 'function'){
|
|
updateSermonFromObject(sermonIdOrSermonObject,callback);
|
|
return;
|
|
}
|
|
dateOrCallbackFunction = new Date(dateOrCallbackFunction);
|
|
connection.query("UPDATE Sermons SET ModifiedDate = CURRENT_TIMESTAMP, Date = ?, Title = ?, Author = ?, Description = ? WHERE Id = ?",[dateOrCallbackFunction,title,author,description,sermonIdOrSermonObject],function(error,result){
|
|
if (error){
|
|
console.log(error);
|
|
callback(error);
|
|
return;
|
|
}
|
|
dbSermons.getSermon(sermonIdOrSermonObject,function(error,sermon){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null,{"changedRows":result.changedRows,"sermon":sermon});
|
|
});
|
|
});
|
|
};
|
|
|
|
function updateSermonFromObject(sermon, callback){
|
|
dbSermons.updateSermon(sermon.id,sermon.date,sermon.title,sermon.author,sermon.description,callback);
|
|
}
|
|
|
|
exports.deleteSermon = function(sermonIdOrSermonObject, callback){
|
|
if (typeof sermonIdOrSermonObject !== 'number'){
|
|
sermonIdOrSermonObject = sermonIdOrSermonObject.id;
|
|
}
|
|
connection.query("UPDATE Sermons SET DeletedDate = CURRENT_TIMESTAMP WHERE Id = ?;",[sermonIdOrSermonObject],function(error,result){
|
|
if (error){
|
|
console.log(error);
|
|
callback(error);
|
|
return;
|
|
}
|
|
dbSermons.getSermon(sermonIdOrSermonObject,function(error,sermon){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
console.log(sermon);
|
|
callback(null,{"affectedRows":result.affectedRows,"sermon":sermon});
|
|
},true);
|
|
});
|
|
} |