2025-07-30 22:38:44 +07:00
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
|
|
import 'package:dartz/dartz.dart';
|
2025-08-05 19:20:45 +07:00
|
|
|
import 'package:dio/dio.dart';
|
2025-07-30 22:38:44 +07:00
|
|
|
import 'package:enaklo_pos/core/constants/variables.dart';
|
2025-08-05 19:20:45 +07:00
|
|
|
import 'package:enaklo_pos/core/network/dio_client.dart';
|
2025-07-30 22:38:44 +07:00
|
|
|
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
|
|
|
|
import 'package:enaklo_pos/data/models/response/category_response_model.dart';
|
|
|
|
|
|
|
|
|
|
class CategoryRemoteDatasource {
|
2025-08-05 19:20:45 +07:00
|
|
|
final Dio dio = DioClient.instance;
|
|
|
|
|
|
|
|
|
|
Future<Either<String, CategoryResponseModel>> getCategories({
|
|
|
|
|
int page = 1,
|
|
|
|
|
int limit = 10,
|
|
|
|
|
bool isActive = true,
|
|
|
|
|
}) async {
|
2025-07-30 22:38:44 +07:00
|
|
|
final authData = await AuthLocalDataSource().getAuthData();
|
2025-08-05 19:20:45 +07:00
|
|
|
final headers = {
|
2025-07-30 22:38:44 +07:00
|
|
|
'Authorization': 'Bearer ${authData.token}',
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
};
|
2025-08-05 19:20:45 +07:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final response = await dio.get(
|
|
|
|
|
'${Variables.baseUrl}/api/v1/categories',
|
|
|
|
|
queryParameters: {
|
|
|
|
|
'page': page,
|
|
|
|
|
'limit': limit,
|
|
|
|
|
'is_active': isActive,
|
|
|
|
|
},
|
|
|
|
|
options: Options(headers: headers),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return right(CategoryResponseModel.fromMap(response.data));
|
|
|
|
|
} else {
|
|
|
|
|
return left(response.data.toString());
|
|
|
|
|
}
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
log('Dio error: ${e.message}');
|
|
|
|
|
return left(e.response?.data.toString() ?? e.message ?? 'Unknown error');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log('Unexpected error: $e');
|
|
|
|
|
return left('Unexpected error occurred');
|
2025-07-30 22:38:44 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|