Advertisement
harmonyV

PostRepo

May 6th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.80 KB | None | 0 0
  1. @riverpod
  2. class PostRepository extends _$PostRepository {
  3.   static const _tableName = 'posts';
  4.  
  5.  
  6.   @override
  7.   Stream<List<Post>> build() {
  8.     return postsStream();
  9.   }
  10.  
  11.   Stream<List<Post>> postsStream({String? communityId}) {
  12.     final id = communityId ?? defaultCommunityId;
  13.  
  14.     return supabaseClient
  15.         .from(_tableName)
  16.         .stream(primaryKey: ['id'])
  17.         .eq('community_id', id)
  18.         .order('created_at', ascending: false)
  19.         .map(
  20.             (list) => list.map(Post.fromJson).toList())
  21.         .handleError((Object error, StackTrace stackTrace) {
  22.           logError(
  23.             error,
  24.             stack: stackTrace,
  25.             context: 'PostRepository._fetchPostsStream',
  26.             hints: {'communityId': communityId},
  27.           );
  28.         });
  29.   }
  30.  
  31.   Future<List<Post>> getPosts({String? communityId}) async {
  32.     final id = communityId ?? defaultCommunityId;
  33.  
  34.     final response = await supabaseClient
  35.         .from(_tableName)
  36.         .select()
  37.         .eq('community_id', id)
  38.         .order('created_at', ascending: false);
  39.  
  40.     return response.map(Post.fromJson).toList();
  41.   }
  42.  
  43.   Future<(ActionResult, String)?> createPost({
  44.     required Post post,
  45.   }) async {
  46.     final user = supabaseClient.auth.currentUser;
  47.  
  48.     if (user == null) {
  49.       return (ActionResult.error, 'User must be logged in to create a post.');
  50.     }
  51.  
  52.     try {
  53.       await supabaseClient
  54.           .from(_tableName)
  55.           .insert(post.copyWith(authorId: user.id).toJson());
  56.       return null;
  57.     } catch (error, stack) {
  58.       logError(
  59.         error,
  60.         stack: stack,
  61.         context: 'PostRepository.createPost',
  62.         hints: {'post': post.toJson()},
  63.       );
  64.       return (ActionResult.error, 'Error creating post: $error');
  65.     }
  66.   }
  67.  
  68.   Future<(ActionResult, String)?> updatePost(Post post) async {
  69.     try {
  70.       await supabaseClient
  71.           .from(_tableName)
  72.           .update(post.toJson())
  73.           .eq('id', post.id);
  74.       return null;
  75.     } catch (error, stack) {
  76.       logError(
  77.         error,
  78.         stack: stack,
  79.         context: 'PostRepository.updatePost',
  80.       );
  81.       return (ActionResult.error, 'Error updating post: $error');
  82.     }
  83.   }
  84.  
  85.   Future<(ActionResult, String)?> deletePost(String postId) async {
  86.     try {
  87.       await supabaseClient.from(_tableName).delete().eq('id', postId);
  88.       return null;
  89.     } catch (error, stack) {
  90.       logError(
  91.         error,
  92.         stack: stack,
  93.         context: 'PostRepository.deletePost',
  94.       );
  95.       return (ActionResult.error, 'Error deleting post: $error');
  96.     } finally {
  97.       ref.invalidateSelf();
  98.     }
  99.   }
  100.  
  101.   Future<void> refresh() async {
  102.     state = const AsyncLoading();
  103.     await Future<void>.delayed(refreshDelay);
  104.     ref.invalidateSelf();
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement