refactoring
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'package:afterwork/data/models/user_model.dart';
|
||||
|
||||
import 'package:afterwork/core/constants/urls.dart';
|
||||
import 'package:afterwork/data/models/user_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../../core/errors/exceptions.dart';
|
||||
@@ -10,7 +11,8 @@ class UserRemoteDataSource {
|
||||
|
||||
UserRemoteDataSource(this.client);
|
||||
|
||||
Future<UserModel> authenticateUser(String email, String password) async {
|
||||
// Authentifier l'utilisateur
|
||||
Future<UserModel> authenticateUser(String email, String password, String userId) async {
|
||||
if (email.isEmpty || password.isEmpty) {
|
||||
throw Exception('Email ou mot de passe vide');
|
||||
}
|
||||
@@ -34,8 +36,9 @@ class UserRemoteDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer un utilisateur par ID
|
||||
Future<UserModel> getUser(String id) async {
|
||||
final response = await client.get(Uri.parse('${Urls.baseUrl}/user/$id'));
|
||||
final response = await client.get(Uri.parse('${Urls.baseUrl}/users/$id'));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return UserModel.fromJson(json.decode(response.body));
|
||||
@@ -43,4 +46,45 @@ class UserRemoteDataSource {
|
||||
throw ServerException();
|
||||
}
|
||||
}
|
||||
|
||||
// Créer un nouvel utilisateur
|
||||
Future<UserModel> createUser(UserModel user) async {
|
||||
final response = await client.post(
|
||||
Uri.parse('${Urls.baseUrl}/users'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode(user.toJson()),
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
return UserModel.fromJson(json.decode(response.body));
|
||||
} else {
|
||||
throw ServerException();
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour un utilisateur
|
||||
Future<UserModel> updateUser(UserModel user) async {
|
||||
final response = await client.put(
|
||||
Uri.parse('${Urls.baseUrl}/users/${user.userId}'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode(user.toJson()),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return UserModel.fromJson(json.decode(response.body));
|
||||
} else {
|
||||
throw ServerException();
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un utilisateur par ID
|
||||
Future<void> deleteUser(String id) async {
|
||||
final response = await client.delete(
|
||||
Uri.parse('${Urls.baseUrl}/users/$id'),
|
||||
);
|
||||
|
||||
if (response.statusCode != 204) {
|
||||
throw ServerException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user