1) RD Place order Api integration
This commit is contained in:
parent
961d1f249c
commit
476bfc4035
55
lib/controller/rd_processing_order_controller.dart
Normal file
55
lib/controller/rd_processing_order_controller.dart
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import 'package:cheminova/controller/rd_processing_service.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
import '../models/rd_order_item_model.dart';
|
||||||
|
import '../models/rd_placed_order_model.dart';
|
||||||
|
|
||||||
|
class RDOrderPlacedController extends GetxController {
|
||||||
|
final RDOrderPlacedService _rdOrderPlacedService = RDOrderPlacedService();
|
||||||
|
|
||||||
|
// Observable to hold the order details
|
||||||
|
var placedOrder1 = PlacedOrdersProcessing(
|
||||||
|
orderId: '66ffdd0dd315eb7de3092cd6',
|
||||||
|
invoiceItems: [
|
||||||
|
RDOrderItem(
|
||||||
|
productId: "66e27c190ccbf6e1c57c9fba",
|
||||||
|
sku: "SKU045",
|
||||||
|
name: "Testing",
|
||||||
|
categoryName: "Testing",
|
||||||
|
brandName: "Testing brand",
|
||||||
|
price: 232.0,
|
||||||
|
gst: 10,
|
||||||
|
hsnCode: 4004,
|
||||||
|
description: "",
|
||||||
|
image: [],
|
||||||
|
quantity: 1,
|
||||||
|
remainingQuantity: 1,
|
||||||
|
processquantity: 1,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
).obs;
|
||||||
|
|
||||||
|
var isLoading = false.obs;
|
||||||
|
|
||||||
|
// Method to place an order
|
||||||
|
Future<void> placeRDOrder() async {
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
String? token = prefs.getString('token');
|
||||||
|
isLoading.value = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Construct the order details from the observable variable
|
||||||
|
PlacedOrdersProcessing orderDetails = placedOrder1.value;
|
||||||
|
print("Order Details: ${orderDetails.toJson()}"); // Debugging the order details
|
||||||
|
|
||||||
|
// Call the service to place the order
|
||||||
|
await _rdOrderPlacedService.placRDeOrder(orderDetails, token!);
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
print("Error placing order: $e");
|
||||||
|
} finally {
|
||||||
|
isLoading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
40
lib/controller/rd_processing_service.dart
Normal file
40
lib/controller/rd_processing_service.dart
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:cheminova/models/rd_placed_order_model.dart';
|
||||||
|
import 'package:cheminova/utils/api_urls.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import '../models/rd_order_item_model.dart';
|
||||||
|
|
||||||
|
|
||||||
|
class RDOrderPlacedService {
|
||||||
|
final Dio _dio = Dio(); // Create Dio instance
|
||||||
|
|
||||||
|
Future<void> placRDeOrder(PlacedOrdersProcessing orderDetails, String token) async {
|
||||||
|
//try {
|
||||||
|
// logger.w("orderjson ${jsonEncode(orderDetails.toJson())}");
|
||||||
|
final response = await _dio.post(
|
||||||
|
'https://api.cnapp.co.in/api/pd-process-order', // Ensure this is your correct endpoint
|
||||||
|
data: jsonEncode(orderDetails.toJson()),
|
||||||
|
options: Options(
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer $token',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
//logger.w("Status code,${response.statusCode}");
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
|
||||||
|
throw Exception('Failed to RD place order');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// on DioException catch (e) {
|
||||||
|
// print("DioException: ${e.message}");
|
||||||
|
// throw Exception('Failed to place order: ${e.message}');
|
||||||
|
// }
|
||||||
|
// catch (e) {
|
||||||
|
// print("General Exception: ${e.toString()}");
|
||||||
|
// throw Exception('Failed to place order: ${e.toString()}');
|
||||||
|
// }
|
||||||
|
}
|
35
lib/models/rd_placed_order_model.dart
Normal file
35
lib/models/rd_placed_order_model.dart
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import 'package:cheminova/models/rd_order_item_model.dart';
|
||||||
|
|
||||||
|
class PlacedOrdersProcessing {
|
||||||
|
final String orderId;
|
||||||
|
final List<RDOrderItem> invoiceItems;
|
||||||
|
|
||||||
|
PlacedOrdersProcessing({
|
||||||
|
required this.orderId,
|
||||||
|
required this.invoiceItems,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Factory constructor for creating an instance from JSON
|
||||||
|
factory PlacedOrdersProcessing.fromJson(Map<String, dynamic> json) {
|
||||||
|
return PlacedOrdersProcessing(
|
||||||
|
orderId: json['orderId'] ?? '', // Handle missing or null values
|
||||||
|
invoiceItems: (json['invoiceItems'] as List)
|
||||||
|
.map((item) => RDOrderItem.fromJson(item))
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to convert instance to JSON
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'orderId': orderId,
|
||||||
|
'invoiceItems': invoiceItems.map((item) => item.toJson()).toList(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overriding toString method
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'PlacedOrdersProcessing(orderId: $orderId, invoiceItems: $invoiceItems)';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user