Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
Initial post
  • Loading branch information
lenanex committed Feb 16, 2016
0 parents commit 1998724
Show file tree
Hide file tree
Showing 15 changed files with 695 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/globals.js
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'
}
}
75 changes: 75 additions & 0 deletions lib/model/model-albums.js
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;

64 changes: 64 additions & 0 deletions lib/model/model-photos.js
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;
89 changes: 89 additions & 0 deletions lib/model/model-users.js
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;
18 changes: 18 additions & 0 deletions package.json
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"
}
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
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;
}
62 changes: 62 additions & 0 deletions routes/albums.js
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;
9 changes: 9 additions & 0 deletions routes/index.js
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;
Loading

0 comments on commit 1998724

Please sign in to comment.