Skip to content

Instantly share code, notes, and snippets.

@timburks
Created September 16, 2010 17:54
Show Gist options
  • Save timburks/582847 to your computer and use it in GitHub Desktop.
Save timburks/582847 to your computer and use it in GitHub Desktop.
#import <Nunja/Nunja.h>
#import "NuMongoDB.h"
#import "JSON.h"
@interface ServerDelegate : NunjaDefaultDelegate
{
}
@end
@implementation ServerDelegate
- (void) applicationDidFinishLaunching {
[self addHandlerWithHTTPMethod:@"POST"
path:@"/reset"
block:^(NunjaRequest *REQUEST) {
NuMongoDB *mongo = [NuMongoDB new];
[mongo connectWithOptions:nil];
[mongo authenticateUser:@"stickup"
withPassword:@"stickup"
forDatabase:@"stickup"];
[mongo dropCollection:@"users" inDatabase:@"stickup"];
[mongo dropCollection:@"stickups" inDatabase:@"stickup"];
id result = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:200], @"status",
@"Reset database.", @"message",
nil];
[mongo close];
return [result JSONRepresentation];
}];
[self addHandlerWithHTTPMethod:@"POST"
path:@"/stickup"
block:^(NunjaRequest *REQUEST) {
NuMongoDB *mongo = [NuMongoDB new];
[mongo connectWithOptions:nil];
[mongo authenticateUser:@"stickup"
withPassword:@"stickup"
forDatabase:@"stickup"];
id stickup = [REQUEST post];
id user = [mongo findOne:[NSDictionary dictionaryWithObjectsAndKeys:
[stickup objectForKey:@"user"], @"name",
nil]
inCollection:@"stickup.users"];
if (!user) {
user = [NSDictionary dictionaryWithObjectsAndKeys:
[stickup objectForKey:@"user"], @"name",
[stickup objectForKey:@"user"], @"password",
nil];
[mongo insertObject:user intoCollection:@"stickup.users"];
}
id result;
if ([[user objectForKey:@"password"] isEqualToString:
[stickup objectForKey:@"password"]]) {
[stickup removeObjectForKey:@"password"];
[stickup setObject:[NSDate date] forKey:@"time"];
NSNumber *latitude = [NSNumber numberWithFloat:
[[stickup objectForKey:@"latitude"] floatValue]];
NSNumber *longitude = [NSNumber numberWithFloat:
[[stickup objectForKey:@"longitude"] floatValue]];
[stickup setObject:[NSDictionary dictionaryWithObjectsAndKeys:
latitude, @"latitude",
longitude, @"longitude",
nil]
forKey:@"location"];
[stickup removeObjectForKey:@"latitude"];
[stickup removeObjectForKey:@"longitude"];
[mongo insertObject:stickup intoCollection:@"stickup.stickups"];
result = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:200], @"status",
@"Thank you.", @"message",
stickup, @"saved",
nil];
} else {
result = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:403], @"status",
@"Unable to post stickup.", @"message",
nil];
}
[mongo close];
return [result JSONRepresentation];
}];
[self addHandlerWithHTTPMethod:@"GET"
path:@"/count"
block:^(NunjaRequest *REQUEST) {
NuMongoDB *mongo = [NuMongoDB new];
[mongo connectWithOptions:nil];
[mongo authenticateUser:@"stickup"
withPassword:@"stickup"
forDatabase:@"stickup"];
int count = [mongo countWithCondition:nil inCollection:@"stickups" inDatabase:@"stickup"];
id result = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:count], @"count",
[NSNumber numberWithInt:200], @"status",
nil];
[mongo close];
return [result JSONRepresentation];
}];
[self addHandlerWithHTTPMethod:@"GET"
path:@"/stickups"
block:^(NunjaRequest *REQUEST) {
NuMongoDB *mongo = [NuMongoDB new];
[mongo connectWithOptions:nil];
[mongo authenticateUser:@"stickup"
withPassword:@"stickup"
forDatabase:@"stickup"];
[mongo ensureCollection:@"stickup.stickups"
hasIndex:[NSDictionary dictionaryWithObjectsAndKeys:
@"2d", @"location",
nil]
withOptions:0];
NSMutableDictionary *query = [NSMutableDictionary dictionary];
NSNumber *latitude = [NSNumber numberWithFloat:
[[[REQUEST query] objectForKey:@"latitude"] floatValue]];
NSNumber *longitude = [NSNumber numberWithFloat:
[[[REQUEST query] objectForKey:@"longitude"] floatValue]];
[query setObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:
latitude, @"latitude",
longitude, @"longitude",
nil],
@"$near",
nil]
forKey:@"location"];
int count = [[[REQUEST query] objectForKey:@"count"] intValue];
if (count == 0) {
count = 10;
}
id stickups = [mongo findArray:query inCollection:@"stickup.stickups"
returningFields:nil numberToReturn:count numberToSkip:0];
id result = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:200], @"status",
stickups, @"stickups",
nil];
[mongo close];
return [result JSONRepresentation];
}];
}
@end
int main (int argc, const char * argv[])
{
return NunjaMain(argc, argv, @"ServerDelegate");
}
(load "NuMongoDB")
(load "NuJSON")
(post "/reset"
(set mongo (NuMongoDB new))
(set connected (mongo connectWithOptions:nil))
(mongo authenticateUser:"stickup" withPassword:"stickup" forDatabase:"stickup")
(mongo dropCollection:"users" inDatabase:"stickup")
(mongo dropCollection:"stickups" inDatabase:"stickup")
(mongo close)
((dict status:200 message:"Reset database.") JSONRepresentation))
(post "/stickup"
(set mongo (NuMongoDB new))
(set connected (mongo connectWithOptions:nil))
(mongo authenticateUser:"stickup" withPassword:"stickup" forDatabase:"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 (stickup removeObjectForKey:"password")
(stickup time:((NSDate date) description))
(stickup location:(dict latitude:((stickup latitude:) floatValue)
longitude:((stickup longitude:) floatValue)))
(stickup removeObjectForKey:"latitude")
(stickup 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."))))
(mongo close)
(result JSONRepresentation))
(get "/stickups"
(set mongo (NuMongoDB new))
(set connected (mongo connectWithOptions:nil))
(mongo authenticateUser:"stickup" withPassword:"stickup" forDatabase:"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))
(mongo close)
((dict status:200 stickups:(mongo findArray:query
inCollection:"stickup.stickups"
returningFields:nil
numberToReturn:count
numberToSkip:0))
JSONRepresentation))
(get "/count"
(set mongo (NuMongoDB new))
(set connected (mongo connectWithOptions:nil))
(mongo authenticateUser:"stickup" withPassword:"stickup" forDatabase:"stickup")
(mongo close)
(set count (mongo countWithCondition:nil inCollection:"stickups" inDatabase:"stickup"))
((dict status:200 count:count) JSONRepresentation))
@itfrombit
Copy link

A macro to wrap the mongo connection and knock out some boilerplate code:
http://gist.github.com/585386

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment