LCOV - code coverage report
Current view: top level - core/api - collections_api.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 64 64 100.0 %
Date: 2021-04-14 08:03:52 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:dio/dio.dart';
       2             : import 'package:stream_feed_dart/src/core/http/stream_http_client.dart';
       3             : import 'package:stream_feed_dart/src/core/http/token.dart';
       4             : import 'package:stream_feed_dart/src/core/models/collection_entry.dart';
       5             : import 'package:stream_feed_dart/src/core/util/extension.dart';
       6             : import 'package:stream_feed_dart/src/core/util/routes.dart';
       7             : 
       8             : class CollectionsApi {
       9           3 :   const CollectionsApi(this.client);
      10             : 
      11             :   final StreamHttpClient client;
      12             : 
      13           1 :   Future<CollectionEntry> add(
      14             :       Token token, String? userId, CollectionEntry entry) async {
      15           2 :     checkNotNull(entry.collection, "Collection name can't be null");
      16           1 :     checkArgument(
      17           2 :         entry.collection!.isNotEmpty, "Collection name can't be empty");
      18           2 :     checkNotNull(entry.data, "Collection data can't be null");
      19           3 :     final result = await client.post<Map>(
      20           2 :       Routes.buildCollectionsUrl(entry.collection),
      21           2 :       headers: {'Authorization': '$token'},
      22           1 :       data: {
      23           2 :         'data': entry.data,
      24           1 :         if (userId != null) 'user_id': userId,
      25           3 :         if (entry.id != null) 'id': entry.id,
      26             :       },
      27             :     );
      28           2 :     return CollectionEntry.fromJson(result.data as Map<String, dynamic>);
      29             :   }
      30             : 
      31           1 :   Future<Response> delete(
      32             :       Token token, String collection, String entryId) async {
      33           2 :     checkArgument(collection.isNotEmpty, "Collection name can't be empty");
      34           2 :     checkArgument(entryId.isNotEmpty, "Collection id can't be empty");
      35           2 :     return client.delete(
      36           2 :       Routes.buildCollectionsUrl('$collection/$entryId/'),
      37           2 :       headers: {'Authorization': '$token'},
      38             :     );
      39             :   }
      40             : 
      41           1 :   Future<Response> deleteMany(
      42             :       Token token, String collection, Iterable<String> entryIds) async {
      43           2 :     checkArgument(collection.isNotEmpty, "Collection name can't be empty");
      44           2 :     checkArgument(entryIds.isNotEmpty, "Collection ids can't be empty");
      45           2 :     return client.delete(
      46           1 :       Routes.buildCollectionsUrl(),
      47           2 :       headers: {'Authorization': '$token'},
      48           1 :       queryParameters: {
      49             :         'collection_name': collection,
      50           1 :         'ids': entryIds.join(','),
      51             :       },
      52             :     );
      53             :   }
      54             : 
      55           1 :   Future<CollectionEntry> get(
      56             :       Token token, String collection, String entryId) async {
      57           2 :     checkArgument(collection.isNotEmpty, "Collection name can't be empty");
      58           2 :     checkArgument(entryId.isNotEmpty, "Collection id can't be empty");
      59           3 :     final result = await client.get<Map>(
      60           2 :       Routes.buildCollectionsUrl('$collection/$entryId/'),
      61           2 :       headers: {'Authorization': '$token'},
      62             :     );
      63           2 :     return CollectionEntry.fromJson(result.data as Map<String, dynamic>);
      64             :   }
      65             : 
      66           1 :   Future<List<CollectionEntry>> select(
      67             :       Token token, String collection, Iterable<String> entryIds) async {
      68           2 :     checkArgument(collection.isNotEmpty, "Collection name can't be empty");
      69           2 :     checkArgument(entryIds.isNotEmpty, "Collection ids can't be empty");
      70           3 :     final result = await client.get<Map>(
      71           1 :       Routes.buildCollectionsUrl(),
      72           2 :       headers: {'Authorization': '$token'},
      73           1 :       queryParameters: {
      74           4 :         'foreign_ids': entryIds.map((id) => '$collection:$id').join(','),
      75             :       },
      76             :     );
      77           3 :     final data = (result.data!['response']['data'] as List)
      78           3 :         .map((e) => CollectionEntry.fromJson(e))
      79           1 :         .toList(growable: false);
      80             :     return data;
      81             :   }
      82             : 
      83           1 :   Future<CollectionEntry> update(
      84             :       Token token, String? userId, CollectionEntry entry) async {
      85           1 :     checkNotNull(entry, "Collection can't be null");
      86           2 :     checkNotNull(entry.collection, "Collection name can't be null");
      87           1 :     checkArgument(
      88           2 :         entry.collection!.isNotEmpty, "Collection name can't be empty");
      89           3 :     final result = await client.put<Map>(
      90           4 :       Routes.buildCollectionsUrl('${entry.collection}/${entry.id}/'),
      91           2 :       headers: {'Authorization': '$token'},
      92           1 :       data: {
      93           2 :         'data': entry.data,
      94           1 :         if (userId != null) 'user_id': userId,
      95             :       },
      96             :     );
      97           2 :     return CollectionEntry.fromJson(result.data as Map<String, dynamic>);
      98             :   }
      99             : 
     100           1 :   Future<Response> upsert(
     101             :       //TODO: Map<String, Iterable<CollectionEntry>>
     102             :       Token token,
     103             :       String collection,
     104             :       Iterable<CollectionEntry> entries) async {
     105           2 :     checkArgument(collection.isNotEmpty, "Collection name can't be empty");
     106           2 :     checkArgument(entries.isNotEmpty, "Collection data can't be empty");
     107           4 :     return client.post(Routes.buildCollectionsUrl(), headers: {
     108             :       'Authorization': '$token'
     109           2 :     }, data: {
     110           1 :       'data': {collection: entries}
     111             :     });
     112             :   }
     113             : }

Generated by: LCOV version 1.15