feat: syn product
This commit is contained in:
@@ -19,57 +19,58 @@ class UpdateProductBloc extends Bloc<UpdateProductEvent, UpdateProductState> {
|
||||
) : super(const _Initial()) {
|
||||
on<_UpdateProduct>((event, emit) async {
|
||||
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!.isEmpty) {
|
||||
|
||||
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.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 = int.tryParse(event.product.price!);
|
||||
if (price == null) {
|
||||
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: event.product.stock!,
|
||||
categoryId: event.product.categoryId!,
|
||||
isBestSeller: event.product.isFavorite ?? 0, // Default to 0 if null
|
||||
stock: 0,
|
||||
categoryId: 0,
|
||||
isBestSeller: 0, // Default to 0 if null
|
||||
image: event.image,
|
||||
printerType: event.product.printerType ?? 'kitchen', // Default to kitchen if null
|
||||
printerType: 'kitchen', // Default to kitchen if null
|
||||
);
|
||||
|
||||
|
||||
log("Update requestData: ${requestData.toString()}");
|
||||
log("Request map: ${requestData.toMap()}");
|
||||
|
||||
|
||||
final response = await datasource.updateProduct(requestData);
|
||||
response.fold(
|
||||
(l) => emit(_Error(l)),
|
||||
(r) async {
|
||||
// Update local database after successful API update
|
||||
try {
|
||||
await ProductLocalDatasource.instance.updateProduct(event.product);
|
||||
await ProductLocalDatasource.instance
|
||||
.updateProduct(event.product);
|
||||
log("Local product updated successfully");
|
||||
} catch (e) {
|
||||
log("Error updating local product: $e");
|
||||
@@ -83,4 +84,4 @@ class UpdateProductBloc extends Bloc<UpdateProductEvent, UpdateProductState> {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user