Refactor code structure for improved readability and maintainability
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_paths.dart';
|
||||
import '../../../core/api/guarded_api_call.dart';
|
||||
import '../domain/user_admin.dart';
|
||||
|
||||
final adminRepositoryProvider = Provider<AdminRepository>((ref) {
|
||||
return AdminRepository(ref.watch(apiClientProvider), ref);
|
||||
});
|
||||
|
||||
class AdminRepository {
|
||||
final ApiClient _apiClient;
|
||||
final Ref _ref;
|
||||
|
||||
AdminRepository(this._apiClient, this._ref);
|
||||
|
||||
Future<List<UserAdmin>> listUsers() async {
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.getJson(UserApiPaths.list),
|
||||
);
|
||||
return (data as List<dynamic>).map((e) => UserAdmin.fromJson(e as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
Future<UserAdmin> setRole(int userId, String newRole) async {
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.patchJson(UserApiPaths.setRole(userId), body: {'role': newRole}),
|
||||
);
|
||||
return UserAdmin.fromJson(data);
|
||||
}
|
||||
|
||||
Future<UserAdmin> setPremium(int userId, {required bool isPremium}) async {
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.patchJson(UserApiPaths.setPremium(userId), body: {'isPremium': isPremium}),
|
||||
);
|
||||
return UserAdmin.fromJson(data);
|
||||
}
|
||||
|
||||
Future<UserAdmin> createUser({
|
||||
required String username,
|
||||
required String email,
|
||||
required String password,
|
||||
String role = 'user',
|
||||
}) async {
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.postJson(UserApiPaths.list, body: {
|
||||
'username': username,
|
||||
'email': email,
|
||||
'password': password,
|
||||
'role': role,
|
||||
}),
|
||||
);
|
||||
return UserAdmin.fromJson(data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
Future<void> deleteUser(int userId) => guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.deleteJson(UserApiPaths.delete(userId)),
|
||||
);
|
||||
|
||||
/// Returns `{ temporaryPassword, to, subject, body }`.
|
||||
Future<Map<String, dynamic>> resetPassword(int userId) async {
|
||||
final result = await guardedApiCall<dynamic>(
|
||||
_ref,
|
||||
() => _apiClient.postJson(UserApiPaths.resetPassword(userId)),
|
||||
);
|
||||
return (result as Map<String, dynamic>);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user