Skip to content

Commit

Permalink
Necessary changes to support PersistedModel.updateAll
Browse files Browse the repository at this point in the history
  • Loading branch information
hideya committed Oct 23, 2015
1 parent 2f782b4 commit 2c14cdd
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 100 deletions.
3 changes: 2 additions & 1 deletion LoopBack/LBPersistedModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ - (NSDictionary *)toDictionary {
- (void)saveWithSuccess:(LBPersistedModelSaveSuccessBlock)success
failure:(SLFailureBlock)failure {
[self invokeMethod:self._id ? @"save" : @"create"
parameters:[self toDictionary]
parameters:nil
bodyParameters:[self toDictionary]
success:^(id value) {
[self setId:[value valueForKey:@"id"]];
success();
Expand Down
3 changes: 2 additions & 1 deletion LoopBack/LBUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ - (void)loginWithEmail:(NSString*)email
NSParameterAssert(email);
NSParameterAssert(password);
[self invokeStaticMethod:@"login"
parameters:@{ @"email": email, @"password": password }
parameters:nil
bodyParameters:@{ @"email": email, @"password": password }
success:^(id value) {
NSAssert([[value class] isSubclassOfClass:[NSDictionary class]], @"Received non-Dictionary: %@", value);
LBRESTAdapter* adapter = (LBRESTAdapter*)self.adapter;
Expand Down
7 changes: 5 additions & 2 deletions LoopBackTests/LBPersistedModelTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ - (void)testFindWithFilter {

- (void)testFindOne {
ASYNC_TEST_START
[[self.repository adapter] invokeStaticMethod:@"widgets.findOne" parameters:
@{ @"filter": @{@"where": @{ @"name" : @"Foo" }}} success:^(LBPersistedModel *model) {
[[self.repository adapter] invokeStaticMethod:@"widgets.findOne"
parameters:@{ @"filter": @{@"where": @{ @"name" : @"Foo" }}}
bodyParameters:nil
outputStream:nil
success:^(LBPersistedModel *model) {
XCTAssertNotNil(model, @"No models returned.");
XCTAssertEqualObjects(model[@"name"], @"Foo", @"Invalid name");
XCTAssertEqualObjects(model[@"bars"], @0, @"Invalid bars");
Expand Down
49 changes: 6 additions & 43 deletions SLRemoting/SLAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,6 @@ extern NSString *SLAdapterNotConnectedErrorDescription;
*/
- (void)connectToURL:(NSURL *)url;

/**
* Invokes a remotable method exposed statically on the server.
*
* Unlike SLAdapter::invokeInstanceMethod:constructorParameters:parameters:success:failure:,
* no object needs to be created on the server.
*
* @param method The method to invoke, e.g. `module.doSomething`.
* @param parameters The parameters to invoke with.
* @param success An SLSuccessBlock to be executed when the invocation
* succeeds.
* @param failure An SLFailureBlock to be executed when the invocation
* fails.
*/
- (void)invokeStaticMethod:(NSString *)method
parameters:(NSDictionary *)parameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;

/**
* Invokes a remotable method exposed statically on the server.
*
Expand All @@ -130,6 +112,8 @@ extern NSString *SLAdapterNotConnectedErrorDescription;
*
* @param method The method to invoke, e.g. `module.doSomething`.
* @param parameters The parameters to invoke with.
* @param bodyParameters The parameters that get JSON encoded and put into
* the message body when the method is POST or PUT.
* @param outputStream The stream to which all the response data goes.
* If this is set, no data is routed for further
* processing and the success block is invoked with `nil`.
Expand All @@ -140,6 +124,7 @@ extern NSString *SLAdapterNotConnectedErrorDescription;
*/
- (void)invokeStaticMethod:(NSString *)method
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;
Expand All @@ -158,31 +143,8 @@ extern NSString *SLAdapterNotConnectedErrorDescription;
* @param constructorParameters The parameters the virual object should be
* created with.
* @param parameters The parameters to invoke with.
* @param success An SLSuccessBlock to be executed when the
* invocation succeeds.
* @param failure An SLFailureBlock to be executed when the
* invocation fails.
*/
- (void)invokeInstanceMethod:(NSString *)method
constructorParameters:(NSDictionary *)constructorParameters
parameters:(NSDictionary *)parameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;

/**
* Invokes a remotable method exposed within a prototype on the server.
*
* This should be thought of as a two-step process. First, the server loads or
* creates an object with the appropriate type. Then and only then is the method
* invoked on that object. The two parameter dictionaries correspond to these
* two steps: `creationParameters` for the former, and `parameters` for the
* latter.
*
* @param method The method to invoke, e.g.
* `MyClass.prototype.doSomething`.
* @param constructorParameters The parameters the virual object should be
* created with.
* @param parameters The parameters to invoke with.
* @param bodyParameters The parameters that get JSON encoded and put into
* the message body when the method is POST or PUT.
* @param outputStream The stream to which all the response data goes.
* If this is set, no data is routed for further
* processing and the success block is invoked
Expand All @@ -195,6 +157,7 @@ extern NSString *SLAdapterNotConnectedErrorDescription;
- (void)invokeInstanceMethod:(NSString *)method
constructorParameters:(NSDictionary *)constructorParameters
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;
Expand Down
17 changes: 2 additions & 15 deletions SLRemoting/SLAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,17 @@ - (void)connectToURL:(NSURL *)url {

- (void)invokeStaticMethod:(NSString *)path
parameters:(NSDictionary *)parameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
NSAssert(NO, @"Invalid Adapter.");
}

- (void)invokeStaticMethod:(NSString *)path
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
NSAssert(NO, @"Invalid Adapter.");
}

- (void)invokeInstanceMethod:(NSString *)path
constructorParameters:(NSDictionary *)constructorParameters
parameters:(NSDictionary *)parameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
NSAssert(NO, @"Invalid Adapter.");
}

- (void)invokeInstanceMethod:(NSString *)method
constructorParameters:(NSDictionary *)constructorParameters
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
Expand Down
44 changes: 44 additions & 0 deletions SLRemoting/SLObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ extern NSString *SLObjectInvalidRepositoryDescription;
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;

/**
* Invokes a remotable method exposed within instances of this class on the
* server.
*
* @see SLAdapter::invokeInstanceMethod:constructorParameters:parameters:success:failure:
*
* @param name The method to invoke (without the prototype), e.g.
* `doSomething`.
* @param parameters The parameters to invoke with.
* @param bodyParameters The parameters that get JSON encoded and put into
* the message body when the verb is POST or PUT.
* @param success An SLSuccessBlock to be executed when the invocation
* succeeds.
* @param failure An SLFailureBlock to be executed when the invocation
* fails.
*/
- (void)invokeMethod:(NSString *)name
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;

/**
* Invokes a remotable method exposed within instances of this class on the
* server.
Expand Down Expand Up @@ -153,6 +175,28 @@ extern NSString *SLObjectInvalidRepositoryDescription;
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;

/**
* Invokes a remotable method exposed statically within this class on the
* server.
*
* @see SLAdapter::invokeStaticMethod:parameters:success:failure:
*
* @param name The method to invoke (without the class name), e.g.
* `doSomething`.
* @param parameters The parameters to invoke with.
* @param bodyParameters The parameters that get JSON encoded and put into
* the message body when the verb is POST or PUT.
* @param success An SLSuccessBlock to be executed when the invocation
* succeeds.
* @param failure An SLFailureBlock to be executed when the invocation
* fails.
*/
- (void)invokeStaticMethod:(NSString *)name
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;

/**
* Invokes a remotable method exposed statically within this class on the
* server.
Expand Down
49 changes: 45 additions & 4 deletions SLRemoting/SLObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,32 @@ - (void)invokeMethod:(NSString *)name
name];

[self.repository.adapter invokeInstanceMethod:path
constructorParameters:self.creationParameters
parameters:parameters
success:success
failure:failure];
constructorParameters:self.creationParameters
parameters:parameters
bodyParameters:nil
outputStream:nil
success:success
failure:failure];
}

- (void)invokeMethod:(NSString *)name
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
NSAssert(self.repository, SLObjectInvalidRepositoryDescription);

NSString *path = [NSString stringWithFormat:@"%@.prototype.%@",
self.repository.className,
name];

[self.repository.adapter invokeInstanceMethod:path
constructorParameters:self.creationParameters
parameters:parameters
bodyParameters:bodyParameters
outputStream:nil
success:success
failure:failure];
}

- (void)invokeMethod:(NSString *)name
Expand All @@ -73,6 +95,7 @@ - (void)invokeMethod:(NSString *)name
[self.repository.adapter invokeInstanceMethod:path
constructorParameters:self.creationParameters
parameters:parameters
bodyParameters:nil
outputStream:outputStream
success:success
failure:failure];
Expand Down Expand Up @@ -108,6 +131,23 @@ - (void)invokeStaticMethod:(NSString *)name
NSString *path = [NSString stringWithFormat:@"%@.%@", self.className, name];
[self.adapter invokeStaticMethod:path
parameters:parameters
bodyParameters:nil
outputStream:nil
success:success
failure:failure];
}

- (void)invokeStaticMethod:(NSString *)name
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {

NSString *path = [NSString stringWithFormat:@"%@.%@", self.className, name];
[self.adapter invokeStaticMethod:path
parameters:parameters
bodyParameters:bodyParameters
outputStream:nil
success:success
failure:failure];
}
Expand All @@ -121,6 +161,7 @@ - (void)invokeStaticMethod:(NSString *)name
NSString *path = [NSString stringWithFormat:@"%@.%@", self.className, name];
[self.adapter invokeStaticMethod:path
parameters:parameters
bodyParameters:nil
outputStream:outputStream
success:success
failure:failure];
Expand Down
55 changes: 21 additions & 34 deletions SLRemoting/SLRESTAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ @interface SLRESTAdapter() {
- (void)requestWithPath:(NSString *)path
verb:(NSString *)verb
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
multipart:(BOOL)multipart
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
Expand Down Expand Up @@ -64,18 +65,7 @@ - (void)connectToURL:(NSURL *)url {

- (void)invokeStaticMethod:(NSString *)method
parameters:(NSDictionary *)parameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {

[self invokeStaticMethod:method
parameters:parameters
outputStream:nil
success:success
failure:failure];
}

- (void)invokeStaticMethod:(NSString *)method
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
Expand All @@ -90,6 +80,7 @@ - (void)invokeStaticMethod:(NSString *)method
[self requestWithPath:path
verb:verb
parameters:mutableParams
bodyParameters:bodyParameters
multipart:multipart
outputStream:outputStream
success:success
Expand All @@ -99,20 +90,7 @@ - (void)invokeStaticMethod:(NSString *)method
- (void)invokeInstanceMethod:(NSString *)method
constructorParameters:(NSDictionary *)constructorParameters
parameters:(NSDictionary *)parameters
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {

[self invokeInstanceMethod:method
constructorParameters:constructorParameters
parameters:parameters
outputStream:nil
success:success
failure:failure];
}

- (void)invokeInstanceMethod:(NSString *)method
constructorParameters:(NSDictionary *)constructorParameters
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
Expand All @@ -130,6 +108,7 @@ - (void)invokeInstanceMethod:(NSString *)method
[self requestWithPath:path
verb:verb
parameters:combinedParameters
bodyParameters:bodyParameters
multipart:multipart
outputStream:outputStream
success:success
Expand All @@ -139,28 +118,36 @@ - (void)invokeInstanceMethod:(NSString *)method
- (void)requestWithPath:(NSString *)path
verb:(NSString *)verb
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
multipart:(BOOL)multipart
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {

NSAssert(self.connected, SLAdapterNotConnectedErrorDescription);

if ([[verb uppercaseString] isEqualToString:@"GET"]) {
client.parameterEncoding = AFFormURLParameterEncoding;
} else {
client.parameterEncoding = AFJSONParameterEncoding;
}

// Remove the leading / so that the path is treated as relative to the baseURL
if ([path hasPrefix:@"/"]) {
path = [path substringFromIndex:1];
}

NSURLRequest *request;
NSMutableURLRequest *request;

if (!multipart) {
request = [client requestWithMethod:verb path:path parameters:parameters];
if ([verb isEqualToString:@"GET"] || [verb isEqualToString:@"HEAD"] || [verb isEqualToString:@"DELETE"]) {
parameters = ([parameters count] > 0) ? parameters : nil;
request = [client requestWithMethod:verb path:path parameters:parameters];
} else {
client.parameterEncoding = AFJSONParameterEncoding;
request = [client requestWithMethod:verb path:path parameters:bodyParameters];
if (parameters) {
NSURL *url = [NSURL URLWithString:[[request.URL absoluteString]
stringByAppendingFormat:@"?%@",
SLAFQueryStringFromParametersWithEncoding(parameters,
client.stringEncoding)]];
[request setURL:url];
}
}
} else {
request = [client multipartFormRequestWithMethod:verb
path:path
Expand Down
Loading

0 comments on commit 2c14cdd

Please sign in to comment.