Advertisement
hardW

Heavy RAC for REST API

Feb 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (RACSignal *)saveQuizBody:(Quiz *)quiz {
  2.     NSString *path, *method;
  3.     if (quiz.identifier) {
  4.         path = NSStringWithFormat(@"/api/scrapbook/quizzes/%@", quiz.identifier);
  5.         method = @"PUT";
  6.     } else {
  7.         path = @"/api/scrapbook/quizzes/";
  8.         method = @"POST";
  9.     }
  10.     NSDictionary *quizJSON = @{@"quiz" : [MTLJSONAdapter JSONDictionaryFromModel:quiz]};
  11.  
  12.     NSMutableURLRequest *request = [self.service.http requestWithMethod:method path:path parameters:quizJSON];
  13.     return [self.service startAuthorizedJSONRequest:request returningInstanceOfClass:[Quiz class]];
  14. }
  15.  
  16. - (RACSignal *)saveQuizQuestionBody:(QuizQuestion *)questionToSave {
  17.     NSString *path, *method;
  18.     if (questionToSave.identifier) {
  19.         path =  NSStringWithFormat(@"/api/scrapbook/quizzes/%@/questions/%@",
  20.                                    questionToSave.quiz.identifier,
  21.                                    questionToSave.identifier);
  22.         method = @"PUT";
  23.     } else {
  24.         path =  NSStringWithFormat(@"/api/scrapbook/quizzes/%@/questions/",
  25.                                    questionToSave.quiz.identifier);
  26.         method = @"POST";
  27.     }
  28.    
  29.     NSMutableDictionary *questionJson = [[MTLJSONAdapter JSONDictionaryFromModel:questionToSave] mutableCopy];
  30.     NSMutableURLRequest *request = [self.service.http requestWithMethod:method
  31.                                                                    path:path
  32.                                                              parameters:@{@"quizQuestion":questionJson}];
  33.    
  34.     return [self.service startAuthorizedJSONRequest:request returningInstanceOfClass:[QuizQuestion class]];
  35. }
  36.  
  37. - (RACSignal *)saveQuizQuestion:(QuizQuestion *)questionToSave {
  38.     RACSignal *signal;
  39.     if (questionToSave.coverImage) {
  40.         signal = [[self.service startImageUploadRequestWithPath:@"api/files/upload" image:questionToSave.coverImage] flattenMap:^RACStream *(NSDictionary *val) {
  41.             if (!questionToSave.cover) {
  42.                 questionToSave.cover = [CoverVO new];
  43.             }
  44.             questionToSave.cover.identifier = [val[@"file"][@"id"] integerValue];
  45.             questionToSave.cover.originalImageURL = [NSURL URLWithString:val[@"file"][@"url"]];
  46.            
  47.             return [self saveQuizQuestionBody:questionToSave];
  48.         }];
  49.     } else {
  50.         signal = [self saveQuizQuestionBody:questionToSave];
  51.     }
  52.    
  53.     return [signal doNext:^(QuizQuestion *savedQuestion) {
  54.         questionToSave.identifier = savedQuestion.identifier;
  55.        
  56.         Quiz *quiz = questionToSave.quiz;
  57.         BOOL found = NO;
  58.         for (NSString *questionId in quiz.questionIds) {
  59.             if ([questionId isEqualToString:savedQuestion.identifier]) {
  60.                 found = YES;
  61.                 break;
  62.             }
  63.         }
  64.         if (!found) {
  65.             if (quiz.questionIds) {
  66.                 quiz.questionIds = [quiz.questionIds arrayByAddingObject:questionToSave.identifier];
  67.             } else {
  68.                 quiz.questionIds = @[savedQuestion.identifier];
  69.             }
  70.         }
  71.        
  72.         NSUInteger answersToSaveCount = questionToSave.answers.count;
  73.         NSUInteger savedAnswersCount = savedQuestion.answers.count;
  74.         NSUInteger totalCount;
  75.        
  76.         if (answersToSaveCount == savedAnswersCount) {
  77.             totalCount = answersToSaveCount;
  78.             NSLog(@"ERROR: %d answers to save DON'T MATCH %d saved answers for questions: toSave = %@ saved = %@", answersToSaveCount, savedAnswersCount, questionToSave, savedQuestion);
  79.         } else {
  80.             totalCount = MIN(answersToSaveCount, savedAnswersCount);
  81.         }
  82.        
  83.         for (NSInteger i = 0; i < totalCount; i ++) {
  84.             ((QuizAnswer *)questionToSave.answers[i]).identifier = ((QuizAnswer *)savedQuestion.answers[i]).identifier;
  85.         }
  86.     }];
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement