diff --git a/app.js b/app.js index 7331a02..69ec775 100644 --- a/app.js +++ b/app.js @@ -19,8 +19,6 @@ Post = require('./post.js'); User = require('./user.js'); Category = require('./category.js'); Static = require('./static.js'); -var database = require('./db.js'); -var genPhotos = require('./genPhotos.js'); // Get config variables var config = require('./config.js').config; diff --git a/db.js b/db.js deleted file mode 100644 index d55b972..0000000 --- a/db.js +++ /dev/null @@ -1,175 +0,0 @@ -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); - }); - }); -} diff --git a/package.json b/package.json index 8201701..3e6477f 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "passport": "^0.3.0", "passport-local": "^1.0.0", "redis": "^2.4.2", - "serve-favicon": "^2.3.0", - "sqlite3": "^3.1.0" + "serve-favicon": "^2.3.0" } } diff --git a/schema.sql b/schema.sql deleted file mode 100644 index 748bc5f..0000000 --- a/schema.sql +++ /dev/null @@ -1,137 +0,0 @@ --- SQL Schema for Crunch - --- Note all dates must be in epoch seconds to allow for sqlite comparison - --- User table - -DROP TABLE IF EXISTS "users"; - -CREATE TABLE "users" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "username" TEXT NOT NULL UNIQUE, - "password" TEXT NOT NULL, -- sha256 hash of plaintext password + salt - "salt" TEXT NOT NULL, -- salt that is appended to the password - "email" TEXT UNIQUE, - "displayName" TEXT, - "createDate" TEXT -); - --- Content Tables - -DROP TABLE IF EXISTS "posts"; - -CREATE TABLE "posts" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "title" TEXT, -- The title of the post - "slug" TEXT NOT NULL, -- A slug form of the title or id - "markdown" TEXT NOT NULL, -- The post content in markdown format - "postDate" INTEGER, -- The date that the post should go live and be sorted by - "deleted" INTEGER DEFAULT 0, -- Whether or not the post is "Deleted" - "published" INTEGER DEFAULT 0, -- Whether or not the post has been published - "updatedDate" INTEGER, -- The date that the post was last updated - "createDate" INTEGER NOT NULL, -- The date the post was created - "lastGenerateDate" INTEGER, -- The date this post was last built - "lastUpload" INTEGER -- The date the post was last uploaded -); - -DROP TABLE IF EXISTS "galleries"; - -CREATE TABLE "galleries" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "title" TEXT, -- The title of the gallery - "slug" TEXT NOT NULL UNIQUE, -- A slug form of the title or id - "description" TEXT NOT NULL, -- The gallery description in markdown format - "deleted" INTEGER DEFAULT 0, -- Whether or not the gallery has been deleted - "published" INTEGER DEFAULT 0, -- Whether or not the gallery has been published - "postDate" INTEGER, -- The date that the gallery should go live and be sorted by - "updatedDate" INTEGER, -- The date that the gallery was last updated - "createDate" INTEGER NOT NULL, -- The date the gallery was created - "lastGenerateDate" INTEGER, -- The date this gallery was last built - "lastUpload" INTEGER -- The date the gallery was last uploaded -); - -DROP TABLE IF EXISTS "photos"; - -CREATE TABLE "photos" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "path" TEXT NOT NULL, -- The path of the photo location on disk - "title" TEXT, -- The title of the photo - "slug" TEXT NOT NULL UNIQUE, -- A slug form of the title or id - "description" TEXT, -- The photo description in markdown format - "published" INTEGER DEFAULT 0, -- Whether or not the photo has been published - "deleted" INTEGER DEFAULT 0, -- Whether or not the photo has been deleted - "photoDate" INTEGER, -- The date that the photo should go live and be sorted by - "updatedDate" INTEGER, -- The date that the photo was last updated - "createDate" INTEGER NOT NULL, -- The date the photo was created - "lastGenerateDate" INTEGER, -- The date this photo was last built - "lastUpload" INTEGER, -- The date the photo was last uploaded - "extension" TEXT, -- The original extension of the photo - "mimetype" TEXT, -- The original mimetype of the photo - "orientation" TEXT, -- The correct orientation of the photo (landscape, portrait) - "origWidth" INTEGER, -- The original width of the photo - "origHeight" INTEGER, -- The original height of the photo - "aperture" REAL, -- The aperture of the original photo - "speed" REAL, -- The shutter speed of the original photo (in seconds) - "location" TEXT -- The location data of the original photo -); - -DROP TABLE IF EXISTS "galleryPhotos"; - -CREATE TABLE "galleryPhotos" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "galleryId" INTEGER NOT NULL, -- the id of the gallery - "photoId" INTEGER NOT NULL, -- the id of the photo - FOREIGN KEY(galleryId) REFERENCES galleries(id), - FOREIGN KEY(photoId) REFERENCES photos(id) -); - -DROP TABLE IF EXISTS "categories"; - -CREATE TABLE "categories" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "name" TEXT NOT NULL, -- The name of this category - "slug" TEXT NOT NULL UNIQUE, -- A slug form of the name or id - "description" TEXT, -- A brief description of the category in markdown - "createDate" INTEGER NOT NULL -- The date the category was created -); - -INSERT INTO "categories" VALUES (1, "Uncategorized", "uncategorized", "Uncategorized posts", (strftime('%s','now')*1000)); - -DROP TABLE IF EXISTS "categoryPosts"; - -CREATE TABLE "categoryPosts" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "categoryId" INTEGER NOT NULL, -- the id of the category - "postId" INTEGER NOT NULL, -- the id of the post - FOREIGN KEY(categoryId) REFERENCES categories(id), - FOREIGN KEY(postId) REFERENCES posts(id) -); - -DROP TABLE IF EXISTS "tags"; - -CREATE TABLE "tags" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "name" TEXT NOT NULL, -- The name of this tag - "slug" TEXT NOT NULL UNIQUE, -- A slug form of the name or id - "createDate" INTEGER NOT NULL -- The date the tag was created -); - -DROP TABLE IF EXISTS "postTags"; - -CREATE TABLE "postTags" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "postId" INTEGER NOT NULL, -- the id of the post - "tagId" INTEGER NOT NULL, -- the id of the tag - FOREIGN KEY(postId) REFERENCES posts(id), - FOREIGN KEY(tagId) REFERENCES tags(id) -); - -DROP TABLE IF EXISTS "photoTags"; - -CREATE TABLE "photoTags" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "photoId" INTEGER NOT NULL, -- the id of the photo - "tagId" INTEGER NOT NULL, -- the id of the tag - FOREIGN KEY(photoId) REFERENCES photos(id), - FOREIGN KEY(tagId) REFERENCES tags(id) -);