-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueryResult.m
More file actions
executable file
·122 lines (105 loc) · 4.44 KB
/
QueryResult.m
File metadata and controls
executable file
·122 lines (105 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// QueryResult.m
// Contentstack
//
// Created by Reefaq on 11/07/15.
// Copyright (c) 2015 Contentstack. All rights reserved.
//
#import "QueryResult.h"
#import "CSIOInternalHeaders.h"
#import "CSIOConstants.h"
#import "ContentType.h"
#import "Taxonomy.h"
#import "Entry.h"
@interface QueryResult ()
@property (nonatomic, strong) NSMutableDictionary *resultsDictionary;
@end
@implementation QueryResult
- (instancetype)initWithContentType:(ContentType*)contentType objectDictionary:(NSDictionary*)dictionary{
if (self = [super init]) {
self.contentType = contentType;
self.resultsDictionary = [NSMutableDictionary dictionary];
if (dictionary) {
[self.resultsDictionary addEntriesFromDictionary:dictionary];
}
}
return self;
}
- (instancetype)initWithTaxonomy:(Taxonomy*)taxonomy objectDictionary:(NSDictionary*)dictionary{
if (self = [super init]) {
self.taxonomy = taxonomy;
self.resultsDictionary = [NSMutableDictionary dictionary];
if (dictionary) {
[self.resultsDictionary addEntriesFromDictionary:dictionary];
}
}
return self;
}
- (NSInteger)totalCount {
if ([self.resultsDictionary objectForKey:kCSIO_Count]) {
if ([[self.resultsDictionary objectForKey:kCSIO_Count] isKindOfClass:[NSNumber class]]) {
NSInteger count = [[self.resultsDictionary objectForKey:kCSIO_Count] integerValue];
return count;
}
}
if ([self.resultsDictionary objectForKey:kCSIO_Entries]) {
if ([[self.resultsDictionary objectForKey:kCSIO_Entries] isKindOfClass:[NSArray class]]) {
NSInteger count = [[self.resultsDictionary objectForKey:kCSIO_Entries] count];
return count;
}
}
return 0;
}
//MARK: Result of the Query -
- (NSArray<Entry *> *)getResult{
if ([self.resultsDictionary objectForKey:kCSIO_Entries] && [[self.resultsDictionary objectForKey:kCSIO_Entries] isKindOfClass:[NSArray class]]) {
NSArray *objectsArray = (NSArray*)[self.resultsDictionary objectForKey:kCSIO_Entries];
NSMutableArray *entryObjects = [NSMutableArray array];
// if condition is fix for value of "entries" key ie.array inside array in response JSON
if (objectsArray.firstObject && [objectsArray.firstObject isKindOfClass:[NSDictionary class]]) {
[objectsArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
Entry *formEntry = [self.contentType entry];
if (formEntry == NULL) {
formEntry = [self.taxonomy entry];
}
[formEntry configureWithDictionary:obj];
[entryObjects addObject:formEntry];
}];
} else if (objectsArray.firstObject && [objectsArray.firstObject isKindOfClass:[NSArray class]]) {
[objectsArray enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj enumerateObjectsUsingBlock:^(NSDictionary *objDict, NSUInteger idx, BOOL * _Nonnull stop) {
Entry *formEntry = [self.contentType entry];
[formEntry configureWithDictionary:objDict];
[entryObjects addObject:formEntry];
}];
}];
} else {
[objectsArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSDictionary *objDict = (NSDictionary *)obj;
Entry *formEntry = [self.contentType entry];
[formEntry configureWithDictionary:objDict];
[entryObjects addObject:formEntry];
}];
}
return entryObjects;
} else {
return nil;
}
}
//MARK: Get Schema -
- (NSArray *)schema{
if ([self.resultsDictionary objectForKey:kCSIO_Schema] && [[self.resultsDictionary objectForKey:kCSIO_Schema] isKindOfClass:[NSArray class]]) {
NSArray *objectsArray = (NSArray *)[self.resultsDictionary objectForKey:kCSIO_Schema];
return objectsArray;
}
return nil;
}
//MARK: Get content_type -
- (NSDictionary *)content_type{
if ([self.resultsDictionary objectForKey:kCSIO_ContentType] && [[self.resultsDictionary objectForKey:kCSIO_ContentType] isKindOfClass:[NSDictionary class]]) {
NSDictionary *objectsArray = (NSDictionary *)[self.resultsDictionary objectForKey:kCSIO_ContentType];
return objectsArray;
}
return nil;
}
@end