LCOV - code coverage report
Current view: top level - lib/src - base_query.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 44 44 100.0 %
Date: 2021-05-07 19:45:01 Functions: 0 0 -

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

Generated by: LCOV version 1.15