LCOV - code coverage report
Current view: top level - client - collections_client.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 24 29 82.8 %
Date: 2021-04-14 08:03:52 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:stream_feed_dart/src/core/api/collections_api.dart';
       2             : import 'package:stream_feed_dart/src/core/http/token.dart';
       3             : import 'package:stream_feed_dart/src/core/models/collection_entry.dart';
       4             : import 'package:stream_feed_dart/src/core/util/token_helper.dart';
       5             : 
       6             : class CollectionsClient {
       7           2 :   CollectionsClient(this.collections, {this.userToken, this.secret})
       8             :       : assert(
       9           0 :           userToken != null || secret != null,
      10             :           'At least a secret or userToken must be provided',
      11             :         );
      12             :   final Token? userToken;
      13             :   final String? secret;
      14             :   final CollectionsApi collections;
      15             : 
      16             :   /// Add item to collection
      17             :   ///
      18             :   /// Usage:
      19             :   ///
      20             :   /// For example let's our CheeseBurger object to the food collection
      21             :   /// ```dart
      22             :   /// final cheeseBurger = await client.collections.add(
      23             :   ///   'food',
      24             :   ///   {
      25             :   ///     'name': 'Cheese Burger',
      26             :   ///     'ingredients': [
      27             :   ///       'cheese',
      28             :   ///       'burger',
      29             :   ///       'bread',
      30             :   ///       'lettuce',
      31             :   ///       'tomato',
      32             :   ///     ],
      33             :   ///   },
      34             :   ///   entryId: '123',
      35             :   /// );
      36             :   /// ```
      37             :   /// Example
      38             :   /// Parameters:
      39             :   /// - [collection] : collection name
      40             :   /// - [entryId] : entry id, if null a random id will be assigned to the item
      41             :   /// - [data] :  ObjectStore data
      42             :   ///
      43             :   /// API docs: [adding-collections](https://getstream.io/activity-feeds/docs/flutter-dart/collections_introduction/?language=dart#adding-collections)
      44           1 :   Future<CollectionEntry> add(
      45             :     String collection,
      46             :     Map<String, Object> data, {
      47             :     String? entryId,
      48             :     String? userId,
      49             :   }) {
      50           1 :     final entry = CollectionEntry(
      51             :       id: entryId,
      52             :       collection: collection,
      53             :       data: data,
      54             :     );
      55           1 :     final token = userToken ??
      56           0 :         TokenHelper.buildCollectionsToken(secret!, TokenAction.write);
      57           2 :     return collections.add(token, userId, entry);
      58             :   }
      59             : 
      60             :   /// Delete entry from collection
      61             :   ///
      62             :   /// ## Parameters
      63             :   /// [entryId] : Collection entry id
      64             :   /// [collection] : Collection name
      65             :   ///
      66             :   ///
      67             :   /// ## Usage:
      68             :   ///
      69             :   /// Let's delete the burger we created in the [add] example, like this:
      70             :   /// ```dart
      71             :   /// await client.collections.delete('food', 'cheese-burger');
      72             :   /// ```
      73             :   /// API docs: [removing-collections](https://getstream.io/activity-feeds/docs/flutter-dart/collections_introduction/?language=dart#removing-collections)
      74           1 :   Future<void> delete(String collection, String entryId) {
      75           1 :     final token = userToken ??
      76           0 :         TokenHelper.buildCollectionsToken(secret!, TokenAction.delete);
      77           2 :     return collections.delete(token, collection, entryId);
      78             :   }
      79             : 
      80             :   /// Get item from collection and sync data
      81             :   ///
      82             :   /// ### Parameters
      83             :   /// [collection] : collections name
      84             :   /// [entryId] : the id of the collection entry we want to get
      85             :   ///
      86             :   /// ### Usage
      87             :   /// ```dart
      88             :   /// final collection = await client.collections.get('food', 'cheese-burger');
      89             :   /// ```
      90           1 :   Future<CollectionEntry> get(String collection, String entryId) {
      91           1 :     final token = userToken ??
      92           0 :         TokenHelper.buildCollectionsToken(secret!, TokenAction.read);
      93           2 :     return collections.get(token, collection, entryId);
      94             :   }
      95             : 
      96             :   /// Update item in the object storage
      97             :   /// ### Parameters
      98             :   ///  - [collection] :  collection name
      99             :   ///  - [data] :  ObjectStore data
     100             :   ///  - [entryId] : Collection entry id
     101             :   ///
     102             :   /// ### Usage
     103             :   /// Let's update our cheeseburger
     104             :   /// ```dart
     105             :   ///   await client.collections.update('food', 'cheese-burger', {
     106             :   ///   'name': 'Cheese Burger',
     107             :   ///   'rating': '1 Star',
     108             :   /// });
     109             :   /// ```
     110             :   ///
     111             :   /// API docs : [updating-collections](https://getstream.io/activity-feeds/docs/flutter-dart/collections_introduction/?language=dart#updating-collections)
     112           1 :   Future<CollectionEntry> update(
     113             :     String? collection,
     114             :     String? entryId,
     115             :     Map<String, Object> data, {
     116             :     String? userId,
     117             :   }) {
     118           1 :     final entry = CollectionEntry(
     119             :       id: entryId,
     120             :       collection: collection,
     121             :       data: data,
     122             :     );
     123           1 :     final token = userToken ??
     124           0 :         TokenHelper.buildCollectionsToken(secret!, TokenAction.write);
     125           2 :     return collections.update(token, userId, entry);
     126             :   }
     127             : 
     128             :   //Serverside methods
     129             : 
     130             :   /// Remove all objects by id from the collection.
     131             :   ///
     132             :   /// ### Parameters:
     133             :   /// [collection] : collections name
     134             :   /// [ids] : an array of ids we want to delete
     135             :   /// ### Usage
     136             :   /// For example, to delete the entries with ID 123 and 124
     137             :   /// from visitor collection we do this
     138             :   /// ```dart
     139             :   /// await client.collections.deleteMany('visitor', ['123', '124']);
     140             :   /// ```
     141             :   /// API docs : [delete_many](https://getstream.io/activity-feeds/docs/flutter-dart/collections_batch/?language=dart#delete_many)
     142           1 :   Future<void> deleteMany(String collection, Iterable<String> ids) {
     143             :     //TODO: assert that secret is not null since it is a serverside method
     144             :     final token =
     145           2 :         TokenHelper.buildCollectionsToken(secret!, TokenAction.delete);
     146           2 :     return collections.deleteMany(token, collection, ids);
     147             :   }
     148             : 
     149             :   /// Select all objects with ids from the collection.
     150             :   ///
     151             :   /// ### Parameters
     152             :   /// [collection] : collection name
     153             :   /// [ids] : an array of ids
     154             :   /// ### Usage
     155             :   /// To select the entries with ID 123 and 124 from items collection
     156             :   ///  we do this:
     157             :   /// ```dart
     158             :   /// final objects = await client.collections.select('items', ['123', '124']);
     159             :   /// ```
     160             :   ///
     161             :   /// API docs: [select](https://getstream.io/activity-feeds/docs/flutter-dart/collections_batch/?language=dart#select)
     162           1 :   Future<List<CollectionEntry>> select(
     163             :       String collection, Iterable<String> ids) {
     164           2 :     final token = TokenHelper.buildCollectionsToken(secret!, TokenAction.read);
     165           2 :     return collections.select(token, collection, ids);
     166             :   }
     167             : 
     168             :   /// Upsert one or more items within a collection.
     169             :   ///
     170             :   /// ### Parameters
     171             :   /// - [collection] : collection name
     172             :   /// - [entries] : an array of collection entries
     173             :   ///
     174             :   /// ### Usage
     175             :   ///
     176             :   /// ```dart
     177             :   /// await client.collections.upsert('visitor', <CollectionEntry>[
     178             :   ///   const CollectionEntry(id: '123', data: {
     179             :   ///     'name': 'john',
     180             :   ///     'favorite_color': 'blue',
     181             :   ///   }),
     182             :   ///   const CollectionEntry(id: '124', data: {
     183             :   ///     'name': 'jane',
     184             :   ///     'favorite_color': 'purple',
     185             :   ///     'interests': ['fashion', 'jazz'],
     186             :   ///   }),
     187             :   /// ]);
     188             :   /// ```
     189             :   /// API docs : [upsert](https://getstream.io/activity-feeds/docs/flutter-dart/collections_batch/?language=dart#upsert)
     190           1 :   Future<void> upsert(String collection, Iterable<CollectionEntry> entries) {
     191           2 :     final token = TokenHelper.buildCollectionsToken(secret!, TokenAction.write);
     192           2 :     return collections.upsert(token, collection, entries);
     193             :   }
     194             : }

Generated by: LCOV version 1.15