diff --git a/fbla_ui/lib/api_logic.dart b/fbla_ui/lib/api_logic.dart index dab862a..f9c35c7 100644 --- a/fbla_ui/lib/api_logic.dart +++ b/fbla_ui/lib/api_logic.dart @@ -8,8 +8,6 @@ import 'package:http/http.dart' as http; var apiAddress = 'https://homelab.marinodev.com/fbla-api'; var client = http.Client(); // var apiAddress = '192.168.0.114:8000'; -// var apiLogoAddress = 'https://homelab.marinodev.com/fbla-api'; -// var apiLogoAddress = 'http://192.168.0.114:8000'; Future fetchBusinessData() async { try { @@ -31,13 +29,59 @@ Future fetchBusinessData() async { } } +Future fetchBusinessDataOverview() async { + try { + var response = await http + .get(Uri.parse('$apiAddress/businessdata/overview')) + .timeout(const Duration(seconds: 20)); + if (response.statusCode == 200) { + var decodedResponse = json.decode(response.body); + Map> groupedBusinesses = {}; + + for (int i = 0; i < decodedResponse.length; i++) { + groupedBusinesses.addAll({ + JobType.values.byName(decodedResponse[i]): + decodedResponse.map((json) => Business.fromJson(json)).toList() + }); + } + + 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 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 createBusiness(Business business, String jwt) async { var json = ''' { "id": ${business.id}, "name": "${business.name}", "description": "${business.description}", - "type": "${business.type.name}", "website": "${business.website}", "contactName": "${business.contactName}", "contactEmail": "${business.contactEmail}", @@ -62,10 +106,10 @@ Future createBusiness(Business business, String jwt) async { } } -Future deleteBusiness(Business business, String jwt) async { +Future deleteBusiness(int id, String jwt) async { var json = ''' { - "id": ${business.id} + "id": $id } '''; @@ -89,7 +133,6 @@ Future editBusiness(Business business, String jwt) async { "id": ${business.id}, "name": "${business.name}", "description": "${business.description}", - "type": "${business.type.name}", "website": "${business.website}", "contactName": "${business.contactName}", "contactEmail": "${business.contactEmail}",