feat: syn product
This commit is contained in:
@@ -20,10 +20,10 @@ class AddProductBloc extends Bloc<AddProductEvent, AddProductState> {
|
||||
emit(const _Loading());
|
||||
final requestData = ProductRequestModel(
|
||||
name: event.product.name!,
|
||||
price: int.parse(event.product.price!),
|
||||
stock: event.product.stock!,
|
||||
categoryId: event.product.categoryId!,
|
||||
isBestSeller: event.product.isFavorite!,
|
||||
price: event.product.price!,
|
||||
stock: 0,
|
||||
categoryId: 0,
|
||||
isBestSeller: 0,
|
||||
image: event.image,
|
||||
);
|
||||
log("requestData: ${requestData.toString()}");
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:enaklo_pos/data/datasources/product_remote_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
|
||||
@@ -19,7 +18,7 @@ class GetProductsBloc extends Bloc<GetProductsEvent, GetProductsState> {
|
||||
response.fold(
|
||||
(l) => emit(_Error(l)),
|
||||
(r) {
|
||||
emit(_Success(r.data!));
|
||||
emit(_Success(r.data!.products!));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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