Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
19 kB
1
Indexable
Never
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:dio/dio.dart' as mPart;
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:iketfaa_delivery/App/Common/Services/AuthenticationService/Core/manager.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/Constants/Strings/General.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/Methods/tools.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/RoutesManagement/pages.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/LookupRequestReasonTypes.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/BusinessServiceItems.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/BusinessServiceItemsEdit.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/BusinessStatistic.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/CategoryItem.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/MyRequestsOrders.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/OurService.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/ProivderSpecialityItem.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/ProviderReviews.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/RoleItem.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/SpecialityItem.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/RequestsParameters/request_service_dto.dart';

class FreelanceAPI {
  static final Dio httpClient = Dio();
  static final String baseUrl = BASE_URL;
  static final String localBaseUrl = LOCALE_BASE_URL;
  static final GetStorage storage = GetStorage();
  static final CommonTools commonTools = CommonTools();

  Future<List<RoleItem>> getAllRoles() async {
    final AuthenticationManager authenticationManager = Get.find();
    List<RoleItem> items = [];

    try {
      await httpClient
          .get(baseUrl + 'Roles/GetAll',
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authenticationManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<RoleItem>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => RoleItem.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }

    return items;
  }

  Future<List<OurServiceItem>> getAllOurServices() async {
    final AuthenticationManager authenticationManager = Get.find();
    List<OurServiceItem> items = [];

    try {
      await httpClient
          .get(baseUrl + 'OurServices/GetAll',
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authenticationManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<OurServiceItem>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => OurServiceItem.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }

    return items;
  }

  Future<List<SpecialityItem>> getAllSepcialities(int categoryId) async {
    final AuthenticationManager authenticationManager = Get.find();
    List<SpecialityItem> items = [];

    try {
      await httpClient
          .get(baseUrl + 'Specialities/GetAll',
              queryParameters: {
                'searchCriteriaModel.categoryId': categoryId,
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authenticationManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<SpecialityItem>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => SpecialityItem.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }

    return items;
  }

  Future<List<CategoryItem>> getAllCategories(int roleId) async {
    final AuthenticationManager authenticationManager = Get.find();
    List<CategoryItem> items = [];

    try {
      await httpClient
          .get(baseUrl + 'Categories/GetAll',
              queryParameters: {
                'searchCriteriaModel.roleId': roleId,
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authenticationManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<CategoryItem>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => CategoryItem.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'failedRegisterMess'
          : 'failedRegisterMess');
    }

    return items;
  }

  Future<List<BusinessServiceItems>> getAllBusinessServices() async {
    final AuthenticationManager authManager = Get.find();
    List<BusinessServiceItems> items = [];
    try {
      await httpClient
          .get(baseUrl + 'Iktifaa/RequestServices/GetAll',
              queryParameters: {
                'searchCriteriaModel.providerId': authManager.appUser.value.id,
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<BusinessServiceItems>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => BusinessServiceItems.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }

    return items;
  }

  Future editRequestService(BusinessServiceItemsEdit param) async {
    final AuthenticationManager authManger = Get.find();
    BusinessServiceItemsEdit items = BusinessServiceItemsEdit();
    commonTools.showLoading();
    try {
      var response =
          await httpClient.put(baseUrl + 'Iktifaa/RequestServices/Edit',
              data: param.toJson(),
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authManger.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }));

      if (response.statusCode == 200) {
        Get.back();
        Get.back();
        Get.back();
        Get.back();
        commonTools.showSuccessSnackBar('the Service updated successfully'.tr);
        items = response.data;
        return items;
      }
    } on DioError catch (e) {
      Get.back();
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }

    return items;
  }

  Future sendOfferRequestService(BusinessServiceItems param) async {
    final AuthenticationManager authManager = Get.find();
    try {
      await httpClient.put(
          baseUrl + 'Iktifaa/RequestOffers/UserAddBid/${param.offerId}',
          data: {
            'bid': param.bid,
            'expectedDeliveryDate': param.modifiedDate,
            'paymentMethod': param.paymentMethod
          },
          options: Options(headers: {
            'accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': authManager.getToken(),
            'Accept-Language': '${Get.locale!.languageCode}',
          }));
      Get.back();
      commonTools.showSuccessSnackBar('Request Service updated successfully'.tr,
          duration: 1);
    } on DioError catch (e) {
      Get.back();
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }
  }

  Future<void> requestService(
      RequestServiceDTO param, List<File> images) async {
    final AuthenticationManager authManger = Get.find();
    mPart.FormData form = mPart.FormData.fromMap(param.toJson());

    for (var image in images) {
      form.files.add(
        MapEntry(
          '[files]',
          mPart.MultipartFile.fromBytes(
            image.readAsBytesSync(),
            filename:
                '${DateTime.now().toUtc()}${authManger.commonTools.createRandom()}.jpg',
          ),
        ),
      );
    }

    try {
      await httpClient
          .post(baseUrl + 'Iktifaa/RequestOffers/Add',
              data: form,
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authManger.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          Get.offNamedUntil(Routes.FREELANCE_HOME, (route) => true);
          commonTools.showSuccessSnackBar('serviceRequestSuccessMess');
        }
      });
    } on DioError catch (e) {
      Get.back();
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }
  }

  Future<List<MyRequestsOrders>> getAllRequestsOrders() async {
    final AuthenticationManager authenticationManager = Get.find();
    List<MyRequestsOrders> items = [];

    try {
      await httpClient
          .get(baseUrl + 'Iktifaa/RequestOffers/GetAll',
              queryParameters: {
                'searchCriteriaModel.requesterId':
                    authenticationManager.appUser.value.id
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authenticationManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<MyRequestsOrders>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => MyRequestsOrders.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }

    return items;
  }

  Future<List<BusinessServiceItems>> getAllRequestFromProviders(
      String requestID) async {
    final AuthenticationManager authenticationManager = Get.find();
    List<BusinessServiceItems> items = [];

    try {
      await httpClient
          .get(baseUrl + 'Iktifaa/RequestServices/GetAll',
              queryParameters: {
                'searchCriteriaModel.offerId': requestID,
                'searchCriteriaModel.offerStatus': 2
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authenticationManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<BusinessServiceItems>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => BusinessServiceItems.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }
    Get.back();

    return items;
  }

  Future acceptOffer(BusinessServiceItems param) async {
    final AuthenticationManager authManager = Get.find();
    try {
      await httpClient.put(
        baseUrl + 'Iktifaa/RequestOffers/AcceptUserRequestService/${param.id}',
        options: Options(
          headers: {
            'accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': authManager.getToken(),
            'Accept-Language': '${Get.locale!.languageCode}',
          },
        ),
      );
      Get.back();
      commonTools.showSuccessSnackBar('Offer accepted'.tr, duration: 1);
    } on DioError catch (e) {
      Get.back();
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }
  }

  Future<BusinessStatisticResponse> getBusinessStatistic() async {
    final AuthenticationManager authManager = Get.find();
    BusinessStatisticResponse businessStatistic = BusinessStatisticResponse();
    try {
      await httpClient
          .get(baseUrl + 'Iktifaa/ProviderDashboard/GetBusinessStatistic',
              queryParameters: {
                'searchCriteriaModel.providerId': authManager.appUser.value.id,
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          businessStatistic = BusinessStatisticResponse.fromJson(response.data);
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }
    return businessStatistic;
  }

  Future<List<LookupRequestReasonTypes>>
      getAllLookupRequestReasonTypes() async {
    final AuthenticationManager authManager = Get.find();
    List<LookupRequestReasonTypes> items = [];
    try {
      await httpClient
          .get(baseUrl + 'LookupRequestReasonTypes/GetAll',
              queryParameters: {
                'searchCriteriaModel.providerId': authManager.appUser.value.id,
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authManager.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<LookupRequestReasonTypes>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => LookupRequestReasonTypes.fromJson(x)));
        }
      });
    } on DioError catch (e) {
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }
    return items;
  }

  Future<List<ProivderSpecialityItem>> getAllProviderSpecialities() async {
    final AuthenticationManager authManger = Get.find();
    List<ProivderSpecialityItem> items = [];

    try {
      await httpClient
          .get(baseUrl + 'Iktifaa/ProviderSpecialities/GetAll',
              queryParameters: {
                'searchCriteriaDto.providerId': authManger.appUser.value.id
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authManger.getToken(),
              }))
          .then((response) {
        if (response.statusCode == 200) {
          items = List<ProivderSpecialityItem>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => ProivderSpecialityItem.fromJson(x)));

          Get.back();
        }
      });
    } on DioError catch (e) {
      Get.back();
      print(e.message);
    }

    return items;
  }

  Future<List<ProviderReviews>> getAllProviderReviews() async {
    final AuthenticationManager authManger = Get.find();
    List<ProviderReviews> items = [];

    for (var item in items) print(item);

    try {
      await httpClient
          .get(baseUrl + 'Iktifaa/ProviderReviews/GetAll',
              queryParameters: {
                'searchCriteriaModel.providerId': authManger.appUser.value.id
              },
              options: Options(headers: {
                'accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': authManger.getToken(),
                'Accept-Language': '${Get.locale!.languageCode}',
              }))
          .then((response) {
        if (response.statusCode == 200) {
          print(response.data);
          items = List<ProviderReviews>.from(json
              .decode(json.encode(response.data['data']['items']))
              .map((x) => ProviderReviews.fromJson(x)));

          Get.back();
        }
      });
    } on DioError catch (e) {
      Get.back();
      commonTools.showFailedSnackBar(e.response!.data != null
          ? e.response!.data[0] != null
              ? e.response!.data[0]['errorMessage']
              : 'errorMessage'
          : 'errorMessage');
    }

    return items;
  }
}