feat: get table
This commit is contained in:
@@ -358,19 +358,19 @@ class ProductLocalDatasource {
|
||||
|
||||
// generate table managent with count
|
||||
Future<void> createTableManagement(String tableName, Offset position) async {
|
||||
final db = await instance.database;
|
||||
TableModel newTable = TableModel(
|
||||
tableName: tableName,
|
||||
status: 'available',
|
||||
orderId: 0,
|
||||
paymentAmount: 0,
|
||||
startTime: DateTime.now().toIso8601String(),
|
||||
position: position,
|
||||
);
|
||||
await db.insert(
|
||||
tableManagement,
|
||||
newTable.toMap(),
|
||||
);
|
||||
// final db = await instance.database;
|
||||
// TableModel newTable = TableModel(
|
||||
// tableName: tableName,
|
||||
// status: 'available',
|
||||
// orderId: 0,
|
||||
// paymentAmount: 0,
|
||||
// startTime: DateTime.now().toIso8601String(),
|
||||
// position: position,
|
||||
// );
|
||||
// await db.insert(
|
||||
// tableManagement,
|
||||
// newTable.toMap(),
|
||||
// );
|
||||
}
|
||||
|
||||
// change position table
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:developer';
|
||||
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';
|
||||
|
||||
@@ -50,4 +51,41 @@ class TableRemoteDataSource {
|
||||
return const Left('Unexpected error occurred');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<String, TableResponseModel>> getTable({
|
||||
int page = 1,
|
||||
int limit = 10,
|
||||
}) async {
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final url = '${Variables.baseUrl}/api/v1/tables';
|
||||
|
||||
final response = await dio.get(
|
||||
url,
|
||||
queryParameters: {
|
||||
'page': page,
|
||||
'limit': limit,
|
||||
'outlet_id': authData.user?.outletId,
|
||||
},
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,74 +1,132 @@
|
||||
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
||||
import 'dart:ui';
|
||||
import 'dart:convert';
|
||||
|
||||
class TableResponseModel {
|
||||
final bool? success;
|
||||
final TableData? data;
|
||||
final dynamic errors;
|
||||
|
||||
TableResponseModel({
|
||||
this.success,
|
||||
this.data,
|
||||
this.errors,
|
||||
});
|
||||
|
||||
factory TableResponseModel.fromJson(String str) =>
|
||||
TableResponseModel.fromMap(json.decode(str));
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory TableResponseModel.fromMap(Map<String, dynamic> json) =>
|
||||
TableResponseModel(
|
||||
success: json["success"],
|
||||
data: json["data"] == null ? null : TableData.fromMap(json["data"]),
|
||||
errors: json["errors"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"success": success,
|
||||
"data": data?.toMap(),
|
||||
"errors": errors,
|
||||
};
|
||||
}
|
||||
|
||||
class TableData {
|
||||
final List<TableModel>? tables;
|
||||
final int? totalCount;
|
||||
final int? page;
|
||||
final int? limit;
|
||||
final int? totalPages;
|
||||
|
||||
TableData({
|
||||
this.tables,
|
||||
this.totalCount,
|
||||
this.page,
|
||||
this.limit,
|
||||
this.totalPages,
|
||||
});
|
||||
|
||||
factory TableData.fromMap(Map<String, dynamic> json) => TableData(
|
||||
tables: json["tables"] == null
|
||||
? []
|
||||
: List<TableModel>.from(
|
||||
json["tables"].map((x) => TableModel.fromMap(x))),
|
||||
totalCount: json["total_count"],
|
||||
page: json["page"],
|
||||
limit: json["limit"],
|
||||
totalPages: json["total_pages"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"tables": tables == null
|
||||
? []
|
||||
: List<dynamic>.from(tables!.map((x) => x.toMap())),
|
||||
"total_count": totalCount,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total_pages": totalPages,
|
||||
};
|
||||
}
|
||||
|
||||
class TableModel {
|
||||
int? id;
|
||||
final String tableName;
|
||||
final String startTime;
|
||||
final String status;
|
||||
final int orderId;
|
||||
final int paymentAmount;
|
||||
final Offset position;
|
||||
final String? id;
|
||||
final String? organizationId;
|
||||
final String? outletId;
|
||||
final String? tableName;
|
||||
final String? status;
|
||||
final int? paymentAmount;
|
||||
final double? positionX;
|
||||
final double? positionY;
|
||||
final int? capacity;
|
||||
final bool? isActive;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
TableModel({
|
||||
this.id,
|
||||
required this.tableName,
|
||||
required this.startTime,
|
||||
required this.status,
|
||||
required this.orderId,
|
||||
required this.paymentAmount,
|
||||
required this.position,
|
||||
this.organizationId,
|
||||
this.outletId,
|
||||
this.tableName,
|
||||
this.status,
|
||||
this.paymentAmount,
|
||||
this.positionX,
|
||||
this.positionY,
|
||||
this.capacity,
|
||||
this.isActive,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
@override
|
||||
factory TableModel.fromMap(Map<String, dynamic> json) => TableModel(
|
||||
id: json["id"],
|
||||
organizationId: json["organization_id"],
|
||||
outletId: json["outlet_id"],
|
||||
tableName: json["table_name"],
|
||||
status: json["status"],
|
||||
paymentAmount: json["payment_amount"],
|
||||
positionX: json["position_x"]?.toDouble(),
|
||||
positionY: json["position_y"]?.toDouble(),
|
||||
capacity: json["capacity"],
|
||||
isActive: json["is_active"],
|
||||
createdAt: json["created_at"] == null
|
||||
? null
|
||||
: DateTime.parse(json["created_at"]),
|
||||
updatedAt: json["updated_at"] == null
|
||||
? null
|
||||
: DateTime.parse(json["updated_at"]),
|
||||
);
|
||||
|
||||
// from map
|
||||
factory TableModel.fromMap(Map<String, dynamic> map) {
|
||||
return TableModel(
|
||||
id: map['id'],
|
||||
tableName: map['table_name'],
|
||||
startTime: map['start_time'],
|
||||
status: map['status'],
|
||||
orderId: map['order_id'],
|
||||
paymentAmount: map['payment_amount'],
|
||||
position: Offset(map['x_position'], map['y_position']),
|
||||
);
|
||||
}
|
||||
|
||||
// to map
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'table_name': tableName,
|
||||
'status': status,
|
||||
'start_time': startTime,
|
||||
'order_id': orderId,
|
||||
'payment_amount': paymentAmount,
|
||||
'x_position': position.dx,
|
||||
'y_position': position.dy,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(covariant TableModel other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other.id == id &&
|
||||
other.tableName == tableName &&
|
||||
other.startTime == startTime &&
|
||||
other.status == status &&
|
||||
other.orderId == orderId &&
|
||||
other.paymentAmount == paymentAmount &&
|
||||
other.position == position;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return id.hashCode ^
|
||||
tableName.hashCode ^
|
||||
startTime.hashCode ^
|
||||
status.hashCode ^
|
||||
orderId.hashCode ^
|
||||
paymentAmount.hashCode ^
|
||||
position.hashCode;
|
||||
}
|
||||
Map<String, dynamic> toMap() => {
|
||||
"id": id,
|
||||
"organization_id": organizationId,
|
||||
"outlet_id": outletId,
|
||||
"table_name": tableName,
|
||||
"status": status,
|
||||
"payment_amount": paymentAmount,
|
||||
"position_x": positionX,
|
||||
"position_y": positionY,
|
||||
"capacity": capacity,
|
||||
"is_active": isActive,
|
||||
"created_at": createdAt?.toIso8601String(),
|
||||
"updated_at": updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user