import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:fbla_ui/shared/global_vars.dart'; import 'package:fbla_ui/shared/utils.dart'; import 'package:http/http.dart' as http; var apiAddress = 'https://homelab.marinodev.com/fbla-api'; // var apiAddress = 'http://192.168.0.114:8000/fbla-api'; var client = http.Client(); Future fetchBusinessData() async { try { var response = await http .get(Uri.parse('$apiAddress/businessdata')) .timeout(const Duration(seconds: 20)); if (response.statusCode == 200) { var decodedResponse = json.decode(response.body); List businessList = List.from( decodedResponse.map((json) => Business.fromJson(json)).toList()); return businessList; } else { return 'Error ${response.statusCode}! Please try again later!'; } } on TimeoutException { return 'Unable to connect to server (timeout).\nPlease try again later.'; } on SocketException { return 'Unable to connect to server (socket exception).\nPlease check your internet connection.\n'; } } Future fetchBusinessNames() async { try { var response = await http .get(Uri.parse('$apiAddress/businessdata/businessnames')) .timeout(const Duration(seconds: 20)); if (response.statusCode == 200) { List> decodedResponse = json.decode(response.body).cast>(); return decodedResponse; } else { return 'Error ${response.statusCode}! Please try again later!'; } } on TimeoutException { return 'Unable to connect to server (timeout).\nPlease try again later.'; } on SocketException { return 'Unable to connect to server (socket exception).\nPlease check your internet connection.\n'; } } Future fetchBusinessDataOverviewJobs({List? typeFilters}) async { try { String? typeString = typeFilters?.map((jobType) => jobType.name).toList().join(','); Uri uri = Uri.parse('$apiAddress/businessdata/overview/jobs?filters=$typeString'); if (typeFilters == null || typeFilters.isEmpty) { uri = Uri.parse('$apiAddress/businessdata/overview/jobs'); } var response = await http.get(uri).timeout(const Duration(seconds: 20)); if (response.statusCode == 200) { var decodedResponse = json.decode(response.body); Map> groupedBusinesses = {}; for (String stringType in decodedResponse.keys) { List businesses = []; for (Map map in decodedResponse[stringType]) { Business business = Business.fromJson(map); businesses.add(business); } groupedBusinesses .addAll({JobType.values.byName(stringType): businesses}); } return groupedBusinesses; } else { return 'Error ${response.statusCode}! Please try again later!'; } } on TimeoutException { return 'Unable to connect to server (timeout).\nPlease try again later.'; } on SocketException { return 'Unable to connect to server (socket exception).\nPlease check your internet connection.\n'; } } Future fetchBusinessDataOverviewTypes({List? typeFilters}) async { try { String? typeString = typeFilters?.map((jobType) => jobType.name).toList().join(','); Uri uri = Uri.parse( '$apiAddress/businessdata/overview/types?filters=$typeString'); if (typeFilters == null || typeFilters.isEmpty) { uri = Uri.parse('$apiAddress/businessdata/overview/types'); } var response = await http.get(uri).timeout(const Duration(seconds: 20)); if (response.statusCode == 200) { var decodedResponse = json.decode(response.body); Map> groupedBusinesses = {}; for (String stringType in decodedResponse.keys) { List businesses = []; for (Map map in decodedResponse[stringType]) { map.addAll({'type': stringType}); Business business = Business.fromJson(map); businesses.add(business); } groupedBusinesses .addAll({BusinessType.values.byName(stringType): businesses}); } return groupedBusinesses; } else { return 'Error ${response.statusCode}! Please try again later!'; } } on TimeoutException { return 'Unable to connect to server (timeout).\nPlease try again later.'; } on SocketException { return 'Unable to connect to server (socket exception).\nPlease check your internet connection.\n'; } } Future fetchBusinesses(List ids) async { try { var response = await http .get(Uri.parse( '$apiAddress/businessdata/businesses?businesses=${ids.join(',')}')) .timeout(const Duration(seconds: 20)); if (response.statusCode == 200) { List decodedResponse = json.decode(response.body); List businesses = decodedResponse .map((json) => Business.fromJson(json)) .toList(); return businesses; } else { return 'Error ${response.statusCode}! Please try again later!'; } } on TimeoutException { return 'Unable to connect to server (timeout).\nPlease try again later.'; } on SocketException { return 'Unable to connect to server (socket exception).\nPlease check your internet connection.\n'; } } Future fetchBusiness(int id) async { try { var response = await http .get(Uri.parse('$apiAddress/businessdata/business/$id')) .timeout(const Duration(seconds: 20)); if (response.statusCode == 200) { var decodedResponse = json.decode(response.body); Business business = Business.fromJson(decodedResponse); return business; } else { return 'Error ${response.statusCode}! Please try again later!'; } } on TimeoutException { return 'Unable to connect to server (timeout).\nPlease try again later.'; } on SocketException { return 'Unable to connect to server (socket exception).\nPlease check your internet connection.\n'; } } Future fetchJob(int id) async { try { var response = await http .get(Uri.parse('$apiAddress/businessdata/jobs/$id')) .timeout(const Duration(seconds: 20)); if (response.statusCode == 200) { var decodedResponse = json.decode(response.body); Business business = Business.fromJson(decodedResponse); return business; } else { return 'Error ${response.statusCode}! Please try again later!'; } } on TimeoutException { return 'Unable to connect to server (timeout).\nPlease try again later.'; } on SocketException { return 'Unable to connect to server (socket exception).\nPlease check your internet connection.\n'; } } Future createBusiness(Business business) async { var json = ''' { "id": ${business.id}, "name": "${business.name}", "description": "${business.description?.replaceAll('\n', '\\n')}", "website": "${business.website}", "type": "${business.type!.name}", "contactName": "${business.contactName}", "contactEmail": "${business.contactEmail}", "contactPhone": "${business.contactPhone}", "notes": "${business.notes}", "locationName": "${business.locationName}", "locationAddress": "${business.locationAddress}" } '''; try { var response = await http.post(Uri.parse('$apiAddress/createbusiness'), body: json, headers: {'Authorization': jwt}).timeout(const Duration(seconds: 20)); if (response.statusCode != 200) { return response.body; } } on TimeoutException { return 'Unable to connect to server (timeout). Please try again later'; } on SocketException { return 'Unable to connect to server (socket exception). Please check your internet connection.'; } } Future createListing(JobListing listing) async { var json = ''' { "id": ${listing.id}, "businessId": ${listing.businessId}, "name": "${listing.name}", "description": "${listing.description.replaceAll('\n', '\\n')}", "wage": "${listing.wage}", "link": "${listing.link}" } '''; try { var response = await http.post(Uri.parse('$apiAddress/createlisting'), body: json, headers: {'Authorization': jwt}).timeout(const Duration(seconds: 20)); if (response.statusCode != 200) { return response.body; } } on TimeoutException { return 'Unable to connect to server (timeout). Please try again later'; } on SocketException { return 'Unable to connect to server (socket exception). Please check your internet connection.'; } } Future deleteBusiness(int id) async { var json = ''' { "id": $id } '''; try { var response = await http.post(Uri.parse('$apiAddress/deletebusiness'), body: json, headers: {'Authorization': jwt}).timeout(const Duration(seconds: 20)); if (response.statusCode != 200) { return response.body; } } on TimeoutException { return 'Unable to connect to server (timeout). Please try again later'; } on SocketException { return 'Unable to connect to server (socket exception). Please check your internet connection.'; } } Future deleteListing(int id) async { var json = ''' { "id": $id } '''; try { var response = await http.post(Uri.parse('$apiAddress/deletelisting'), body: json, headers: {'Authorization': jwt}).timeout(const Duration(seconds: 20)); if (response.statusCode != 200) { return response.body; } } on TimeoutException { return 'Unable to connect to server (timeout). Please try again later'; } on SocketException { return 'Unable to connect to server (socket exception). Please check your internet connection.'; } } Future editBusiness(Business business) async { var json = ''' { "id": ${business.id}, "name": "${business.name}", "description": "${business.description?.replaceAll('\n', '\\n')}", "website": "${business.website}", "type": "${business.type!.name}", "contactName": "${business.contactName}", "contactEmail": "${business.contactEmail}", "contactPhone": "${business.contactPhone}", "notes": "${business.notes}", "locationName": "${business.locationName}", "locationAddress": "${business.locationAddress}" } '''; try { var response = await http.post(Uri.parse('$apiAddress/editbusiness'), body: json, headers: {'Authorization': jwt}).timeout(const Duration(seconds: 20)); if (response.statusCode != 200) { return response.body; } } on TimeoutException { return 'Unable to connect to server (timeout). Please try again later'; } on SocketException { return 'Unable to connect to server (socket exception). Please check your internet connection.'; } } Future editListing(JobListing listing) async { var json = ''' { "id": ${listing.id}, "businessId": ${listing.businessId}, "name": "${listing.name}", "description": "${listing.description.replaceAll('\n', '\\n')}", "type": "${listing.type!.name}", "wage": "${listing.wage}", "link": "${listing.link}" } '''; try { var response = await http.post(Uri.parse('$apiAddress/editlisting'), body: json, headers: {'Authorization': jwt}).timeout(const Duration(seconds: 20)); if (response.statusCode != 200) { return response.body; } } on TimeoutException { return 'Unable to connect to server (timeout). Please try again later'; } on SocketException { return 'Unable to connect to server (socket exception). Please check your internet connection.'; } } Future signIn(String username, String password) async { var json = ''' { "username": "$username", "password": "$password" } '''; var response = await http.post( Uri.parse('$apiAddress/signin'), body: json, ); if (response.statusCode == 200) { return response.body; } else { return 'Error: ${response.body}'; } } Future marinoDevLogo() async { var response = await http.get( Uri.parse('$apiAddress/marinodev'), ); // File logo = File ('${getTemporaryDirectory().toString()}/marinodev.svg'); // logo.writeAsBytes(response.bodyBytes); return response.bodyBytes; } Future getLogo(int logoId) async { var response = await http.get( Uri.parse('$apiAddress/logos/$logoId'), ); if (response.statusCode == 200) { return response.bodyBytes; } else { return 'Error ${response.statusCode}'; } }