2025-08-03 23:33:00 +07:00
|
|
|
import 'dart:developer';
|
2025-08-04 00:09:51 +07:00
|
|
|
import 'dart:ui';
|
2025-08-03 23:33:00 +07:00
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:enaklo_pos/core/network/dio_client.dart';
|
2025-08-03 23:53:06 +07:00
|
|
|
import 'package:enaklo_pos/data/models/response/table_model.dart';
|
2025-08-03 23:33:00 +07:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-03 23:53:06 +07:00
|
|
|
|
|
|
|
|
Future<Either<String, TableResponseModel>> getTable({
|
|
|
|
|
int page = 1,
|
2025-08-14 00:12:49 +07:00
|
|
|
int limit = 50,
|
2025-08-04 00:50:10 +07:00
|
|
|
String? status,
|
2025-08-03 23:53:06 +07:00
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
final authData = await AuthLocalDataSource().getAuthData();
|
|
|
|
|
final url = '${Variables.baseUrl}/api/v1/tables';
|
|
|
|
|
|
2025-08-04 00:50:10 +07:00
|
|
|
Map<String, dynamic> queryParameters = {
|
|
|
|
|
'page': page,
|
|
|
|
|
'limit': limit,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (status != null) {
|
|
|
|
|
queryParameters['status'] = status;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 23:53:06 +07:00
|
|
|
final response = await dio.get(
|
|
|
|
|
url,
|
2025-08-04 00:50:10 +07:00
|
|
|
queryParameters: queryParameters,
|
2025-08-03 23:53:06 +07:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-04 00:09:51 +07:00
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-08 13:45:03 +07:00
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-03 23:33:00 +07:00
|
|
|
}
|