SHARE
TWEET

Untitled

a guest Aug 23rd, 2017 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. scanExpression.scanFilter = @{@"Password": passwordCondition, UserNameOrUserEmail : usernameOrEmailCondition};
  2.    
  3. scanExpression.scanFilter = @{@"Password": passwordCondition};
  4.    
  5. // Check if email or username and decide DynamoDB Attribute accordingly.
  6. NSString *UserNameOrUserEmail;  
  7. if ([usernameOrEmail rangeOfString:@"@"].location == NSNotFound)
  8.     UserNameOrUserEmail = @"UserName";
  9. else
  10.     UserNameOrUserEmail = @"UserEmail";
  11.  
  12. AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
  13.  
  14. AWSDynamoDBCondition *passwordCondition = [AWSDynamoDBCondition new];
  15. AWSDynamoDBAttributeValue *passwordAttribute = [AWSDynamoDBAttributeValue new];
  16. passwordAttribute.S = password;
  17. passwordCondition.attributeValueList = @[passwordAttribute];
  18. passwordCondition.comparisonOperator = AWSDynamoDBComparisonOperatorEQ;
  19.  
  20. AWSDynamoDBCondition *usernameOrEmailCondition = [AWSDynamoDBCondition new];
  21. AWSDynamoDBAttributeValue *usernameOrEmailAttribute = [AWSDynamoDBAttributeValue new];
  22. usernameOrEmailAttribute.S = @"sammy@hhd.com";//usernameOrEmail;
  23. usernameOrEmailCondition.attributeValueList = @[usernameOrEmailAttribute];
  24. usernameOrEmailCondition.comparisonOperator = AWSDynamoDBComparisonOperatorEQ;
  25.  
  26. //Apply filter condtions: http://stackoverflow.com/a/27856299/818506
  27. scanExpression.scanFilter = @{@"Password": passwordCondition, UserNameOrUserEmail : usernameOrEmailCondition}; //@{@"UserEmail": usernameOrEmailCondition};//
  28.  
  29.  //^^^^^^^^^^^^^^^^^^^^^^^^^^^Is this correct?^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    
  30.  
  31. //Perform a scan:
  32. [[dynamoDBObjectMapper scan:[User class] expression:scanExpression]continueWithBlock: ^id (AWSTask *task){
  33.  
  34.     if (task.error) {
  35.         NSLog(@"The request failed. Error: [%@]", task.error);
  36.     }
  37.     if (task.exception) {
  38.         NSLog(@"The request failed. Exception: [%@]", task.exception);
  39.     }
  40.     if (task.result) {
  41.         AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
  42.         for (User *user in paginatedOutput.items) {
  43.             isLoginSuccessful = YES;
  44.          }
  45.         completionHandler(isLoginSuccessful);
  46.     }
  47.     return nil;
  48. }];
  49.    
  50. // create password condition
  51.     let passwordCondition = AWSDynamoDBCondition()
  52.     let v1    = AWSDynamoDBAttributeValue(); v1.S = password
  53.     passwordCondition.comparisonOperator = AWSDynamoDBComparisonOperator.EQ
  54.     passwordCondition.attributeValueList = [ v1 ]
  55.  
  56.     // create username condition        
  57.     let usernameCondition = AWSDynamoDBCondition()
  58.     let v2    = AWSDynamoDBAttributeValue(); v2.S = "sammy@hhd.com"
  59.     usernameCondition.comparisonOperator = AWSDynamoDBComparisonOperator.EQ
  60.     usernameCondition.attributeValueList = [ v2 ]
  61.  
  62.     // add these conditions to a NSMutableDictionary
  63.     var scanFilterDict : NSMutableDictionary! = NSMutableDictionary()
  64.     scanFilterDict.addEntriesFromDictionary(["password": passwordCondition])
  65.     scanFilterDict.addEntriesFromDictionary(["UserNameOrUserEmail": usernameCondition])
  66.  
  67.     // use the mutable dictionary as scan filter
  68.     scanInput.scanFilter = scanFilterDict as Dictionary<NSObject, AnyObject>
  69.     scanInput.exclusiveStartKey = self.lastEvaluatedKey
  70.     scanInput.limit = 20
  71.     scanInput.tableName = "yourDynamoDBTable"
  72.  
  73.     // apply AND operator on both conditions
  74.     scanInput.conditionalOperator = AWSDynamoDBConditionalOperator.And
  75.  
  76.     AWSDynamoDB.defaultDynamoDB().scan(input)
  77.    
  78. let objectMapper = AWSDynamoDBObjectMapper.default()
  79.     let scanExpression = AWSDynamoDBScanExpression()
  80.  
  81.     scanExpression.filterExpression = "#UserNameOrUserEmail = :UserNameOrUserEmail AND #password = :follows"
  82.     scanExpression.expressionAttributeNames = ["#UserNameOrUserEmail": "UserNameOrUserEmail" , "#password": "password" ,]
  83.     scanExpression.expressionAttributeValues = [":UserNameOrUserEmail": "sammy@hhd.com", ":password": password ,]
  84.  
  85.     objectMapper.scan(User.self, expression: scanExpression) { (response: AWSDynamoDBPaginatedOutput?, error: Error?) in
  86.         // handle data or error here...
  87.     }
RAW Paste Data
Top