Created
May 7, 2014 15:59
-
-
Save ebaizel/47f508b1defbfc4022c6 to your computer and use it in GitHub Desktop.
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
mongoose = require('mongoose') | |
Schema = mongoose.Schema | |
artistSchema = require('../models2').artistSchema | |
models = require('../models2') | |
module.exports = (app) -> | |
getDB = -> | |
db = mongoose.createConnection(app.get('dburi')) | |
db.on 'error', console.error.bind(console, 'connection error:') | |
return db | |
closeDB = (db) -> | |
db.close() | |
# Admin routes | |
app.all '/admin/*', @.authed | |
app.get '/admin', (req, res, nextFn) -> | |
res.redirect '/admin/radio' | |
app.get '/admin/artists', (req, res, nextFn) -> | |
db = getDB() | |
Artist = db.model 'Artist', artistSchema | |
Artist.find({}).limit(10).exec (err, artists) -> | |
closeDB(db) | |
if err? | |
console.log 'JSON in the house' | |
res.render 'admin/artists.jade' | |
else | |
res.format | |
'text/plain': -> | |
console.log 'text' | |
res.render 'admin/artists.jade', { artists: artists }, | |
'text/html': -> | |
console.log 'html' | |
res.render 'admin/artists.jade', { artists: artists } | |
'application/json': -> | |
console.log 'json' | |
#console.log 'artists are ' + JSON.stringify(artists) | |
res.json artists | |
# app.post '/admin/artists', (req, res, nextFn) -> | |
# console.log('POSTing a new artist with body: ' + JSON.stringify(req.body)) | |
# artist = {} | |
# artist.name = req.body.name | |
# if req.body.artistId? | |
# artist._id = req.body.artistId | |
# getData.createArtist artist, (err, newArtist) -> | |
# if err? | |
# req.flash('error', 'error occurred and artist was not added') | |
# res.redirect('/artists') | |
# else | |
# req.flash('info', 'artist was added') | |
# res.redirect('/artists') | |
app.get '/admin/artists/:id', (req, res, nextFn) -> | |
db = getDB() | |
artistid = req.params.id | |
Artist = db.model('Artist', artistSchema) | |
Artist.findOne({'_id': artistid}).exec (err, artist) -> | |
closeDB db | |
if err? | |
req.flash('error', 'error getting artist') | |
res.redirect('/admin/artists') | |
else | |
closeDB db | |
console.log JSON.stringify artist | |
res.json artist | |
app.get '/admin/artists/:id/songs', (req, res, nextFn) -> | |
db = getDB() | |
artistid = req.params.id | |
Song = db.model('Song', songSchema) | |
Song.find {'artist' : artistid}, (err, songs) -> | |
closeDB db | |
if err? | |
res.redirect('/admin/artists') | |
else | |
res.json songs | |
app.get '/admin/artists/:id/shows', (req, res, nextFn) -> | |
db = getDB() | |
artistid = req.params.id | |
Show = db.model('Show', showSchema) | |
artistarray = new Array() | |
artistarray.push(artistid) | |
Show.find({'artist' : { $in : artistarray}}).populate('venue').exec (err, shows) -> | |
console.log("shows are " + JSON.stringify(shows)) | |
closeDB db | |
if err? | |
res.redirect('/admin/artists') | |
else | |
res.json shows | |
authed = (req, res, nextFn) -> | |
header = req.headers['authorization']||'' # get the header | |
token = header.split(/\s+/).pop()||'' # and the encoded auth token | |
auth = new Buffer(token, 'base64').toString() # convert from base64 | |
parts = auth.split(/:/) # split on colon | |
username = parts[0] | |
pass = parts[1] | |
if username == 'bugsy' && pass == 'bugsy123' | |
nextFn() | |
else | |
res.setHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"") | |
res.writeHead(401, {'Content-Type': 'text/html'}) | |
res.end('you need to login') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment