Skip to content

Commit

Permalink
Adding the ability to update items/categories through post requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgleason committed Aug 15, 2012
1 parent f02908e commit 5f73a65
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 14 deletions.
25 changes: 24 additions & 1 deletion lib/mongo/dbhelper.js
Expand Up @@ -47,6 +47,10 @@ exports.remove = function(db,docType,id, callback){
remove(db,docType,id, callback)
}

exports.updateByJSON = function(objJSON,changeJSON,db,docType,callback){
updateByJSON(objJSON,changeJSON,db,docType,callback);
}

getDatabase = function(){
if(typeof dbinfo.DBURL() !== 'string') console.log("URL is bad");
if(typeof dbinfo.DBPORT() !== 'number') console.log("PORT is bad");
Expand Down Expand Up @@ -129,9 +133,11 @@ findDocumentsMapped = function(db,docType,callback) {


findByIdentifier = function(db,docType,id, callback) {
console.log(id)
this.getCollection(db,docType,function(error, collection) {
if( error ) callback(error,null)
else {
console.log("ID is "+id)
collection.findOne({_id: collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
if( error ) callback(error)
else {
Expand All @@ -155,6 +161,23 @@ save = function(db, docType, doc, callback) {
});
};

/*
*Generic function to update
*objJSON: a JSON object that has the values to search for (similar to where in SQL)
*changeJSON: Update JSON (similar to set values)
*/
updateByJSON = function(objJSON,changeJSON,db,docType,callback){
var options = {safe:true,multi:true,};
getCollection(db, docType, function(error, collection) {
if(error != undefined){
callback(error,null)
}
else{
collection.update(objJSON,changeJSON,callback)
}
})
}

remove = function(db,docType,id, callback){
var id = db.bson_serializer.ObjectID.createFromHexString(id);
this.getCollection(db,docType,function(error, collection) {
Expand All @@ -166,4 +189,4 @@ remove = function(db,docType,id, callback){
});
}
})
};
}
4 changes: 4 additions & 0 deletions lib/mongo/document.js
Expand Up @@ -57,6 +57,10 @@ DocumentProvider.prototype.save = function(item, callback) {
dbhelper.save(this.db,this.TYPE,item,callback)
};

DocumentProvider.prototype.updateJSON = function(whereJSON, valuesJSON, callback){
dbhelper.updateByJSON(whereJSON, valuesJSON, this.db,this.TYPE, callback);
}

DocumentProvider.prototype.remove = function(id, callback){
dbhelper.remove(this.db,this.TYPE,id,callback)
};
Expand Down
124 changes: 112 additions & 12 deletions lib/mongo/rest.js
Expand Up @@ -50,6 +50,9 @@ exports.getImage = function(req, res){
fileProvider.streamFile(res, fileid, 'image/jpg')
}


/* Catalog Entries */

exports.getCatalogEntries= function (req, res, next){
documentProvider.setType('items')
documentProvider.findAllMapped(function (err, catalog) {
Expand Down Expand Up @@ -117,9 +120,7 @@ exports.getCategories= function (req, res, next){
})
}



exports.getItemsForCategory = function(req,res,next){
exports.getCategory = function(req,res,next){
documentProvider.setType('categories')
var objectId
try{
Expand All @@ -130,27 +131,69 @@ exports.getItemsForCategory = function(req,res,next){
return next()
}
documentProvider.findByJSON({_id:objectId}, function(err,categories){
if(err != null){
req.err = err
checkCategoryFromJSON(categories, err, function(err, category){
if(err != undefined){
req.category = category
}
else{
req.err = err
}
return next()
})
})
}

checkCategoryFromJSON = function(categories, err, callback){
var category;
var error;
if(err != null){
error = err
callback(error,category)
}
else if(categories == null || categories.length == 0){
req.err = "Category Undefined"
error = "Category Undefined"
console.log("Error Set")
return next()
callback(error,category)
}
else if(categories.length != 1){
req.err = "Two Categories Share the same ID, Cannot retrieve"
req.error = "Two Categories Share the same ID, Cannot retrieve"
console.log("Strange things are afoot: Two categories share the same ID")
return next()
callback(error,category)
}
else {
var category = categories[0]
category = categories[0]
getExplodedItemsForCategory(category,function(err){
req.category = category
return next()
callback(err,category)
})
}

}


exports.getItemsForCategory = function(req,res,next){
documentProvider.setType('categories')
var objectId
try{
objectId=new BSON.ObjectID(req.params.categoryId);
}
catch(exception){
req.err = exception
return next()
}
documentProvider.findByJSON({_id:objectId}, function(err,categories){
checkCategoryFromJSON(categories, err, function(err, category){
console.log("ERR is "+JSON.stringify(err))
if(err == undefined){
console.log("Category: "+category)
req.category = category
}
else{
console.log("else")
req.err = err
}
console.log("returning "+JSON.stringify(category))
return next()
})
})
}

Expand All @@ -171,7 +214,12 @@ getExplodedItemsForCategory = function (category,callback){
documentProvider.setType('items')
documentProvider.findByJSON({_id:{$in:itemIds}},function(err, items){
//console.log("ITEMS: "+JSON.stringify(items))
if(items == undefined){
//console.log("making a new array")
items = []
}
category.explodedItems = items
console.log("Category should have exploded " + JSON.stringify(category))
finalCallback(null)
})
}
Expand Down Expand Up @@ -240,9 +288,61 @@ exports.postCategory = function (req, res, next){
})
}

exports.putCategory = function(req, res){
//TODO: We should check the type to ensure JSON
res.set({
'Content-Type': 'application/json',
'type': 'get',
'dataType': 'json'
})
console.log(req.body)
var updateObj = req.body
console.log(JSON.stringify(updateObj))
documentProvider.setType('categories')
getIdFromString(updateObj.categoryId, function(err, id){
if(err != undefined){
console.log(err)
res.json(500, { error: err })
}
else{
documentProvider.findByJSON({_id:id}, function(err,categories){
if(err != undefined){
console.log(err)
res.json(500, { error: err })
}
else if(categories.length <= 0){
err = "There were no categories found"
console.log(err)
res.json(500, { error: err })
}
else{
documentProvider.updateJSON({_id:id},updateObj.updateValues,function(err){
if(err==undefined){
res.json({ message: "Category Updated" })
}
else{
res.json(500, { error: err })
}
})
}
})
}
})
}

exports.deleteCatalogEntry = function(req, res, next){
documentProvider.setType('items')
documentProvider.remove(req.params.itemid,function(){
return next()
})
}

getIdFromString = function(id, callback){
try{
var objectId=new BSON.ObjectID(id);
callback(null,objectId)
}
catch(exception){
callback(exception,null)
}
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "bullettrain",
"version": "1.0.6",
"version": "1.0.7",
"description": "Simple Ecommerce",
"keywords": [
"OpenShift",
Expand Down

0 comments on commit 5f73a65

Please sign in to comment.