54 lines
1.5 KiB
Dart
54 lines
1.5 KiB
Dart
|
|
import 'dart:developer';
|
||
|
|
import 'package:dartz/dartz.dart';
|
||
|
|
import 'package:dio/dio.dart';
|
||
|
|
import 'package:enaklo_pos/core/network/dio_client.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');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|