UIAngular/Server/src/routes/api/sermons.js

178 lines
6.8 KiB
JavaScript

var express = require('express');
var router = express.Router();
var fs = require('fs');
var dbSermons = require('../../database/sermons');
var storage = process.env['HOME'] + '/webapps/ofbstatic/media/sermons/';
var deletedStorage = process.env['HOME'] + '/webapps/ofbstatic/media/sermons/deleted/';
function removeFile(filepath){
fs.unlink(filepath,function(err){
if (err) throw err;
});
}
function renameFile(tempFilePath, finalFilePath,cb){
fs.rename(tempFilePath,finalFilePath,function(err){
cb(err);
});
}
function filenameFromPath(filepath){
return filepath.split('\\').pop().split('/').pop();
}
router.get("/search",function(req,res){
var pageSize = 5;
var page = 1;
var search = '';
if (req.query.pageSize) pageSize = parseInt(req.query.pageSize);
if (req.query.page) page = parseInt(req.query.page);
if (req.query.searchTerm) search = req.query.searchTerm;
dbSermons.searchSermons(pageSize,page,search,function(err,sermons){
if (err){
res.status(500).json({"status":500,"message":"Error processing request","error":err});
return;
}
res.status(200).json({"status":200,"sermons":sermons,"page":page,"pageSize":pageSize > sermons.length ? sermons.length : pageSize, "searchTerm":search});
});
});
router.get("/:id",function(req,res){
dbSermons.getSermon(req.params.id,function(error,sermon){
if (error){
res.status(404).json({"status":404,"message":"Error processing request"});
return;
}
if (sermon == null){
res.status(404).json({"status":404,"message":"Sermon does not exist"});
return;
}
res.status(200).json({"status":200,"sermon":sermon});
});
});
router.get("/download/:id",function(req,res){
var finalStorage = '/home/ofbbutte/webapps/ofbstatic/media/';
dbSermons.getSermon(req.params.id,function(error,sermon){
if (error){
res.status(404).json({"status":404,"message":"Error processing request"});
return;
}
if (sermon == null){
res.status(404).json({"status":404,"message":"Sermon does not exist"});
return;
}
var file = finalStorage + sermon.file;
console.log(file);
if (fs.existsSync(file)){
res.download(file);
} else {
res.status(404).json({"status":404,"message":"Could not find the file associated with this sermon"});
}
});
});
router.get("/page/:page",function(req,res){
console.log("page");
var pageSize = 5;
if (req.query.pageSize){
pageSize = parseInt(req.query.pageSize);
}
dbSermons.getSermons(pageSize,req.params.page,function(err,sermons){
if (err){
res.status(500).json({"status":500,"message":"Error processing request","error":err});
return;
}
res.status(200).json({"status":200,"sermons":sermons,"page":req.params.page,"pageSize":pageSize > sermons.length ? sermons.length : pageSize});
});
});
router.post("/a/", function(req,res){
console.log(req.body);
if (!req.body.date || !req.body.title || !req.body.author || !req.body.description || !req.body.file){
removeFile(req.body.tmpPath);
res.status(400).json({"status":400,"message":"date, title, author, file, and description are required fields in the body"});
return;
}
req.body.file = "sermons/" + req.body.file;
dbSermons.insertSermon(req.body.date,req.body.title,req.body.timezoneOffset,req.body.author,req.body.description,req.body.file,function(error,sermon){
if (error){
removeFile(req.body.tmpPath);
res.status(500).json({"status":500,"message":"There was an error inserting the sermon"});
return;
}
if (sermon == null){
removeFile(req.body.tmpPath);
res.status(404).json({"status":404,"message":"Sermon does not exist"});
return;
}
/////******************************
/////Add the sermon to the postgres db for the existing site
var pg = require("../database/postgres-for-old-db/addSermon");
pg.addSermon(new Date(),req.body.title,req.body.author,req.body.description,req.body.file,function(error){
renameFile(req.body.tmpPath,req.body.finalPath,function(err){
if (err){
removeFile(req.body.tmpPath);
res.status(500).json({"status":500,"message":"Could not rename the temp file"});
} else {
res.status(201).json({"status":201,"sermon":sermon});
}
});
});
});
});
router.put("/a/",function(req,res){
console.log(req.body);
if (!req.body.id || !req.body.date || !req.body.title || !req.body.author || !req.body.description ){
res.status(400).json({"status":400,"message":"id, date, title, author, and description are required fields in the body"});
return;
}
dbSermons.updateSermon(req.body.id,req.body.date,req.body.title,req.body.author,req.body.description,function(error,result){
if (error){
res.status(500).json({"status":500,"message":"There was an error updating the sermon"});
return;
}
if (result.sermon == null){
res.status(404).json({"status":404,"message":"Sermon does not exist"});
return;
}
res.status(200).json({"status":200,"changedRows":result.changedRows,"sermon":result.sermon});
});
});
router.delete("/a/",function(req,res){
if (!req.body.id){
res.status(400).json({"status":400,"message":"id of the sermon needs to be supplied to delete"});
return;
}
dbSermons.deleteSermon(req.body.id,function(error,result){
if (error){
res.status(500).json({"status":500,"message":"There was an error deleting the sermon"});
return;
}
if (result.affectedRows > 0){
/////******************************
/////Add the sermon to the postgres db for the existing site
var pg = require("../database/postgres-for-old-db/addSermon");
pg.deleteSermon(result.sermon.file,function(error){
console.log(error);
var cPath = storage + filenameFromPath(result.sermon.file);
var dPath = deletedStorage + filenameFromPath(result.sermon.file);
renameFile(cPath,dPath,function(error){
res.status(200).json({"status":200,"affectedRows":result.affectedRows});
});
});
} else {
res.status(200).json({"status":200,"affectedRows":result.affectedRows});
}
});
});
module.exports = router;