Skip to content

Commit

Permalink
ability to save/retrieve/delete/hit bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
rixth committed Nov 15, 2010
1 parent a2d7170 commit 6c783c0
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions app.js
Expand Up @@ -15,7 +15,8 @@ var express = require('express'),
app = module.exports = express.createServer();
mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://' + config.mongo.host + '/' + config.mongo.database),
sys = require('sys');
sys = require('sys'),
multipart = require('multipart');

mongoose.model('Item', {
properties: ['key', 'name', 'type', 'views', 'remote_url', 'created_at', 'updated_at'],
Expand Down Expand Up @@ -43,25 +44,33 @@ mongoose.model('Item', {
return 'http://my.cl.ly/images/item_types/' + this.type + '.png'
},
jsonObject: function () {
return {
var returnObject = {
'href': this.adminUrl,
'name': this.name,
'url': this.playerUrl,
'content_url': this.contentUrl,
'item_type': this.type,
'view_counter': this.views,
'icon': this.icon,
'remote_url': this.s3_url,
'created_at': this.created_at.toString(),
'updated_at': this.updated_at.toString()
}

if (this.type === 'bookmark') {
returnObject.redirect_url = this.remote_url;
} else {
returnObject.remote_url = this.remote_url;
returnObject.content_url = this.contentUrl;
}

return returnObject;
}
},

methods: {
save: function(fn){
if (this.isNew) {
this.created_at = new Date();
this.views = 0;
}
this.updated_at = new Date();
this.__super__(fn);
Expand All @@ -71,20 +80,21 @@ mongoose.model('Item', {

// Configuration

app.configure(function(){
app.configure(function (){
app.set('views', __dirname + '/views');
app.use(express.bodyDecoder());
app.use(express.methodOverride());
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(app.router);
app.use(express.staticProvider(__dirname + '/public'));
app.use(express.logger());
});

app.configure('development', function(){
app.configure('development', function (){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
app.configure('production', function (){
app.use(express.errorHandler());
});

Expand All @@ -99,7 +109,8 @@ app.get('/', function(req, res){
});

app.get('/items', function (req, res) {
db.model('Item').find().all(function (results) {
var search = req.param('filter') ? {type: req.param('filter')} : {};
db.model('Item').find(search).all(function (results) {
var returnData = [];

results.forEach(function (result) {
Expand All @@ -112,15 +123,40 @@ app.get('/items', function (req, res) {
});

app.post('/items', function (req, res){
res.send('create bookmark');
var url = req.rawBody.split('-------NPRequestBoundary-----')[1].split("\n")[3].trim(),
name = req.rawBody.split('-------NPRequestBoundary-----')[2].split("\n")[3].trim(),
bookmark = new (db.model('Item'));

if (name && url) {
bookmark.name = name;
bookmark.key = (Math.random() + '').substr(3);
bookmark.type = 'bookmark';
bookmark.remote_url = url;
bookmark.save(function () {
res.header('Content-type', 'application/json');
res.send(JSON.stringify(bookmark.jsonObject)));
});
} else {
res.send(400);
}
});

app.get('/items/new', function (req, res){
res.send('new item data');
});

app.delete('/items', function (req, res){
res.send('get data');
app.delete('/items/:key', function (req, res){
db.model('Item').find({key: req.param('key')}).one(function (result) {
if (result) {
result.remove();
if (result.type !== 'bookmark') {
// TODO delete from s3
}
res.send(200);
} else {
res.send(404);
}
});
});

app.get('/items', function (req, res){
Expand All @@ -131,7 +167,9 @@ app.get('/:key', function (req, res){
db.model('Item').find({key: req.param('key')}).one(function (result) {
if (result) {
if (result.type === 'bookmark') {
req.redirect(result.remote_url)
result.views++;
result.save();
res.redirect(result.remoteUrl)
} else {
res.render(result.type + '.jade', {
layout: 'player',
Expand All @@ -150,6 +188,8 @@ app.get('/:key', function (req, res){
app.get('/:key/content', function (req, res){
db.model('Item').find({key: req.param('key')}).one(function (result) {
if (result) {
result.views++;
result.save();
res.redirect(result.remoteUrl);
} else {
res.send(404);
Expand Down

0 comments on commit 6c783c0

Please sign in to comment.