import 'dart:convert'; import 'dart:developer'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/utils/api_response.dart'; import 'package:http/http.dart' as http; const url = 'server.muaawana.com'; class RatingServices { 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')!; } //print(token); return token.toString(); } Future publishProductRating( String description, int productId, int rate, String reason) async { final rating = rate.toDouble(); errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, 'api/v1/user_rating/rate_product/$productId', {'name': '$description', 'rate': '$rating', 'reason': '$reason'}), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, // body: { // 'name': description, // // 'product_id':productId, // 'rate': rate.toDouble(), // 'reason': reason // } ).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode, data: jsonData ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((e, stack) { return APIResponse( statusCode: statusCode, errorMessage: 'no internet connection'); }); } Future publishServiceRating( String description, int productId, int rate, String reason, int? technicianId) async { errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, '/api/v1/user_rating/rate_service/$productId'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, body: { 'name': description, 'rate': rate.toDouble(), 'reason': reason, if (technicianId != null) 'technicianId' : technicianId }).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode, ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future updateProductRating( String description, int rateId, int rate, String reason) async { final rating = rate.toDouble(); errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, 'api/v1/user_rating/update_rate_product/$rateId', {'description': description, 'rate': '${rating.toDouble()}', 'reason': reason}), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, // body: { // 'name': description, // // 'product_id':productId, // 'rate': rate.toDouble(), // 'reason': reason // } ).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode, ); } //print(statusCode); //print(data.body); //print('eerrror'); return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((e, stack) { //print('errrrrrrrrrrrrrrr'); //print(e); //print(stack); return APIResponse( statusCode: statusCode, errorMessage: 'no internet connection'); }); } Future updateServiceRating( String description, int rateId, int rate, String reason) async { errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, '/api/v1/user_rating/update_rate_service/$rateId'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, body: { 'description': description, // 'product_id':productId, 'rate': '${rate.toDouble()}', 'reason': reason }).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode,data: jsonData ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future getAllProductRating( int productId) async { errorToken = false; statusCode = -1; await getToken(); return await http.get( Uri.https(url, '/api/v1/user_rating/get_all_by_product/$productId'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode,data: jsonData ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future getAllServiceRating( int serviceId) async { errorToken = false; statusCode = -1; await getToken(); return await http.get( Uri.https(url, '/api/v1/user_rating/get_all_by_service/$serviceId'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode, ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future getAllMyProductRating( int size, int page, ) async { errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, '/api/v1/user_rating/get_all_my_product_rate'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, body: { 'page': '$page', 'size': '$size' }).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode,data: jsonData ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future getAllMyServiceRating( int size, int page, ) async { errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, '/api/v1/user_rating/get_all_my_service_rating'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, body: { 'page': '$page', 'size': '$size' }).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode,data: jsonData ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future getProductRatingById( int productId ) async { errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, '/api/v1/user_rating/get_product_rating_by/$productId'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode, ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } Future getServiceRatingById( int serviceId ) async { errorToken = false; statusCode = -1; await getToken(); return await http.post( Uri.https(url, '/api/v1/user_rating/get_service_rating_by/$serviceId'), headers: { 'Authorization': 'Bearer $token', 'Content-type': 'application/json', 'Accept': 'application/json' }, ).then((data) async { statusCode = data.statusCode; if (data.statusCode == 200 || data.statusCode == 201) { final jsonData = json.decode(data.body); log("success rating"); return APIResponse( statusCode: statusCode, ); } return APIResponse( statusCode: statusCode, errorMessage: 'Please check your info'); }).catchError((_) => APIResponse( statusCode: statusCode, errorMessage: 'no internet connection')); } }