Application propre sans erreurs. Bonne base sur laquelle repartir de zero en cas de soucis majeurs.
This commit is contained in:
22
lib/data/datasources/user_remote_data_source.dart
Normal file
22
lib/data/datasources/user_remote_data_source.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'dart:convert';
|
||||
import 'package:afterwork/data/models/user_model.dart';
|
||||
import 'package:afterwork/core/constants/urls.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../../core/errors/exceptions.dart';
|
||||
|
||||
class UserRemoteDataSource {
|
||||
final http.Client client;
|
||||
|
||||
UserRemoteDataSource(this.client);
|
||||
|
||||
Future<UserModel> getUser(String id) async {
|
||||
final response = await client.get(Uri.parse('${Urls.baseUrl}/user/$id'));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return UserModel.fromJson(json.decode(response.body));
|
||||
} else {
|
||||
throw ServerException();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
lib/data/datasources/user_repository_impl.dart
Normal file
14
lib/data/datasources/user_repository_impl.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:afterwork/domain/entities/user.dart';
|
||||
import 'package:afterwork/domain/repositories/user_repository.dart';
|
||||
import 'package:afterwork/data/datasources/user_remote_data_source.dart';
|
||||
|
||||
class UserRepositoryImpl implements UserRepository {
|
||||
final UserRemoteDataSource remoteDataSource;
|
||||
|
||||
UserRepositoryImpl({required this.remoteDataSource});
|
||||
|
||||
@override
|
||||
Future<User> getUser(String id) async {
|
||||
return await remoteDataSource.getUser(id);
|
||||
}
|
||||
}
|
||||
26
lib/data/models/user_model.dart
Normal file
26
lib/data/models/user_model.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
import '../../domain/entities/user.dart';
|
||||
|
||||
class UserModel extends User {
|
||||
const UserModel({
|
||||
required super.id,
|
||||
required super.name,
|
||||
required super.email,
|
||||
});
|
||||
|
||||
factory UserModel.fromJson(Map<String, dynamic> json) {
|
||||
return UserModel(
|
||||
id: json['id'],
|
||||
name: json['name'],
|
||||
email: json['email'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'email': email,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user