Line data Source code
1 : import 'package:contentstack/src/enums/operations.dart'; 2 : 3 : /// 4 : /// This is base Query class that contains common 5 : /// functions to query in Entry, Assets and content_type 6 : /// 7 : class BaseQuery { 8 : 9 : final Map<String, String> queryParameter = <String, String>{}; 10 : final Map<String, dynamic> parameter = <String, dynamic>{}; 11 : 12 1 : void where(String fieldUid, QueryOperation queryOperation) { 13 1 : if (fieldUid != null && fieldUid.isNotEmpty) { 14 2 : queryOperation.when(equals: (operation) { 15 3 : parameter[fieldUid] = operation.value; 16 1 : }, notEquals: (operation) { 17 4 : parameter[fieldUid] = {'\$ne': operation.value}; 18 1 : }, includes: (operation) { 19 4 : parameter[fieldUid] = {'\$in': operation.value}; 20 1 : }, excludes: (operation) { 21 4 : parameter[fieldUid] = {'\$nin': operation.value}; 22 1 : }, isLessThan: (operation) { 23 4 : parameter[fieldUid] = {'\$lt': operation.value}; 24 1 : }, isLessThanOrEqual: (operation) { 25 4 : parameter[fieldUid] = {'\$lte': operation.value}; 26 1 : }, isGreaterThan: (operation) { 27 4 : parameter[fieldUid] = {'\$gt': operation.value}; 28 1 : }, isGreaterThanOrEqual: (operation) { 29 4 : parameter[fieldUid] = {'\$gte': operation.value}; 30 1 : }, exists: (operation) { 31 4 : parameter[fieldUid] = {'\$exists': operation.value}; 32 1 : }, matches: (operation) { 33 4 : parameter[fieldUid] = {'\$regex': operation.regex}; 34 : }); 35 : } 36 : } 37 : 38 : /// 39 : /// The number of objects to skip before returning any. 40 : /// [skipCount] No of objects to skip from returned objects 41 : /// you can chain this call using double dot. 42 : /// 43 : /// The skip parameter can be used for pagination, 44 : /// skip specifies the number of objects to skip in the response. 45 : /// Example: 46 : /// 47 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 48 : /// final query = stack.contentType("contentTypeUid").entry().query(); 49 : /// query.skip(2); 50 : /// 51 1 : void skip(int skipCount) { 52 3 : queryParameter['skip'] = skipCount.toString(); 53 : } 54 : 55 : /// 56 : /// A limit on the number of objects to return. 57 : /// [limitCount] No of objects to limit 58 : /// you can chain this call by using double dot 59 : /// The limit parameter can be used for pagination, limit specifies the number of objects to limit to in the response. 60 : /// 61 : /// Example: 62 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 63 : /// final query = stack.contentType("contentTypeUid").entry().query(); 64 : /// query.limit(2); 65 : /// 66 1 : void limit(int limitCount) { 67 3 : queryParameter['limit'] = limitCount.toString(); 68 : } 69 : 70 : /// 71 : /// Sort the results in ascending order with the given key. 72 : /// Sort the returned entries in ascending order of the provided key. 73 : /// [key] The key to order by. 74 : /// you can chain this call by using double dot 75 : /// 76 : /// Example: 77 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 78 : /// final query = stack.contentType("contentTypeUid").entry().query(); 79 : /// query.orderByAscending('ascendingByKey'); 80 : /// 81 1 : void orderByAscending(String key) { 82 3 : queryParameter['asc'] = key.toString(); 83 : } 84 : 85 : /// 86 : /// Sort the results in descending order with the given key. 87 : /// Sort the returned entries in descending order of the provided key. 88 : /// [key] The key to order by. 89 : /// you can chain this call by using double dot 90 : /// 91 : /// Example: 92 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 93 : /// final query = stack.contentType("contentTypeUid").entry().query(); 94 : /// query.orderByDecending('descendingByKey'); 95 : /// 96 1 : void orderByDescending(String key) { 97 3 : queryParameter['desc'] = key.toString(); 98 : } 99 : 100 : /// 101 : /// This method adds key and value to an Entry. 102 : /// [key] The key as string which needs to be added to the Query 103 : /// [value] The value as string which needs to be added to the Query 104 : /// 105 : /// Example: 106 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 107 : /// final query = stack.contentType("contentTypeUid").entry().query(); 108 : /// query.param('key', 'value'); 109 : /// 110 1 : void param(String key, String value) { 111 2 : if (key != null && value != null && key.isNotEmpty && value.isNotEmpty) { 112 3 : queryParameter[key] = value.toString(); 113 : } 114 : } 115 : 116 : /// 117 : /// This method adds key and value to an Entry. 118 : /// [parameters] The key and value pair that will be added to the Query 119 : /// 120 : /// Example: 121 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 122 : /// final query = stack.contentType("contentTypeUid").entry().query(); 123 : /// query.addParam({key: value, key1: value2}); 124 : /// 125 1 : void addParams(Map parameters) { 126 1 : if (parameters != null && parameters.isNotEmpty) { 127 2 : parameters.forEach((key, value) { 128 2 : queryParameter[key] = value; 129 : }); 130 : } 131 : } 132 : 133 : /// 134 : /// Add a custom query against specified key. 135 : /// [key] key for the query 136 : /// [value] value for the query 137 : /// 138 : /// Example: 139 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 140 : /// final query = stack.contentType("contentTypeUid").entry().query(); 141 : /// query.addQuery("query_param_key", "query_param_value"); 142 : /// 143 1 : void query(String key, String value) { 144 2 : if (key != null && value != null && key.isNotEmpty && value.isNotEmpty) { 145 3 : parameter[key] = value.toString(); 146 : } 147 : } 148 : 149 : /// 150 : /// Add a custom query against specified key. 151 : /// [parameters] The key and value pair that will be added to the Query 152 : /// 153 : /// Example: 154 : /// final stack = contentstack.Stack( "apiKey", "deliveryToken", "environment"); 155 : /// final query = stack.contentType("contentTypeUid").entry().query(); 156 : /// query.addQuery("query_param_key", "query_param_value"); 157 : /// 158 1 : void addQuery(Map parameters) { 159 1 : if (parameters != null && parameters.isNotEmpty) { 160 2 : parameters.forEach((key, value) { 161 2 : parameter[key] = value; 162 : }); 163 : } 164 : } 165 : }