crunch-node/db.js

175 lines
5.5 KiB
JavaScript

var sqlite = require('sqlite3').verbose();
var async = require('async');
var config = require('./config.js').config;
var db = new sqlite.Database(config.dbPath);
// Function to get a count of current galleries
// Returns count of galleries
exports.countGalleries = function(cb) {
db.get('SELECT count(id) as count FROM galleries;', function(err, count) {
cb(count.count);
});
}
// Function to get a count of current photos
// Returns count of tags
exports.countPhotos = function(cb) {
db.get('SELECT count(id) as count FROM photos;', function(err, count) {
cb(count.count);
});
}
// Function to tag a photo with a list of tag names
// Inputs: Photo ID, List of Tag names
// Does not return.
exports.tagPhoto = function (photoId, tags) {
console.log('Deleting old tags');
db.run('DELETE FROM photoTags WHERE photoId = '+photoId+';',
function(err) {
console.log('Old tags deleted');
for (var i = 0, size = tags.length; i < size; i++) {
getOrCreateTag(tags[i], function(row) {
db.run('INSERT INTO photoTags (photoId, tagId) \
VALUES ('+photoId+', '+row.id+');',
function(err) {
if (err) {
console.log(err);
}
else {
console.log('Photo '+photoId+' tagged as '+row.name);
}
});
});
}
});
}
// Function to get tag ids associated with a particular photo id
// Inputs: Photo ID
// Returns: Records associated with tags of that photo id
var getPhotoTags = function (photoId, cb) {
db.all('SELECT tagId FROM photoTags WHERE photoId = '+photoId+';',
function(err, rows) {
if (err) {
console.log(err);
}
else {
var tagList = [];
for (row in rows) {
tagList.push(rows[row].tagId);
}
tagList = tagList.join(', ');
console.log('Tag ids for photo '+photoId+': '+tagList);
db.all('SELECT * FROM tags WHERE id IN ('+tagList+');',
function(err, rows) {
if (err) {
console.log(err);
}
else {
cb(rows);
}
})
}
})
}
exports.getPhotoTags = getPhotoTags;
// Function that returns all the photo tags as a space separated string.
// Inputs: Photo ID, callback function.
// Returns: callback of string with all tags separated by spaces.
var getPhotoTagsAsString = function(photoId, cb) {
getPhotoTags(photoId, function(tags) {
var str = false;
for (tag in tags) {
if (!str) {
str = tags[tag].name;
}
else {
str = str + ' ' + tags[tag].name;
}
}
console.log("Tags for photo "+photoId+": "+str);
cb(str);
})
}
exports.getPhotoTagsAsString = getPhotoTagsAsString;
// Function to get a photo record by the id of the photo
// Returns the photo record
exports.getPhotoById = function(id, cb) {
db.get('SELECT * FROM photos WHERE id = ?', id, function(err, row) {
if (err) console.log(err);
cb(row);
});
}
// Function to create a new photo
// Returns the photo id
exports.createPhoto = function(title, slug, markdown, extension, mimetype, photoDate, updatedDate,
createDate, path, cb) {
console.log('Creating new photo: '+slug);
db.run('INSERT INTO photos (\
title, \
slug, \
description, \
path, \
extension, \
mimetype, \
photoDate, \
createDate, \
updatedDate, \
lastGenerateDate, \
lastUpload) \
VALUES (\
"'+title+'", \
"'+slug+'", \
"'+markdown+'", \
"'+path+'", \
"'+extension+'", \
"'+mimetype+'", \
"'+photoDate+'", \
"'+createDate+'", \
"'+updatedDate+'", \
(strftime("%s","1900-01-01 00:00")*1000), \
(strftime("%s","1900-01-01 00:00")*1000));',
function(err, row) {
if (!err) {
console.log('Photo '+slug+' created.');
}
else {
console.log('Error: '+err);
}
db.get('SELECT * FROM photos WHERE title = "'+title+'" AND createDate = '+createDate+';', function(err, row){
if (err) console.log(err);
cb(err, row);
})
});
}
// Function to update an existing post record
// Returns the post object
exports.updatePhoto = function(id, title, slug, markdown, photoDate, cb) {
console.log("updatePost called.");
db.run('UPDATE photos SET \
title = "'+title+'", \
slug = "'+slug+'", \
description = "'+markdown+'", \
photoDate = '+helper.dateToEpoch(photoDate)+', \
updatedDate = '+helper.dateToEpoch(new Date())+' \
WHERE id = '+id+';',
function(err) {
console.log('updatePhoto UPDATE result: '+err);
db.get('SELECT * FROM photos WHERE id = '+id+';', function(err, row) {
console.log('updatePhoto SELECT result: ' + err);
cb(row);
});
});
}