first commit
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:enaklo_pos/core/constants/variables.dart';
|
||||
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/order_remote_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/payment_method_response_model.dart';
|
||||
import 'package:enaklo_pos/data/models/response/summary_response_model.dart';
|
||||
import 'package:enaklo_pos/presentation/home/models/order_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class OrderRemoteDatasource {
|
||||
//save order to remote server
|
||||
Future<bool> saveOrder(OrderModel orderModel) async {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
print("π API CALL: saveOrder");
|
||||
print("π‘ URL: ${Variables.baseUrl}/api/save-order");
|
||||
print("π Token: ${authData.token?.substring(0, 20)}...");
|
||||
try {
|
||||
final response = await http.post(
|
||||
Uri.parse('${Variables.baseUrl}/api/save-order'),
|
||||
body: orderModel.toJson(),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
print("π₯ HTTP Status Code: ${response.statusCode}");
|
||||
print("π₯ Response Body: ${response.body}");
|
||||
print("π₯ Response Headers: ${response.headers}");
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print("β
API call successful - Order saved to server");
|
||||
return true;
|
||||
} else {
|
||||
print("β API call failed - Status: ${response.statusCode}");
|
||||
print("β Error Response: ${response.body}");
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
print("π₯ API call error: $e");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<String, OrderResponseModel>> getOrderByRangeDate(
|
||||
String stratDate,
|
||||
String endDate,
|
||||
) async {
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final response = await http.get(
|
||||
Uri.parse(
|
||||
'${Variables.baseUrl}/api/orders?start_date=$stratDate&end_date=$endDate'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
print("π₯ HTTP Status Code: ${response.statusCode}");
|
||||
print("π₯ Response Body: ${response.body}");
|
||||
print("π₯ Response Headers: ${response.headers}");
|
||||
if (response.statusCode == 200) {
|
||||
print("β
getOrderByRangeDate API call successful");
|
||||
return Right(OrderResponseModel.fromJson(response.body));
|
||||
} else {
|
||||
print("β getOrderByRangeDate API call failed - Status: ${response.statusCode}");
|
||||
print("β Error Response: ${response.body}");
|
||||
return const Left("Failed Load Data");
|
||||
}
|
||||
} catch (e) {
|
||||
print("π₯ getOrderByRangeDate API call error: $e");
|
||||
return Left("Failed: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<String, SummaryResponseModel>> getSummaryByRangeDate(
|
||||
String stratDate,
|
||||
String endDate,
|
||||
) async {
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final response = await http.get(
|
||||
Uri.parse(
|
||||
'${Variables.baseUrl}/api/summary?start_date=$stratDate&end_date=$endDate'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
print("π‘ URL: ${response.request!.url}");
|
||||
print("π₯ HTTP Status Code: ${response.statusCode}");
|
||||
print("π₯ Response Body: ${response.body}");
|
||||
print("π₯ Response Headers: ${response.headers}");
|
||||
if (response.statusCode == 200) {
|
||||
print("β
getSummaryByRangeDate API call successful");
|
||||
return Right(SummaryResponseModel.fromJson(response.body));
|
||||
} else {
|
||||
print("β getSummaryByRangeDate API call failed - Status: ${response.statusCode}");
|
||||
print("β Error Response: ${response.body}");
|
||||
return const Left("Failed Load Data");
|
||||
}
|
||||
} catch (e) {
|
||||
print("π₯ getSummaryByRangeDate API call error: $e");
|
||||
return Left("Failed: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<String, PaymentMethodResponseModel>> getPaymentMethodByRangeDate(
|
||||
String startDate,
|
||||
String endDate,
|
||||
) async {
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final response = await http.get(
|
||||
Uri.parse(
|
||||
'${Variables.baseUrl}/api/order-paymentmethod?start_date=$startDate&end_date=$endDate'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
print("π₯ Payment Method HTTP Status Code: ${response.statusCode}");
|
||||
print("π₯ Payment Method Response Body: ${response.body}");
|
||||
print("π₯ Payment Method Response Headers: ${response.headers}");
|
||||
if (response.statusCode == 200) {
|
||||
print("β
getPaymentMethodByRangeDate API call successful");
|
||||
return Right(PaymentMethodResponseModel.fromJson(response.body));
|
||||
} else {
|
||||
print("β getPaymentMethodByRangeDate API call failed - Status: ${response.statusCode}");
|
||||
print("β Error Response: ${response.body}");
|
||||
return const Left("Failed Load Payment Method Data");
|
||||
}
|
||||
} catch (e) {
|
||||
print("π₯ getPaymentMethodByRangeDate API call error: $e");
|
||||
return Left("Failed: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<String, bool>> addOrderItems(
|
||||
int orderId,
|
||||
List<Map<String, dynamic>> orderItems,
|
||||
) async {
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final response = await http.post(
|
||||
Uri.parse('${Variables.baseUrl}/api/orders/add-items'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: jsonEncode({
|
||||
'order_id': orderId,
|
||||
'order_items': orderItems,
|
||||
}),
|
||||
);
|
||||
|
||||
print("π₯ Add Order Items HTTP Status Code: ${response.statusCode}");
|
||||
print("π₯ Add Order Items Response Body: ${response.body}");
|
||||
print("π₯ Add Order Items Response Headers: ${response.headers}");
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print("β
addOrderItems API call successful");
|
||||
return const Right(true);
|
||||
} else {
|
||||
print("β addOrderItems API call failed - Status: ${response.statusCode}");
|
||||
print("β Error Response: ${response.body}");
|
||||
return Left("Failed to add order items: ${response.body}");
|
||||
}
|
||||
} catch (e) {
|
||||
print("π₯ addOrderItems API call error: $e");
|
||||
return Left("Failed: $e");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user