feat: create product
This commit is contained in:
@@ -1,27 +1,48 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:enaklo_pos/core/constants/variables.dart';
|
||||
import 'package:enaklo_pos/core/network/dio_client.dart';
|
||||
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/category_response_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class CategoryRemoteDatasource {
|
||||
Future<Either<String, CategroyResponseModel>> getCategories() async {
|
||||
final Dio dio = DioClient.instance;
|
||||
|
||||
Future<Either<String, CategoryResponseModel>> getCategories({
|
||||
int page = 1,
|
||||
int limit = 10,
|
||||
bool isActive = true,
|
||||
}) async {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final Map<String, String> headers = {
|
||||
final headers = {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
};
|
||||
final response = await http.get(
|
||||
Uri.parse('${Variables.baseUrl}/api/api-categories'),
|
||||
headers: headers);
|
||||
log(response.statusCode.toString());
|
||||
log(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
return right(CategroyResponseModel.fromJson(response.body));
|
||||
} else {
|
||||
return left(response.body);
|
||||
|
||||
try {
|
||||
final response = await dio.get(
|
||||
'${Variables.baseUrl}/api/v1/categories',
|
||||
queryParameters: {
|
||||
'page': page,
|
||||
'limit': limit,
|
||||
'is_active': isActive,
|
||||
},
|
||||
options: Options(headers: headers),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return right(CategoryResponseModel.fromMap(response.data));
|
||||
} else {
|
||||
return left(response.data.toString());
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
log('Dio error: ${e.message}');
|
||||
return left(e.response?.data.toString() ?? e.message ?? 'Unknown error');
|
||||
} catch (e) {
|
||||
log('Unexpected error: $e');
|
||||
return left('Unexpected error occurred');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:enaklo_pos/core/constants/variables.dart';
|
||||
import 'package:enaklo_pos/core/network/dio_client.dart';
|
||||
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/file_response_model.dart';
|
||||
|
||||
class FileRemoteDataSource {
|
||||
final Dio dio = DioClient.instance;
|
||||
|
||||
Future<Either<String, FileResponseModel>> uploadFile({
|
||||
required String filePath,
|
||||
required String fileType,
|
||||
required String description,
|
||||
}) async {
|
||||
final url = '${Variables.baseUrl}/api/v1/files/upload';
|
||||
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
|
||||
// Membuat FormData
|
||||
final formData = FormData.fromMap({
|
||||
'file': await MultipartFile.fromFile(filePath,
|
||||
filename: filePath.split('/').last),
|
||||
'file_type': fileType,
|
||||
'description': description,
|
||||
});
|
||||
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: formData,
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
// Content-Type otomatis diatur oleh Dio untuk FormData
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 201 || response.statusCode == 200) {
|
||||
// Misal response.data['url'] adalah URL file yang diupload
|
||||
return Right(FileResponseModel.fromJson(response.data));
|
||||
} else {
|
||||
return Left('Upload gagal: ${response.statusMessage}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
return Left(e.response?.data['message'] ?? 'Upload gagal');
|
||||
} catch (e) {
|
||||
return Left('Unexpected error: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import 'package:enaklo_pos/core/network/dio_client.dart';
|
||||
import 'package:enaklo_pos/data/models/request/product_request_model.dart';
|
||||
import 'package:enaklo_pos/data/models/response/add_product_response_model.dart';
|
||||
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../../core/constants/variables.dart';
|
||||
import 'auth_local_datasource.dart';
|
||||
@@ -21,6 +20,10 @@ class ProductRemoteDatasource {
|
||||
|
||||
final response = await dio.get(
|
||||
url,
|
||||
queryParameters: {
|
||||
'page': 1,
|
||||
'limit': 30,
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
@@ -45,67 +48,64 @@ class ProductRemoteDatasource {
|
||||
|
||||
Future<Either<String, AddProductResponseModel>> addProduct(
|
||||
ProductRequestModel productRequestModel) async {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final Map<String, String> headers = {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
};
|
||||
var request = http.MultipartRequest(
|
||||
'POST', Uri.parse('${Variables.baseUrl}/api/products'));
|
||||
request.fields.addAll(productRequestModel.toMap());
|
||||
request.files.add(await http.MultipartFile.fromPath(
|
||||
'image', productRequestModel.image!.path));
|
||||
request.headers.addAll(headers);
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final url = '${Variables.baseUrl}/api/v1/products';
|
||||
|
||||
http.StreamedResponse response = await request.send();
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: productRequestModel.toMap(),
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
final String body = await response.stream.bytesToString();
|
||||
log(response.stream.toString());
|
||||
log(response.statusCode.toString());
|
||||
if (response.statusCode == 201) {
|
||||
return right(AddProductResponseModel.fromJson(body));
|
||||
} else {
|
||||
return left(body);
|
||||
if (response.statusCode == 200) {
|
||||
return Right(AddProductResponseModel.fromMap(response.data));
|
||||
} else {
|
||||
return const Left('Failed to create products');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
log("Dio error: ${e.message}");
|
||||
return Left(e.response?.data['message'] ?? 'Gagal menambah produk');
|
||||
} catch (e) {
|
||||
log("Unexpected error: $e");
|
||||
return const Left('Unexpected error occurred');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<String, AddProductResponseModel>> updateProduct(
|
||||
ProductRequestModel productRequestModel) async {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final Map<String, String> headers = {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
};
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final url =
|
||||
'${Variables.baseUrl}/api/v1/products/${productRequestModel.id}';
|
||||
|
||||
log("Update Product Request Data: ${productRequestModel.toMap()}");
|
||||
log("Update Product ID: ${productRequestModel.id}");
|
||||
log("Update Product Name: ${productRequestModel.name}");
|
||||
log("Update Product Price: ${productRequestModel.price}");
|
||||
log("Update Product Stock: ${productRequestModel.stock}");
|
||||
log("Update Product Category ID: ${productRequestModel.categoryId}");
|
||||
log("Update Product Is Best Seller: ${productRequestModel.isBestSeller}");
|
||||
log("Update Product Printer Type: ${productRequestModel.printerType}");
|
||||
log("Update Product Has Image: ${productRequestModel.image != null}");
|
||||
final response = await dio.put(
|
||||
url,
|
||||
data: productRequestModel.toMap(),
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
var request = http.MultipartRequest(
|
||||
'POST', Uri.parse('${Variables.baseUrl}/api/products/edit'));
|
||||
request.fields.addAll(productRequestModel.toMap());
|
||||
if (productRequestModel.image != null) {
|
||||
request.files.add(await http.MultipartFile.fromPath(
|
||||
'image', productRequestModel.image!.path));
|
||||
}
|
||||
request.headers.addAll(headers);
|
||||
|
||||
log("Update Product Request Fields: ${request.fields}");
|
||||
log("Update Product Request Files: ${request.files.length}");
|
||||
|
||||
http.StreamedResponse response = await request.send();
|
||||
|
||||
final String body = await response.stream.bytesToString();
|
||||
log("Update Product Status Code: ${response.statusCode}");
|
||||
log("Update Product Body: $body");
|
||||
if (response.statusCode == 200) {
|
||||
return right(AddProductResponseModel.fromJson(body));
|
||||
} else {
|
||||
return left(body);
|
||||
if (response.statusCode == 200) {
|
||||
return Right(AddProductResponseModel.fromMap(response.data));
|
||||
} else {
|
||||
return const Left('Failed to update products');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
log("Dio error: ${e.message}");
|
||||
return Left(e.response?.data['message'] ?? 'Gagal update produk');
|
||||
} catch (e) {
|
||||
log("Unexpected error: $e");
|
||||
return const Left('Unexpected error occurred');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user