-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial post
- Loading branch information
0 parents
commit 1998724
Showing
15 changed files
with
695 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = { | ||
applicationPort : 80, | ||
database : | ||
{ | ||
host : 'localhost', | ||
port : 8889, | ||
database : 'photoalbums', | ||
user : 'root', | ||
password : 'root' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
var mysql = require('mysql'); | ||
var globals = require('./../globals'); | ||
var connection = mysql.createConnection(globals.database); | ||
|
||
function createAlbum(params, callback){ | ||
var query = 'INSERT INTO albums SET ? '; | ||
connection.query(query, params, function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
var response = { | ||
id : rows.insertId, | ||
title : params.title | ||
}; | ||
callback(null, response); | ||
} | ||
}); | ||
} | ||
|
||
function getAlbumsByUser(params, callback){ | ||
var query = 'SELECT albumID, title FROM albums WHERE userID=' + connection.escape(params.userID); | ||
connection.query(query, function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
callback(null, rows); | ||
} | ||
}); | ||
} | ||
|
||
function getAlbumByID(params, callback){ | ||
var query = 'SELECT title, albumID, userID FROM albums WHERE albumID=' + connection.escape(params.albumID); | ||
connection.query(query, function(err, rows, fields){ | ||
if(rows.length > 0){ | ||
getPhotosForAlbum(rows[0], function(err, obj){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
callback(null, obj); | ||
} | ||
}); | ||
} else { | ||
callback(null, []); | ||
} | ||
}); | ||
} | ||
|
||
function deleteAlbum(params, callback){ | ||
var query = 'UPDATE albums SET published=0 WHERE albumID=' + connection.escape(params.albumID); | ||
connection.query(query, function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
callback(null, {message: 'Album deleted successfully'}); | ||
} | ||
}); | ||
} | ||
|
||
function getPhotosForAlbum(album, callback){ | ||
var modelPhotos = require('./model-photos'); | ||
modelPhotos.getPhotosByAlbumID(album, function(err, obj){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
album.photos = obj; | ||
callback(null, album); | ||
} | ||
}); | ||
} | ||
|
||
exports.createAlbum = createAlbum; | ||
exports.deleteAlbum = deleteAlbum; | ||
exports.getAlbumsByUser = getAlbumsByUser; | ||
exports.getAlbumByID = getAlbumByID; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var mysql = require('mysql'); | ||
var globals = require('./../globals'); | ||
var connection = mysql.createConnection(globals.database); | ||
|
||
function createPhoto(params, callback){ | ||
var query = 'INSERT INTO photos SET ? '; | ||
connection.query(query, params, function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
var response = { | ||
id : rows.insertId | ||
}; | ||
callback(null, response); | ||
} | ||
}); | ||
|
||
} | ||
|
||
function getPhotoByID(params, callback){ | ||
var query = 'SELECT photoID, caption, albumID, userID FROM photos WHERE published=1 AND photoID=' + connection.escape(params.photoID); | ||
connection.query(query, function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
if(rows.length > 0){ | ||
callback(null, rows); | ||
} else { | ||
callback(null, []); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function getPhotosByAlbumID(params, callback){ | ||
var query = 'SELECT photoID, caption, albumID, userID FROM photos WHERE published=1 AND albumID=' + connection.escape(params.albumID); | ||
connection.query(query, function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
if(rows.length > 0){ | ||
callback(null, rows); | ||
} else { | ||
callback(null, []); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function deletePhotoByID(params, callback){ | ||
var query = 'UPDATE photos SET published=0 WHERE photoID=' + connection.escape(params.photoID); | ||
connection.query(query, function(err, rows, fields){ | ||
if(rows.length > 0){ | ||
callback(null, rows); | ||
} else { | ||
callback(null, []); | ||
} | ||
}); | ||
} | ||
|
||
exports.createPhoto = createPhoto; | ||
exports.getPhotoByID = getPhotoByID; | ||
exports.getPhotosByAlbumID = getPhotosByAlbumID; | ||
exports.deletePhotoByID = deletePhotoByID; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
var mysql = require('mysql'); | ||
var globals = require('./../globals'); | ||
var connection = mysql.createConnection(globals.database); | ||
|
||
function getAllUsers(callback){ | ||
connection.query('SELECT username, userID FROM users', function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
callback(null, rows); | ||
} | ||
}); | ||
} | ||
|
||
function getUser(params, callback){ | ||
connection.query('SELECT username, userID FROM users WHERE username=' + connection.escape(params.username), function(err, rows, fields){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
if(rows.length > 0){ | ||
var userObject = rows[0]; | ||
var modelAlbums = require('./model-albums'); | ||
modelAlbums.getAlbumsByUser({userID: userObject.userID}, function(err, obj){ | ||
if(err){ | ||
callback(err); | ||
} else { | ||
userObject.albums = obj; | ||
callback(null, userObject); | ||
} | ||
}); | ||
} else { | ||
callback(null, []); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function createUser(params, callback){ | ||
|
||
var newUser = { | ||
username: params.username, | ||
password: params.password, | ||
email: params.email | ||
} | ||
|
||
var query = 'INSERT INTO users SET ? '; | ||
|
||
connection.query(query, newUser, function(err, rows, fields) { | ||
if (err) { | ||
if(err.errno == 1062){ | ||
var error = new Error("This username has already been taken."); | ||
callback(error); | ||
} else { | ||
callback(err); | ||
} | ||
} else { | ||
callback(null, {message:'Registration successful!'}); | ||
} | ||
}); | ||
|
||
} | ||
|
||
function loginUser(params, callback){ | ||
connection.query('SELECT username, password, userID FROM users WHERE username=' + connection.escape(params.username), function(err, rows, fields) { | ||
if(err){ | ||
callback(err); | ||
} else if(rows.length > 0){ | ||
var response = { | ||
username: rows[0].username, | ||
userID: rows[0].userID | ||
} | ||
callback(null, response); | ||
} else { | ||
var error = new Error("Invalid login"); | ||
callback(error); | ||
} | ||
}); | ||
} | ||
|
||
|
||
function logoutUser(params, callback){ | ||
callback({message: 'You have logged out successfully'}); | ||
} | ||
|
||
exports.getAllUsers = getAllUsers; | ||
exports.getUser = getUser; | ||
exports.createUser = createUser; | ||
exports.loginUser = loginUser; | ||
exports.logoutUser = logoutUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "photoalbums", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node server.js" | ||
}, | ||
"dependencies": { | ||
"express": "~4.8.6", | ||
"body-parser": "~1.6.6", | ||
"cookie-parser": "~1.3.2", | ||
"mysql": "2.0.*", | ||
"morgan": "~1.2.3", | ||
"serve-favicon": "~2.0.1", | ||
"debug": "~1.0.4", | ||
"jade": "~1.5.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
body { | ||
padding: 50px; | ||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; | ||
} | ||
|
||
a { | ||
color: #00B7FF; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
var model = require('./../lib/model/model-albums'); | ||
|
||
/* GET album by ID */ | ||
router.get('/id/:albumID', function(req, res) { | ||
if(req.param('albumID')){ | ||
var params = { | ||
albumID : req.param('albumID') | ||
} | ||
model.getAlbumByID(params, function(err, obj){ | ||
if(err){ | ||
res.status(400).send({error: 'Invalid album ID'}); | ||
} else { | ||
res.send(obj); | ||
} | ||
}); | ||
} else { | ||
res.status(400).send({error: 'Invalid album ID'}); | ||
} | ||
}); | ||
|
||
/* POST create album. */ | ||
router.post('/upload', function(req, res) { | ||
if(req.param('title') && req.param('userID')){ | ||
var params = { | ||
userID : req.param('userID'), | ||
title : req.param('title') | ||
} | ||
model.createAlbum(params, function(err, obj){ | ||
if(err){ | ||
res.status(400).send({error: 'Invalid album data'}); | ||
} else { | ||
res.send(obj); | ||
} | ||
}); | ||
} else { | ||
res.status(400).send({error: 'Invalid album data'}); | ||
} | ||
}); | ||
|
||
/* POST delete album. */ | ||
router.post('/delete', function(req, res) { | ||
if(req.param('albumID')){ | ||
var params = { | ||
albumID : req.param('albumID') | ||
} | ||
model.deleteAlbum(params, function(err, obj){ | ||
if(err){ | ||
res.status(400).send({error: 'Album not found'}); | ||
} else { | ||
res.send(obj); | ||
} | ||
}); | ||
} else { | ||
res.status(400).send({error: 'Invalid album ID'}); | ||
} | ||
}); | ||
|
||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
|
||
/* GET home page. */ | ||
router.get('/', function(req, res) { | ||
res.render('index', { title: 'Photoalbums' }); | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.