-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4caa48c
commit d71ea96
Showing
8 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
|
||
extension FirestoreExt on Query<Map<String, dynamic>> { | ||
Future<QueryDocumentSnapshot<Map<String, dynamic>>?> get firstDocOrNull => | ||
get().then((snapshots) => snapshots.docs.firstOrNull); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:poster/core/data/firestore/utils/firestore_ext.dart'; | ||
import 'package:poster/core/data/paging/firestore_paging.dart'; | ||
import 'package:poster/core/data/paging/page_mapper.dart'; | ||
import 'package:poster/core/domain/paging/page_data.dart'; | ||
import 'package:poster/core/domain/paging/paging_config.dart'; | ||
import 'package:poster/core/domain/profile/data_source/follower_api.dart'; | ||
import 'package:poster/core/domain/profile/entity/following_data.dart'; | ||
import 'package:poster/core/utils/functions/try_future.dart'; | ||
|
||
const _collectionFollowers = 'followers'; | ||
|
||
final class FollowerApiImpl with FollowerApi { | ||
@override | ||
Future<Either<Exception, FollowingData>> subscribe({ | ||
required String profileEmail, | ||
required String followerEmail, | ||
}) => tryFuture(() async { | ||
final data = FollowingData(followerEmail: followerEmail, profileEmail: profileEmail); | ||
|
||
await FirebaseFirestore.instance | ||
.collection(_collectionFollowers) | ||
.add(data.toJson()); | ||
|
||
return data; | ||
}); | ||
|
||
@override | ||
Future<Either<Exception, void>> unsubscribe({ | ||
required String profileEmail, | ||
required String followerEmail, | ||
}) => tryFuture(() async { | ||
final subscribeDoc = await FirebaseFirestore.instance | ||
.collection(_collectionFollowers) | ||
.where(FollowingData.fieldProfileEmail, isEqualTo: profileEmail) | ||
.where(FollowingData.fieldFollowerEmail, isEqualTo: followerEmail) | ||
.firstDocOrNull; | ||
|
||
await subscribeDoc?.reference.delete(); | ||
}); | ||
|
||
@override | ||
Future<Either<Exception, PageData<FollowingData>>> subscribersPage({ | ||
required String profileEmail, | ||
int page = PagingConfig.initialPage, | ||
int perPage = PagingConfig.defaultPageSize, | ||
}) => tryFuture(() async { | ||
final snapshots = await FirebaseFirestore.instance | ||
.collection(_collectionFollowers) | ||
.where(FollowingData.fieldProfileEmail, isEqualTo: profileEmail) | ||
.pageAt(page: page, perPage: perPage); | ||
|
||
return snapshots.toPageData( | ||
page: page, | ||
perPage: perPage, | ||
transform: (q) => q.toFollowingData(), | ||
); | ||
}); | ||
} | ||
|
||
extension _FollowingMapper on QueryDocumentSnapshot<Map<String, dynamic>> { | ||
FollowingData toFollowingData() => FollowingData.fromJson(data()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:poster/core/domain/paging/page_data.dart'; | ||
import 'package:poster/core/domain/paging/paging_config.dart'; | ||
import 'package:poster/core/domain/profile/entity/following_data.dart'; | ||
|
||
mixin FollowerApi { | ||
Future<Either<Exception, FollowingData>> subscribe({ | ||
required String profileEmail, | ||
required String followerEmail, | ||
}); | ||
|
||
Future<Either<Exception, void>> unsubscribe({ | ||
required String profileEmail, | ||
required String followerEmail, | ||
}); | ||
|
||
Future<Either<Exception, PageData<FollowingData>>> subscribersPage({ | ||
required String profileEmail, | ||
int page = PagingConfig.initialPage, | ||
int perPage = PagingConfig.defaultPageSize, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
part 'following_data.freezed.dart'; | ||
part 'following_data.g.dart'; | ||
|
||
@freezed | ||
abstract class FollowingData with _$FollowingData { | ||
static const fieldFollowerEmail = 'follower_email'; | ||
static const fieldProfileEmail = 'profile_email'; | ||
|
||
const factory FollowingData({ | ||
@JsonKey(name: FollowingData.fieldFollowerEmail) required String followerEmail, | ||
@JsonKey(name: FollowingData.fieldProfileEmail) required String profileEmail, | ||
}) = _FollowingData; | ||
|
||
factory FollowingData.fromJson(Map<String, dynamic> json) => | ||
_$FollowingDataFromJson(json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters