Created
September 18, 2010 05:28
-
-
Save itfrombit/585386 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
(load "NuMongoDB") | |
(load "NuJSON") | |
(load "Nutils:cl_utils") ; for butlast and last functions | |
(load "Nutils:with_object") ; for with-object | |
; Wrap the mongo connection and make sure the last value of body is | |
; the return value | |
(macro with-mongo ((user password db) *body) | |
(set __allbutlast (butlast *body)) | |
(set __last (last *body)) | |
`(let* ((mongo (NuMongoDB new)) | |
(__connected (mongo connectWithOptions:nil))) | |
(mongo authenticateUser:,user withPassword:,password forDatabase:,db) | |
,@__allbutlast | |
(set __lastResult ,@__last) | |
(mongo close) | |
__lastResult)) | |
; Shortened version: | |
(post "/reset" | |
(with-mongo ("stickup" "stickup" "stickup") | |
(mongo dropCollection:"users" inDatabase:"stickup") | |
(mongo dropCollection:"stickups" inDatabase:"stickup") | |
((dict status:200 message:"Reset database.") JSONRepresentation))) | |
(post "/stickup" | |
(with-mongo ("stickup" "stickup" "stickup") | |
(set stickup (REQUEST post)) | |
(set user (mongo findOne:(dict name:(stickup user:)) inCollection:"stickup.users")) | |
(unless user | |
(set user (dict name:(stickup user:) password:(stickup password:))) | |
(mongo insertObject:user intoCollection:"stickup.users")) | |
(set result (if (eq (user password:) (stickup password:)) | |
(then (with-object stickup | |
(removeObjectForKey:"password") | |
(time:((NSDate date) description)) | |
(location:(dict latitude:((stickup latitude:) floatValue) | |
longitude:((stickup longitude:) floatValue))) | |
(removeObjectForKey:"latitude") | |
(removeObjectForKey:"longitude")) | |
(mongo insertObject:stickup intoCollection:"stickup.stickups") | |
(dict status:200 message:"Thank you." saved:stickup)) | |
(else (dict status:403 message:"Unable to post stickup.")))) | |
(result JSONRepresentation))) | |
(get "/stickups" | |
(with-mongo ("stickup" "stickup" "stickup") | |
(mongo ensureCollection:"stickup.stickups" hasIndex:(dict location:"2d") withOptions:0) | |
(set query (dict)) | |
(if (and (set latitude (((REQUEST query) latitude:) floatValue)) | |
(set longitude (((REQUEST query) longitude:) floatValue))) | |
(query location:(dict $near:(dict latitude:latitude longitude:longitude)))) | |
(unless (set count (((REQUEST query) count:) intValue)) | |
(set count 10)) | |
((dict status:200 stickups:(mongo findArray:query | |
inCollection:"stickup.stickups" | |
returningFields:nil | |
numberToReturn:count | |
numberToSkip:0)) | |
JSONRepresentation))) | |
(get "/count" | |
(with-mongo ("stickup" "stickup" "stickup") | |
(set count (mongo countWithCondition:nil inCollection:"stickups" inDatabase:"stickup")) | |
((dict status:200 count:count) JSONRepresentation))) |
Yeah, that's a better idea. Fixed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. I was thinking about a with-mongo macro but hadn't done it. Breaking out the last statement for evaluation after the database is closed could be a problem if someone packed a mongo reference into that last statement. Why not instead use a let or temporary variable to hold the last value while you close the connection?
Also, in other code I've had some magic for calling JSONRepresentation automatically and setting the content type header (which I forgot to do in this example). And while I'm rambling, I've been considering switching to BSON for my preferred wire format.