Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (RACSignal *)saveQuizBody:(Quiz *)quiz {
- NSString *path, *method;
- if (quiz.identifier) {
- path = NSStringWithFormat(@"/api/scrapbook/quizzes/%@", quiz.identifier);
- method = @"PUT";
- } else {
- path = @"/api/scrapbook/quizzes/";
- method = @"POST";
- }
- NSDictionary *quizJSON = @{@"quiz" : [MTLJSONAdapter JSONDictionaryFromModel:quiz]};
- NSMutableURLRequest *request = [self.service.http requestWithMethod:method path:path parameters:quizJSON];
- return [self.service startAuthorizedJSONRequest:request returningInstanceOfClass:[Quiz class]];
- }
- - (RACSignal *)saveQuizQuestionBody:(QuizQuestion *)questionToSave {
- NSString *path, *method;
- if (questionToSave.identifier) {
- path = NSStringWithFormat(@"/api/scrapbook/quizzes/%@/questions/%@",
- questionToSave.quiz.identifier,
- questionToSave.identifier);
- method = @"PUT";
- } else {
- path = NSStringWithFormat(@"/api/scrapbook/quizzes/%@/questions/",
- questionToSave.quiz.identifier);
- method = @"POST";
- }
- NSMutableDictionary *questionJson = [[MTLJSONAdapter JSONDictionaryFromModel:questionToSave] mutableCopy];
- NSMutableURLRequest *request = [self.service.http requestWithMethod:method
- path:path
- parameters:@{@"quizQuestion":questionJson}];
- return [self.service startAuthorizedJSONRequest:request returningInstanceOfClass:[QuizQuestion class]];
- }
- - (RACSignal *)saveQuizQuestion:(QuizQuestion *)questionToSave {
- RACSignal *signal;
- if (questionToSave.coverImage) {
- signal = [[self.service startImageUploadRequestWithPath:@"api/files/upload" image:questionToSave.coverImage] flattenMap:^RACStream *(NSDictionary *val) {
- if (!questionToSave.cover) {
- questionToSave.cover = [CoverVO new];
- }
- questionToSave.cover.identifier = [val[@"file"][@"id"] integerValue];
- questionToSave.cover.originalImageURL = [NSURL URLWithString:val[@"file"][@"url"]];
- return [self saveQuizQuestionBody:questionToSave];
- }];
- } else {
- signal = [self saveQuizQuestionBody:questionToSave];
- }
- return [signal doNext:^(QuizQuestion *savedQuestion) {
- questionToSave.identifier = savedQuestion.identifier;
- Quiz *quiz = questionToSave.quiz;
- BOOL found = NO;
- for (NSString *questionId in quiz.questionIds) {
- if ([questionId isEqualToString:savedQuestion.identifier]) {
- found = YES;
- break;
- }
- }
- if (!found) {
- if (quiz.questionIds) {
- quiz.questionIds = [quiz.questionIds arrayByAddingObject:questionToSave.identifier];
- } else {
- quiz.questionIds = @[savedQuestion.identifier];
- }
- }
- NSUInteger answersToSaveCount = questionToSave.answers.count;
- NSUInteger savedAnswersCount = savedQuestion.answers.count;
- NSUInteger totalCount;
- if (answersToSaveCount == savedAnswersCount) {
- totalCount = answersToSaveCount;
- NSLog(@"ERROR: %d answers to save DON'T MATCH %d saved answers for questions: toSave = %@ saved = %@", answersToSaveCount, savedAnswersCount, questionToSave, savedQuestion);
- } else {
- totalCount = MIN(answersToSaveCount, savedAnswersCount);
- }
- for (NSInteger i = 0; i < totalCount; i ++) {
- ((QuizAnswer *)questionToSave.answers[i]).identifier = ((QuizAnswer *)savedQuestion.answers[i]).identifier;
- }
- }];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement