import 'dart:convert'; import 'dart:developer' as dev; import 'dart:developer'; import 'package:muaawana/models/products/products_response.dart'; import '../models/ads/Advertisement.dart'; import '../models/categories/category.dart'; import '../models/categories/category_response.dart'; import '../models/services/result.dart'; import '../models/services/service_response.dart'; import '../models/utils/api_response.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; const url = 'server.muaawana.com'; class HomeService { String token = "InvalidToken"; int statusCode = -1; bool errorToken = false; /// Fetching Jwt Token From SharedPrefs Function Future getToken() async { final prefs = await SharedPreferences.getInstance(); if (prefs.getString('token') == null || prefs.getString('token') == '') { prefs.setString('token', 'InvalidToken'); } if (prefs.getString('token') != null) { token = prefs.getString('token')!; } return token.toString(); } /// Fetching All Categories Future>> fetchAllCategories() async { errorToken = false; statusCode = -1; await getToken(); return await http.get( Uri.https(url, '/api/v1/public/get_all_categories'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); var categList = []; for (var categ in jsonData) { final decoded = Category.fromJson(categ); categList.add(decoded); } return APIResponse>( statusCode: statusCode, data: categList); } return APIResponse>( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse>( statusCode: statusCode, errorMessage: 'no internet connection')); } /// Fetching 6 Categories Future> fetchAllCategoriesPg( int page, int size) async { errorToken = false; statusCode = -1; // await getToken(); final params = {'page': page.toString(), 'size': size.toString()}; return await http.get( Uri.https(url, '/api/v1/public/get_all_categories_pg', params), headers: { // 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { log("fetchAllCategoriesPg --$data"); log("fetchAllCategoriesPg -- data --${data.body}"); statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final decoded = json.decode(data.body); CategoryResponse profile = CategoryResponse.fromJson(decoded); return APIResponse( statusCode: statusCode, data: profile); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } /// Fetching Services by Categories Future> fetchServiceByCategory( int categoryId, int page, int size) async { errorToken = false; statusCode = -1; // await getToken(); var params = {'c_id': '$categoryId', 'page': '$page', 'size': '$size'}; return await http.get( Uri.https(url, '/api/v1/public/get_all_services_by_category_pg', params), headers: { // 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { log("fetchServiceByCategory -- ${data.body}"); statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); final result = ServiceResponse.fromJson(jsonData); return APIResponse( statusCode: statusCode, data: result); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } /// Fetching Services by Categories Pg : // fetchServicesByCategoryPg() async { // //todo: Add Category params // errorToken = false; // statusCode = -1; // await getToken(); // final params = {'page': 0, 'size': 6}; // return await http.get( // Uri.https(url, '/api/v1/services/get_all_pg', params), // headers: { // 'Authorization': 'Bearer $token', // 'Content-type': 'application/json', // 'Accept': 'application/json' // }, // ).then((data) { // statusCode = data.statusCode; // if (data.statusCode == 200 || data.statusCode == 201) { // final jsonData = json.decode(data.body); // log("-------> category list pg :"); // log(jsonData); // CategoryResponse profile = CategoryResponse.fromJson(jsonData); // return APIResponse( // statusCode: statusCode, data: profile); // } // return APIResponse( // statusCode: statusCode, errorMessage: 'Please check your info'); // }).catchError((_) => APIResponse( // statusCode: statusCode, errorMessage: 'no internet connection')); // } /// Fetching Products by Categories Future> fetchProductsByCategory( int categoryId, int page, int size) async { errorToken = false; statusCode = -1; await getToken(); var params = {'c_id': '$categoryId', 'page': '$page', 'size': '$size'}; dev.log("fetchProductsByCategory --c_id- $categoryId"); return await http.get( Uri.https(url, '/api/v1/public/get_all_product_by_category_pg', params), headers: { 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { dev.log("fetchProductsByCategory --- $data"); final jsonData = json.decode(data.body); final result = ProductsResponse.fromJson(jsonData); return APIResponse( statusCode: statusCode, data: result); } dev.log("errrorrrrrrrr"); return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future> fetchProducts(int page, int size) async { errorToken = false; statusCode = -1; await getToken(); var params = { 'page': '$page', 'size': '$size'}; return await http.get( Uri.https(url, '/api/v1/public/get_all_product', params), headers: { 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { dev.log("fetchProducts --- $data"); final jsonData = json.decode(data.body); final result = ProductsResponse.fromJson(jsonData); return APIResponse( statusCode: statusCode, data: result); } dev.log("errrorrrrrrrr"); return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } /// Fetching Products By Category PG // fetchProductsByCategoryPg() async { // //todo: Add Category params // errorToken = false; // statusCode = -1; // await getToken(); // final params = {'page': 0, 'size': 6}; // return await http.get( // Uri.https(url, '/api/v1/products/get_all_pg', params), // headers: { // 'Authorization': 'Bearer $token', // 'Content-type': 'application/json', // 'Accept': 'application/json' // }, // ).then((data) { // statusCode = data.statusCode; // if (data.statusCode == 200 || data.statusCode == 201) { // final jsonData = json.decode(data.body); // log("-------> category list pg :"); // log(jsonData); // CategoryResponse profile = CategoryResponse.fromJson(jsonData); // return APIResponse( // statusCode: statusCode, data: profile); // } // return APIResponse( // statusCode: statusCode, errorMessage: 'Please check your info'); // }).catchError((_) => APIResponse( // statusCode: statusCode, errorMessage: 'no internet connection')); // } /// fetch Service Detail Future> getServiceDetail(int serviceId) async { errorToken = false; statusCode = -1; await getToken(); return await http.get( Uri.https( url, '/api/v1/services/get_by_user/$serviceId', ), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); final result = Service.fromJson(jsonData); //print(result); //print("decoded service length"); return APIResponse(statusCode: statusCode, data: result); } dev.log("errrorrrrrrrr"); return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future> searchService( String name, int page, int size) async { errorToken = false; statusCode = -1; await getToken(); var params = {'name': name, 'page': '$page', 'size': '$size'}; return await http.get( Uri.https(url, 'api/v1/public/get_all_by_name_pg', params), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); final result = ServiceResponse.fromJson(jsonData); return APIResponse( statusCode: statusCode, data: result); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future> searchProduct( String name, int page, int size) async { errorToken = false; statusCode = -1; await getToken(); var params = {'name': name, 'page': '$page', 'size': '$size'}; return await http.get( Uri.https(url, '/api/v1/user_product/get_all_by_name_pg', params), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); final result = ProductsResponse.fromJson(jsonData); return APIResponse( statusCode: statusCode, data: result); } dev.log("errrorrrrrrrr"); return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future>> fetchAds() async { errorToken = false; statusCode = -1; await getToken(); return await http.get( Uri.https(url, '/api/v1/public/get_all_ads'), headers: { 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) { log(data.body); statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); List allAds = []; for (var ad in jsonData) { allAds.add(Advertisement.fromJson(ad)); } //print(allAds.length); //print("finished !!! alright"); return APIResponse>( statusCode: statusCode, data: allAds); } //print("finished !!! error"); return APIResponse>( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_, stacktrace) { log(stacktrace.toString()); return APIResponse>( statusCode: statusCode, errorMessage: 'no internet connection'); }); } }