add items order

This commit is contained in:
efrilm
2025-10-28 20:55:10 +07:00
parent 0693411cf7
commit adc90a90a0
27 changed files with 2377 additions and 70 deletions
@@ -173,4 +173,33 @@ class OrderRemoteDataProvider {
return DC.error(OrderFailure.serverError(e));
}
}
Future<DC<OrderFailure, OrderDto>> addItemOrder({
required String id,
required List<AddItemOrderRequestDto> request,
}) async {
try {
final response = await _apiClient.post(
'${ApiPath.orders}/$id/add-items',
data: {
'notes': '',
'order_items': request.map((e) => e.toRequest()).toList(),
},
headers: getAuthorizationHeader(),
);
if (response.data['success'] == false) {
return DC.error(OrderFailure.unexpectedError());
}
final order = OrderDto.fromJson(
response.data['data'] as Map<String, dynamic>,
);
return DC.data(order);
} on ApiFailure catch (e, s) {
log('addItemOrderError', name: _logName, error: e, stackTrace: s);
return DC.error(OrderFailure.serverError(e));
}
}
}
@@ -0,0 +1,50 @@
part of '../order_dtos.dart';
@freezed
class AddItemOrderRequestDto with _$AddItemOrderRequestDto {
const AddItemOrderRequestDto._();
const factory AddItemOrderRequestDto({
@JsonKey(name: 'product_id') String? productId,
@JsonKey(name: 'product_variant_id') String? productvariantId,
@JsonKey(name: 'quantity') int? quantity,
@JsonKey(name: 'unit_price') int? unitPrice,
@JsonKey(name: 'notes') String? notes,
}) = _AddItemOrderRequestDto;
factory AddItemOrderRequestDto.fromJson(Map<String, dynamic> json) =>
_$AddItemOrderRequestDtoFromJson(json);
AddItemOrderRequest toDomain() => AddItemOrderRequest(
productId: productId ?? '',
productVariantId: productvariantId ?? '',
quantity: quantity ?? 0,
unitPrice: unitPrice ?? 0,
notes: notes ?? '',
);
factory AddItemOrderRequestDto.fromDomain(
AddItemOrderRequest addItemOrderRequest,
) => AddItemOrderRequestDto(
productId: addItemOrderRequest.productId,
productvariantId: addItemOrderRequest.productVariantId,
quantity: addItemOrderRequest.quantity,
unitPrice: addItemOrderRequest.unitPrice,
notes: addItemOrderRequest.notes,
);
Map<String, dynamic> toRequest() {
Map<String, dynamic> data = {
'product_id': productId,
'quantity': quantity,
'unit_price': unitPrice,
'notes': notes,
};
if (productvariantId != null && productvariantId != '') {
data['product_variant_id'] = productvariantId;
}
return data;
}
}
+1
View File
@@ -9,3 +9,4 @@ part 'dtos/order_dto.dart';
part 'dtos/order_request_dto.dart';
part 'dtos/payment_request_dto.dart';
part 'dtos/payment_dto.dart';
part 'dtos/add_item_order_request_dto.dart';
@@ -3353,3 +3353,283 @@ abstract class _PaymentOrderDto extends PaymentDto {
_$$PaymentOrderDtoImplCopyWith<_$PaymentOrderDtoImpl> get copyWith =>
throw _privateConstructorUsedError;
}
AddItemOrderRequestDto _$AddItemOrderRequestDtoFromJson(
Map<String, dynamic> json,
) {
return _AddItemOrderRequestDto.fromJson(json);
}
/// @nodoc
mixin _$AddItemOrderRequestDto {
@JsonKey(name: 'product_id')
String? get productId => throw _privateConstructorUsedError;
@JsonKey(name: 'product_variant_id')
String? get productvariantId => throw _privateConstructorUsedError;
@JsonKey(name: 'quantity')
int? get quantity => throw _privateConstructorUsedError;
@JsonKey(name: 'unit_price')
int? get unitPrice => throw _privateConstructorUsedError;
@JsonKey(name: 'notes')
String? get notes => throw _privateConstructorUsedError;
/// Serializes this AddItemOrderRequestDto to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of AddItemOrderRequestDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$AddItemOrderRequestDtoCopyWith<AddItemOrderRequestDto> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $AddItemOrderRequestDtoCopyWith<$Res> {
factory $AddItemOrderRequestDtoCopyWith(
AddItemOrderRequestDto value,
$Res Function(AddItemOrderRequestDto) then,
) = _$AddItemOrderRequestDtoCopyWithImpl<$Res, AddItemOrderRequestDto>;
@useResult
$Res call({
@JsonKey(name: 'product_id') String? productId,
@JsonKey(name: 'product_variant_id') String? productvariantId,
@JsonKey(name: 'quantity') int? quantity,
@JsonKey(name: 'unit_price') int? unitPrice,
@JsonKey(name: 'notes') String? notes,
});
}
/// @nodoc
class _$AddItemOrderRequestDtoCopyWithImpl<
$Res,
$Val extends AddItemOrderRequestDto
>
implements $AddItemOrderRequestDtoCopyWith<$Res> {
_$AddItemOrderRequestDtoCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of AddItemOrderRequestDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? productId = freezed,
Object? productvariantId = freezed,
Object? quantity = freezed,
Object? unitPrice = freezed,
Object? notes = freezed,
}) {
return _then(
_value.copyWith(
productId: freezed == productId
? _value.productId
: productId // ignore: cast_nullable_to_non_nullable
as String?,
productvariantId: freezed == productvariantId
? _value.productvariantId
: productvariantId // ignore: cast_nullable_to_non_nullable
as String?,
quantity: freezed == quantity
? _value.quantity
: quantity // ignore: cast_nullable_to_non_nullable
as int?,
unitPrice: freezed == unitPrice
? _value.unitPrice
: unitPrice // ignore: cast_nullable_to_non_nullable
as int?,
notes: freezed == notes
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String?,
)
as $Val,
);
}
}
/// @nodoc
abstract class _$$AddItemOrderRequestDtoImplCopyWith<$Res>
implements $AddItemOrderRequestDtoCopyWith<$Res> {
factory _$$AddItemOrderRequestDtoImplCopyWith(
_$AddItemOrderRequestDtoImpl value,
$Res Function(_$AddItemOrderRequestDtoImpl) then,
) = __$$AddItemOrderRequestDtoImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({
@JsonKey(name: 'product_id') String? productId,
@JsonKey(name: 'product_variant_id') String? productvariantId,
@JsonKey(name: 'quantity') int? quantity,
@JsonKey(name: 'unit_price') int? unitPrice,
@JsonKey(name: 'notes') String? notes,
});
}
/// @nodoc
class __$$AddItemOrderRequestDtoImplCopyWithImpl<$Res>
extends
_$AddItemOrderRequestDtoCopyWithImpl<$Res, _$AddItemOrderRequestDtoImpl>
implements _$$AddItemOrderRequestDtoImplCopyWith<$Res> {
__$$AddItemOrderRequestDtoImplCopyWithImpl(
_$AddItemOrderRequestDtoImpl _value,
$Res Function(_$AddItemOrderRequestDtoImpl) _then,
) : super(_value, _then);
/// Create a copy of AddItemOrderRequestDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? productId = freezed,
Object? productvariantId = freezed,
Object? quantity = freezed,
Object? unitPrice = freezed,
Object? notes = freezed,
}) {
return _then(
_$AddItemOrderRequestDtoImpl(
productId: freezed == productId
? _value.productId
: productId // ignore: cast_nullable_to_non_nullable
as String?,
productvariantId: freezed == productvariantId
? _value.productvariantId
: productvariantId // ignore: cast_nullable_to_non_nullable
as String?,
quantity: freezed == quantity
? _value.quantity
: quantity // ignore: cast_nullable_to_non_nullable
as int?,
unitPrice: freezed == unitPrice
? _value.unitPrice
: unitPrice // ignore: cast_nullable_to_non_nullable
as int?,
notes: freezed == notes
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String?,
),
);
}
}
/// @nodoc
@JsonSerializable()
class _$AddItemOrderRequestDtoImpl extends _AddItemOrderRequestDto {
const _$AddItemOrderRequestDtoImpl({
@JsonKey(name: 'product_id') this.productId,
@JsonKey(name: 'product_variant_id') this.productvariantId,
@JsonKey(name: 'quantity') this.quantity,
@JsonKey(name: 'unit_price') this.unitPrice,
@JsonKey(name: 'notes') this.notes,
}) : super._();
factory _$AddItemOrderRequestDtoImpl.fromJson(Map<String, dynamic> json) =>
_$$AddItemOrderRequestDtoImplFromJson(json);
@override
@JsonKey(name: 'product_id')
final String? productId;
@override
@JsonKey(name: 'product_variant_id')
final String? productvariantId;
@override
@JsonKey(name: 'quantity')
final int? quantity;
@override
@JsonKey(name: 'unit_price')
final int? unitPrice;
@override
@JsonKey(name: 'notes')
final String? notes;
@override
String toString() {
return 'AddItemOrderRequestDto(productId: $productId, productvariantId: $productvariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$AddItemOrderRequestDtoImpl &&
(identical(other.productId, productId) ||
other.productId == productId) &&
(identical(other.productvariantId, productvariantId) ||
other.productvariantId == productvariantId) &&
(identical(other.quantity, quantity) ||
other.quantity == quantity) &&
(identical(other.unitPrice, unitPrice) ||
other.unitPrice == unitPrice) &&
(identical(other.notes, notes) || other.notes == notes));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(
runtimeType,
productId,
productvariantId,
quantity,
unitPrice,
notes,
);
/// Create a copy of AddItemOrderRequestDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$AddItemOrderRequestDtoImplCopyWith<_$AddItemOrderRequestDtoImpl>
get copyWith =>
__$$AddItemOrderRequestDtoImplCopyWithImpl<_$AddItemOrderRequestDtoImpl>(
this,
_$identity,
);
@override
Map<String, dynamic> toJson() {
return _$$AddItemOrderRequestDtoImplToJson(this);
}
}
abstract class _AddItemOrderRequestDto extends AddItemOrderRequestDto {
const factory _AddItemOrderRequestDto({
@JsonKey(name: 'product_id') final String? productId,
@JsonKey(name: 'product_variant_id') final String? productvariantId,
@JsonKey(name: 'quantity') final int? quantity,
@JsonKey(name: 'unit_price') final int? unitPrice,
@JsonKey(name: 'notes') final String? notes,
}) = _$AddItemOrderRequestDtoImpl;
const _AddItemOrderRequestDto._() : super._();
factory _AddItemOrderRequestDto.fromJson(Map<String, dynamic> json) =
_$AddItemOrderRequestDtoImpl.fromJson;
@override
@JsonKey(name: 'product_id')
String? get productId;
@override
@JsonKey(name: 'product_variant_id')
String? get productvariantId;
@override
@JsonKey(name: 'quantity')
int? get quantity;
@override
@JsonKey(name: 'unit_price')
int? get unitPrice;
@override
@JsonKey(name: 'notes')
String? get notes;
/// Create a copy of AddItemOrderRequestDto
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$AddItemOrderRequestDtoImplCopyWith<_$AddItemOrderRequestDtoImpl>
get copyWith => throw _privateConstructorUsedError;
}
@@ -257,3 +257,23 @@ Map<String, dynamic> _$$PaymentOrderDtoImplToJson(
'created_at': instance.createdAt,
'updated_at': instance.updatedAt,
};
_$AddItemOrderRequestDtoImpl _$$AddItemOrderRequestDtoImplFromJson(
Map<String, dynamic> json,
) => _$AddItemOrderRequestDtoImpl(
productId: json['product_id'] as String?,
productvariantId: json['product_variant_id'] as String?,
quantity: (json['quantity'] as num?)?.toInt(),
unitPrice: (json['unit_price'] as num?)?.toInt(),
notes: json['notes'] as String?,
);
Map<String, dynamic> _$$AddItemOrderRequestDtoImplToJson(
_$AddItemOrderRequestDtoImpl instance,
) => <String, dynamic>{
'product_id': instance.productId,
'product_variant_id': instance.productvariantId,
'quantity': instance.quantity,
'unit_price': instance.unitPrice,
'notes': instance.notes,
};
@@ -105,4 +105,29 @@ class OrderRepository implements IOrderRepository {
return left(const OrderFailure.unexpectedError());
}
}
@override
Future<Either<OrderFailure, Order>> addItemOrder({
required String id,
required List<AddItemOrderRequest> request,
}) async {
try {
final result = await _dataProvider.addItemOrder(
id: id,
request: request
.map((e) => AddItemOrderRequestDto.fromDomain(e))
.toList(),
);
if (result.hasError) {
return left(result.error!);
}
final order = result.data!.toDomain();
return right(order);
} catch (e) {
log('addItemOrderError', name: _logName, error: e);
return left(const OrderFailure.unexpectedError());
}
}
}