course-work/lib/models/order.dart
2022-12-27 08:13:07 +03:00

65 lines
1.8 KiB
Dart

import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:selfprivacy/models/item.dart';
part 'order.g.dart';
@JsonSerializable()
class Order {
factory Order.fromJson(final Map<String, dynamic> json) =>
_$OrderFromJson(json);
Order({
required this.title,
required this.description,
required this.id,
required this.customerName,
required this.customerPhone,
required this.orderDate,
required this.deliveryDate,
required this.address,
required this.status,
required this.courierId,
required this.items,
});
final String title;
final String description;
@JsonKey(name: 'customer_name')
final String customerName;
@JsonKey(name: 'customer_phone')
final String customerPhone;
@JsonKey(name: 'order_date')
final String orderDate;
@JsonKey(name: 'delivery_date')
final String deliveryDate;
final String address;
final String status;
final int id;
final int courierId;
final List<Item> items;
// tojson
Map<String, dynamic> toJson() => _$OrderToJson(this);
Map<String, dynamic> toJsonAsNew() {
final Map<String, dynamic> data = _$OrderToJson(this);
// To "order_in" goes everything except items
data['order_in'] = {
'title': data['title'],
'description': data['description'],
'customer_name': data['customer_name'],
'customer_phone': data['customer_phone'],
'order_date': data['order_date'],
'delivery_date': data['delivery_date'],
'address': data['address'],
'status': data['status'],
'courier_id': data['courier_id'],
};
// To "order_items" goes only items
data['order_items'] = data['items'];
// Remove items from data
data.remove('items');
return data;
}
}