Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

here #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

here #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/cli/shrinkwrap#caveats
node_modules

# Debug log from npm
npm-debug.log
72 changes: 72 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');


var app = express();
module.exports = app;


var massive = require("massive")
var connectionString = "postgres://localhost/mediaranker"
// + app.get('env');
var db = massive.connectSync({connectionString : connectionString});

app.set("db", db);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));

var routes = require('./routes/index');
var movies = require('./routes/movies');
var albums = require('./routes/albums');
var books = require('./routes/books');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/movies', movies);
app.use('/books', books);
app.use('/albums', albums);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('MediaRanker:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
111 changes: 111 additions & 0 deletions controllers/albums.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
var Album = require("../models/album");

var AlbumController = {
all: function(req, res, next) {
Album.all(function(error, albums) {
if(error) {
var err = new Error("Error retrieving albums list:\n" + error.message);
err.status = 500;
next(err);
} else {
res.render('index', { title: 'Albums' , stuff: albums});
}
});
},

item: function(req, res, next) {
Album.item([Number(req.params.id)], function(error, item) {
if(error) {
var err = new Error("Error retrieving album:\n" + error.message);
err.status = 500;
next(err);
} else {
// console.log(item)
res.render('item', { title: 'Albums' , person: 'Artist', stuff: item});
}
})
},

upvote: function(req, res, next) {
Album.upvote([Number(req.params.id)], function(error, item) {
if(error) {
var err = new Error("Error retrieving album:\n" + error.message);
err.status = 500;
next(err);
} else {
Album.item([Number(req.params.id)], function(error, item) {
if(error) {
var err = new Error("Error retrieving album:\n" + error.message);
err.status = 500;
next(err);
} else {
res.redirect('http://localhost:3000/albums/'+req.params.id);
}
})
}
})
},

edit: function(req, res, next) {
Album.item([Number(req.params.id)], function(error, item) {
if(error) {
var err = new Error("Error retrieving album:\n" + error.message);
err.status = 500;
next(err);
} else {
console.log(item)
res.render('edit', { title: 'Albums', person: "Artist", stuff: item});
}
})
},

saveedit: function(req, res, next) {
// console.log(req.body)
Album.saveedit([req.body.id, req.body.name, req.body.person, req.body.description], function(error, item) {
if(error) {
var err = new Error("Error retrieving album:\n" + error.message);
err.status = 500;
next(err);
} else {
Album.item([Number(req.params.id)], function(error, item) {
if(error) {
var err = new Error("Error retrieving album:\n" + error.message);
err.status = 500;
next(err);
} else {
res.redirect('http://localhost:3000/albums/'+req.body.id);
}
})
}
})
},

delete: function(req, res, next) {
Album.delete([Number(req.params.id)], function(error, item) {
if(error) {
var err = new Error("Error retrieving album:\n" + error.message);
err.status = 500;
next(err);
} else {
// console.log(item)
res.redirect('http://localhost:3000/albums');
}
})
},

new: function(req, res, next) {
Album.new([req.body.name, req.body.person, req.body.description], function(error, item) {
if(error) {
var err = new Error("Error creating album:\n" + error.message);
err.status = 500;
next(err);
} else {
console.log(item)
res.redirect('http://localhost:3000/albums/'+item.id);
}
})
}
}


module.exports = AlbumController;
Loading