first commit
This commit is contained in:
112
lib/features/admin/data/services/admin_service.dart
Normal file
112
lib/features/admin/data/services/admin_service.dart
Normal file
@@ -0,0 +1,112 @@
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
import '../../../music/domain/models/track_model.dart';
|
||||
import '../../domain/models/admin_panel_data_model.dart';
|
||||
import '../../domain/models/admin_post_model.dart';
|
||||
import '../../domain/models/admin_user_model.dart';
|
||||
|
||||
class AdminService {
|
||||
const AdminService(this._client);
|
||||
|
||||
final SupabaseClient _client;
|
||||
|
||||
Future<AdminPanelDataModel> fetchPanelData() async {
|
||||
// Fetch all profiles
|
||||
final profileRows = List<Map<String, dynamic>>.from(
|
||||
await _client.from('profiles').select('user_id, username, avatar_url, banned, featured'),
|
||||
);
|
||||
|
||||
// Fetch latest posts
|
||||
final postRows = List<Map<String, dynamic>>.from(
|
||||
await _client
|
||||
.from('posts')
|
||||
.select('id, user_id, caption, image_url, likes_count, featured')
|
||||
.order('created_at', ascending: false)
|
||||
.limit(100),
|
||||
);
|
||||
|
||||
// Fetch tracks
|
||||
final trackRows = List<Map<String, dynamic>>.from(
|
||||
await _client
|
||||
.from('tracks')
|
||||
.select('*, profiles(username)')
|
||||
.order('created_at', ascending: false)
|
||||
.limit(100),
|
||||
);
|
||||
|
||||
final userMap = <String, AdminUserModel>{};
|
||||
for (final row in profileRows) {
|
||||
final user = AdminUserModel(
|
||||
userId: row['user_id'] as String,
|
||||
username: (row['username'] as String?) ?? 'RIOTER',
|
||||
avatarUrl: (row['avatar_url'] as String?) ?? '',
|
||||
banned: (row['banned'] as bool?) ?? false,
|
||||
featured: (row['featured'] as bool?) ?? false,
|
||||
);
|
||||
userMap[user.userId] = user;
|
||||
}
|
||||
|
||||
final users = userMap.values.toList()
|
||||
..sort((a, b) => a.username.toLowerCase().compareTo(b.username.toLowerCase()));
|
||||
|
||||
final posts = postRows.map((row) {
|
||||
final user = userMap[row['user_id']] ??
|
||||
AdminUserModel(
|
||||
userId: row['user_id'] as String,
|
||||
username: 'RIOTER',
|
||||
avatarUrl: '',
|
||||
banned: false,
|
||||
featured: false,
|
||||
);
|
||||
return AdminPostModel(
|
||||
id: row['id'] as String,
|
||||
userId: row['user_id'] as String,
|
||||
username: user.username,
|
||||
imageUrl: (row['image_url'] as String?) ?? '',
|
||||
caption: (row['caption'] as String?) ?? '',
|
||||
likesCount: (row['likes_count'] as int?) ?? 0,
|
||||
featured: (row['featured'] as bool?) ?? false,
|
||||
);
|
||||
}).toList();
|
||||
|
||||
final tracks = trackRows.map((row) {
|
||||
final username = (row['profiles'] as Map<String, dynamic>?)?['username'] as String?;
|
||||
return TrackModel.fromJson(row, username: username);
|
||||
}).toList();
|
||||
|
||||
return AdminPanelDataModel(users: users, posts: posts, tracks: tracks);
|
||||
}
|
||||
|
||||
Future<void> deletePost(String postId) async {
|
||||
await _client.from('posts').delete().eq('id', postId);
|
||||
}
|
||||
|
||||
Future<void> setUserBanned({
|
||||
required String userId,
|
||||
required bool banned,
|
||||
}) async {
|
||||
await _client.from('profiles').update({'banned': banned}).eq('user_id', userId);
|
||||
}
|
||||
|
||||
Future<void> setUserFeatured({
|
||||
required String userId,
|
||||
required bool featured,
|
||||
}) async {
|
||||
await _client.from('profiles').update({'featured': featured}).eq('user_id', userId);
|
||||
}
|
||||
|
||||
Future<void> setPostFeatured({
|
||||
required String postId,
|
||||
required bool featured,
|
||||
}) async {
|
||||
await _client.from('posts').update({'featured': featured}).eq('id', postId);
|
||||
}
|
||||
|
||||
Future<void> setTrackFeatured({
|
||||
required String trackId,
|
||||
required bool featured,
|
||||
}) async {
|
||||
// Assuming tracks table has a 'featured' column
|
||||
await _client.from('tracks').update({'featured': featured}).eq('id', trackId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user