feat: create product

This commit is contained in:
efrilm
2025-08-05 19:20:45 +07:00
parent 935b6b9a5b
commit bb9aef55cf
24 changed files with 2750 additions and 848 deletions
@@ -1,12 +1,9 @@
import 'dart:developer';
import 'package:bloc/bloc.dart';
import 'package:enaklo_pos/data/datasources/product_local_datasource.dart';
import 'package:enaklo_pos/data/datasources/product_remote_datasource.dart';
import 'package:enaklo_pos/data/models/request/product_request_model.dart';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:image_picker/image_picker.dart';
part 'update_product_event.dart';
part 'update_product_state.dart';
@@ -21,61 +18,11 @@ class UpdateProductBloc extends Bloc<UpdateProductEvent, UpdateProductState> {
emit(const _Loading());
try {
// Validate required fields
if (event.product.name == null || event.product.name!.isEmpty) {
emit(_Error('Product name is required'));
return;
}
if (event.product.price == null || event.product.price == 0) {
emit(_Error('Product price is required'));
return;
}
// if (event.product.stock == null) {
// emit(_Error('Product stock is required'));
// return;
// }
if (event.product.categoryId == null) {
emit(_Error('Product category is required'));
return;
}
// Parse price safely
final price = event.product.price!;
if (price == 0) {
emit(_Error('Invalid price format'));
return;
}
final requestData = ProductRequestModel(
id: event.product.id,
name: event.product.name!,
price: price,
stock: 0,
categoryId: 0,
isBestSeller: 0, // Default to 0 if null
image: event.image,
printerType: 'kitchen', // Default to kitchen if null
);
log("Update requestData: ${requestData.toString()}");
log("Request map: ${requestData.toMap()}");
final response = await datasource.updateProduct(requestData);
final response = await datasource.updateProduct(event.product);
response.fold(
(l) => emit(_Error(l)),
(r) async {
// Update local database after successful API update
try {
await ProductLocalDatasource.instance
.updateProduct(event.product);
log("Local product updated successfully");
} catch (e) {
log("Error updating local product: $e");
}
emit(_Success('Update Product Success'));
emit(_Success('Product berhasil diupdate'));
},
);
} catch (e) {
@@ -16,21 +16,20 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$UpdateProductEvent {
Product get product => throw _privateConstructorUsedError;
XFile? get image => throw _privateConstructorUsedError;
ProductRequestModel get product => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function(Product product, XFile? image) updateProduct,
required TResult Function(ProductRequestModel product) updateProduct,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(Product product, XFile? image)? updateProduct,
TResult? Function(ProductRequestModel product)? updateProduct,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(Product product, XFile? image)? updateProduct,
TResult Function(ProductRequestModel product)? updateProduct,
required TResult orElse(),
}) =>
throw _privateConstructorUsedError;
@@ -64,7 +63,7 @@ abstract class $UpdateProductEventCopyWith<$Res> {
UpdateProductEvent value, $Res Function(UpdateProductEvent) then) =
_$UpdateProductEventCopyWithImpl<$Res, UpdateProductEvent>;
@useResult
$Res call({Product product, XFile? image});
$Res call({ProductRequestModel product});
}
/// @nodoc
@@ -83,17 +82,12 @@ class _$UpdateProductEventCopyWithImpl<$Res, $Val extends UpdateProductEvent>
@override
$Res call({
Object? product = null,
Object? image = freezed,
}) {
return _then(_value.copyWith(
product: null == product
? _value.product
: product // ignore: cast_nullable_to_non_nullable
as Product,
image: freezed == image
? _value.image
: image // ignore: cast_nullable_to_non_nullable
as XFile?,
as ProductRequestModel,
) as $Val);
}
}
@@ -106,7 +100,7 @@ abstract class _$$UpdateProductImplCopyWith<$Res>
__$$UpdateProductImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({Product product, XFile? image});
$Res call({ProductRequestModel product});
}
/// @nodoc
@@ -123,17 +117,12 @@ class __$$UpdateProductImplCopyWithImpl<$Res>
@override
$Res call({
Object? product = null,
Object? image = freezed,
}) {
return _then(_$UpdateProductImpl(
null == product
? _value.product
: product // ignore: cast_nullable_to_non_nullable
as Product,
freezed == image
? _value.image
: image // ignore: cast_nullable_to_non_nullable
as XFile?,
as ProductRequestModel,
));
}
}
@@ -141,16 +130,14 @@ class __$$UpdateProductImplCopyWithImpl<$Res>
/// @nodoc
class _$UpdateProductImpl implements _UpdateProduct {
const _$UpdateProductImpl(this.product, this.image);
const _$UpdateProductImpl(this.product);
@override
final Product product;
@override
final XFile? image;
final ProductRequestModel product;
@override
String toString() {
return 'UpdateProductEvent.updateProduct(product: $product, image: $image)';
return 'UpdateProductEvent.updateProduct(product: $product)';
}
@override
@@ -158,12 +145,11 @@ class _$UpdateProductImpl implements _UpdateProduct {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$UpdateProductImpl &&
(identical(other.product, product) || other.product == product) &&
(identical(other.image, image) || other.image == image));
(identical(other.product, product) || other.product == product));
}
@override
int get hashCode => Object.hash(runtimeType, product, image);
int get hashCode => Object.hash(runtimeType, product);
/// Create a copy of UpdateProductEvent
/// with the given fields replaced by the non-null parameter values.
@@ -176,27 +162,27 @@ class _$UpdateProductImpl implements _UpdateProduct {
@override
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function(Product product, XFile? image) updateProduct,
required TResult Function(ProductRequestModel product) updateProduct,
}) {
return updateProduct(product, image);
return updateProduct(product);
}
@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(Product product, XFile? image)? updateProduct,
TResult? Function(ProductRequestModel product)? updateProduct,
}) {
return updateProduct?.call(product, image);
return updateProduct?.call(product);
}
@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(Product product, XFile? image)? updateProduct,
TResult Function(ProductRequestModel product)? updateProduct,
required TResult orElse(),
}) {
if (updateProduct != null) {
return updateProduct(product, image);
return updateProduct(product);
}
return orElse();
}
@@ -231,13 +217,11 @@ class _$UpdateProductImpl implements _UpdateProduct {
}
abstract class _UpdateProduct implements UpdateProductEvent {
const factory _UpdateProduct(final Product product, final XFile? image) =
const factory _UpdateProduct(final ProductRequestModel product) =
_$UpdateProductImpl;
@override
Product get product;
@override
XFile? get image;
ProductRequestModel get product;
/// Create a copy of UpdateProductEvent
/// with the given fields replaced by the non-null parameter values.
@@ -2,5 +2,6 @@ part of 'update_product_bloc.dart';
@freezed
class UpdateProductEvent with _$UpdateProductEvent {
const factory UpdateProductEvent.updateProduct(Product product, XFile? image) = _UpdateProduct;
}
const factory UpdateProductEvent.updateProduct(ProductRequestModel product) =
_UpdateProduct;
}