apskel-pos-flutter/lib/data/datasources/table_remote_datasource.dart
2025-08-14 00:12:49 +07:00

171 lines
4.8 KiB
Dart

import 'dart:developer';
import 'dart:ui';
import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart';
import 'package:enaklo_pos/core/network/dio_client.dart';
import 'package:enaklo_pos/data/models/response/table_model.dart';
import '../../core/constants/variables.dart';
import 'auth_local_datasource.dart';
class TableRemoteDataSource {
final Dio dio = DioClient.instance;
Future<Either<String, bool>> createTable({
required String tableName,
required int capacity,
required String location,
}) async {
try {
final authData = await AuthLocalDataSource().getAuthData();
final url = '${Variables.baseUrl}/api/v1/tables';
final response = await dio.post(
url,
data: {
"outlet_id": authData.user?.outletId,
"table_name": tableName,
"capacity": capacity,
"location": location,
"status": "available",
"is_active": true,
"position_x": 200,
"position_y": 200,
},
options: Options(
headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
},
),
);
if (response.statusCode == 200 || response.statusCode == 201) {
return Right(true);
} else {
return const Left('Failed to create table');
}
} on DioException catch (e) {
log("Dio error: ${e.message}");
return Left(e.response?.data['message'] ?? 'Gagal membuat table');
} catch (e) {
log("Unexpected error: $e");
return const Left('Unexpected error occurred');
}
}
Future<Either<String, TableResponseModel>> getTable({
int page = 1,
int limit = 50,
String? status,
}) async {
try {
final authData = await AuthLocalDataSource().getAuthData();
final url = '${Variables.baseUrl}/api/v1/tables';
Map<String, dynamic> queryParameters = {
'page': page,
'limit': limit,
};
if (status != null) {
queryParameters['status'] = status;
}
final response = await dio.get(
url,
queryParameters: queryParameters,
options: Options(
headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
},
),
);
if (response.statusCode == 200) {
return Right(TableResponseModel.fromMap(response.data));
} else {
return const Left('Failed to get tables');
}
} on DioException catch (e) {
log("Dio error: ${e.message}");
return Left(e.response?.data['message'] ?? 'Gagal mengambil data meja');
} catch (e) {
log("Unexpected error: $e");
return const Left('Unexpected error occurred');
}
}
Future<Either<String, bool>> updatePosition({
required String tableId,
required Offset position,
}) async {
try {
final authData = await AuthLocalDataSource().getAuthData();
final url = '${Variables.baseUrl}/api/v1/tables/$tableId';
final response = await dio.put(
url,
data: {
"position_x": position.dx.round(),
"position_y": position.dy.round(),
},
options: Options(
headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
},
),
);
if (response.statusCode == 200 || response.statusCode == 201) {
return Right(true);
} else {
return const Left('Failed to create table');
}
} on DioException catch (e) {
log("Dio error: ${e.message}");
return Left(e.response?.data['message'] ?? 'Gagal membuat table');
} catch (e) {
log("Unexpected error: $e");
return const Left('Unexpected error occurred');
}
}
Future<Either<String, bool>> transferTable({
required String fromTableId,
required String toTableId,
}) async {
try {
final authData = await AuthLocalDataSource().getAuthData();
final url = '${Variables.baseUrl}/api/v1/tables/transfer';
final response = await dio.put(
url,
data: {
"from_table": fromTableId,
"to_table": toTableId,
},
options: Options(
headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
},
),
);
if (response.statusCode == 200 || response.statusCode == 201) {
return Right(true);
} else {
return const Left('Failed to create table');
}
} on DioException catch (e) {
log("Dio error: ${e.message}");
return Left(e.response?.data['message'] ?? 'Gagal membuat table');
} catch (e) {
log("Unexpected error: $e");
return const Left('Unexpected error occurred');
}
}
}