Created
January 20, 2013 16:05
-
-
Save eventomer/4579520 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
/* | |
* limit is kInitialObjectsPerPageNum(100) | |
* oredered by descending adDate and createdAt | |
* kSpecialPropertyKeyApproved(approved) is YES | |
* kSpecialPropertyKeyDeleted(deleted) is NO | |
* not showing ads with price under 100 | |
* | |
* params: | |
* kSpecialPropertyKeyUserID(userID) | |
* location_lat, location_long for kSpecialPropertyKeyLocation(location) | |
*/ | |
function nearByAds(request, response) | |
{ | |
//free | |
var freeSubQuery = baseQuery(request); | |
freeSubQuery.equalTo("price",0); | |
//above 100 | |
var priceSubQuery = baseQuery(request); | |
priceSubQuery.greaterThanOrEqualTo("price",100); | |
//greatFind included | |
var greatFindQuery = baseQuery(request); | |
greatFindQuery.equalTo("ribbon",'GREAT_FIND'); | |
var orQuery = new Parse.Query.or(greatFindQuery,priceSubQuery,freeSubQuery); | |
orQuery.find({ | |
success:function(results){ | |
//'great finds' are given preferred sorting, their sort order is cut in half. | |
for(var i = 0; i<results.length; i++){ | |
if(results[i].get('ribbon')=='GREAT_FIND'){ | |
results.insert(i/2,results.remove(i).pop()); | |
} | |
} | |
response.success(results.slice(0,100)); | |
}, | |
error: function(error) { | |
response.error(error); | |
} | |
}); | |
} | |
function baseQuery(request) | |
{ | |
var mainQuery = new Parse.Query("Ad"); | |
mainQuery.limit = 200; | |
mainQuery.descending("createdAt"); | |
mainQuery.equalTo("approved",true); | |
mainQuery.equalTo("deleted",false); | |
mainQuery.notEqualTo("userID",request.params.userID); | |
if(request.params.location_lat!='-1' || request.params.location_long != '-1'){//only if valid location | |
var location = new locationUtil.LatLon(request.params.location_lat,request.params.location_long); | |
//gets a relative geopoint by 225 degrees and 4.1km distance | |
var southwest = location.destinationPoint(225,4.1); | |
var northeast = location.destinationPoint(45,4.1);; | |
mainQuery.withinGeoBox("location", | |
new Parse.GeoPoint(southwest.lat(), southwest.lon()), | |
new Parse.GeoPoint(northeast.lat(), northeast.lon())); | |
} | |
return mainQuery; | |
} | |
Array.prototype.insert = function (index, item) { | |
this.splice(index, 0, item); | |
}; | |
Array.prototype.remove = function (index) { | |
return this.splice(index, 1); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment