mirror of
https://github.com/Uuttssaavv/flutter-clean-architecture-riverpod.git
synced 2025-08-16 11:21:55 +08:00
80 lines
2.4 KiB
Dart
80 lines
2.4 KiB
Dart
import 'package:flutter_project/shared/data/remote/remote.dart';
|
|
import 'package:flutter_project/shared/domain/models/either.dart';
|
|
import 'package:flutter_project/shared/domain/models/paginated_response.dart';
|
|
import 'package:flutter_project/shared/exceptions/http_exception.dart';
|
|
import 'package:flutter_project/shared/globals.dart';
|
|
|
|
abstract class DashboardDatasource {
|
|
Future<Either<AppException, PaginatedResponse>> fetchPaginatedProducts(
|
|
{required int skip});
|
|
Future<Either<AppException, PaginatedResponse>> searchPaginatedProducts(
|
|
{required int skip, required String query});
|
|
}
|
|
|
|
class DashboardRemoteDatasource extends DashboardDatasource {
|
|
final NetworkService networkService;
|
|
DashboardRemoteDatasource(this.networkService);
|
|
|
|
@override
|
|
Future<Either<AppException, PaginatedResponse>> fetchPaginatedProducts(
|
|
{required int skip}) async {
|
|
final response = await networkService.get(
|
|
'/products',
|
|
queryParameters: {
|
|
'skip': skip,
|
|
'limit': PRODUCTS_PER_PAGE,
|
|
},
|
|
);
|
|
|
|
return response.fold(
|
|
(l) => Left(l),
|
|
(r) {
|
|
final jsonData = r.data;
|
|
if (jsonData == null) {
|
|
return Left(
|
|
AppException(
|
|
identifier: 'fetchPaginatedData',
|
|
statusCode: 0,
|
|
message: 'The data is not in the valid format.',
|
|
),
|
|
);
|
|
}
|
|
final paginatedResponse =
|
|
PaginatedResponse.fromJson(jsonData, jsonData['products'] ?? []);
|
|
return Right(paginatedResponse);
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<Either<AppException, PaginatedResponse>> searchPaginatedProducts(
|
|
{required int skip, required String query}) async {
|
|
final response = await networkService.get(
|
|
'/products/search?q=$query',
|
|
queryParameters: {
|
|
'skip': skip,
|
|
'limit': PRODUCTS_PER_PAGE,
|
|
},
|
|
);
|
|
|
|
return response.fold(
|
|
(l) => Left(l),
|
|
(r) {
|
|
final jsonData = r.data;
|
|
if (jsonData == null) {
|
|
return Left(
|
|
AppException(
|
|
identifier: 'search PaginatedData',
|
|
statusCode: 0,
|
|
message: 'The data is not in the valid format.',
|
|
),
|
|
);
|
|
}
|
|
final paginatedResponse =
|
|
PaginatedResponse.fromJson(jsonData, jsonData['products'] ?? []);
|
|
return Right(paginatedResponse);
|
|
},
|
|
);
|
|
}
|
|
}
|