Skip to content

Instantly share code, notes, and snippets.

@itfrombit
Created September 18, 2010 05:28
Show Gist options
  • Save itfrombit/585386 to your computer and use it in GitHub Desktop.
Save itfrombit/585386 to your computer and use it in GitHub Desktop.
(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)))
@itfrombit
Copy link
Author

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