397 lines
13 KiB
Dart
397 lines
13 KiB
Dart
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<Business> businessList = List<Business>.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<Map<String, dynamic>> decodedResponse =
|
|
json.decode(response.body).cast<Map<String, dynamic>>();
|
|
|
|
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(
|
|
{Iterable<JobType>? typeFilters, Iterable<OfferType>? offerFilters}) async {
|
|
try {
|
|
String uriString = '$apiAddress/businessdata/overview/jobs';
|
|
if (typeFilters != null && typeFilters.isNotEmpty) {
|
|
uriString +=
|
|
'?typeFilters=${typeFilters.map((jobType) => jobType.name).join(',')}';
|
|
}
|
|
if (offerFilters != null && offerFilters.isNotEmpty) {
|
|
uriString +=
|
|
'?offerFilters=${offerFilters.map((offerType) => offerType.name).join(',')}';
|
|
}
|
|
Uri uri = Uri.parse(uriString);
|
|
|
|
var response = await http.get(uri).timeout(const Duration(seconds: 20));
|
|
if (response.statusCode == 200) {
|
|
List<Map<String, dynamic>> decodedResponse =
|
|
json.decode(response.body).cast<Map<String, dynamic>>();
|
|
|
|
List<Business> initialBusinesses =
|
|
decodedResponse.map((element) => Business.fromJson(element)).toList();
|
|
|
|
Map<JobType, List<Business>> groupedBusinesses = {};
|
|
|
|
for (Business business in initialBusinesses) {
|
|
for (JobListing job in business.listings!) {
|
|
List<Business> newBusinesses = groupedBusinesses[job.type!] ?? [];
|
|
Business newBusiness = Business.copy(business);
|
|
newBusiness.listings =
|
|
newBusiness.listings!.where((element) => element == job).toList();
|
|
newBusinesses.add(newBusiness);
|
|
groupedBusinesses.addAll({job.type!: newBusinesses});
|
|
}
|
|
}
|
|
|
|
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<BusinessType>? 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<BusinessType, List<Business>> groupedBusinesses = {};
|
|
|
|
for (String stringType in decodedResponse.keys) {
|
|
List<Business> businesses = [];
|
|
|
|
for (Map<String, dynamic> 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<int> 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<dynamic> decodedResponse = json.decode(response.body);
|
|
|
|
List<Business> businesses = decodedResponse
|
|
.map<Business>((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')}",
|
|
"type": "${listing.type!.name}",
|
|
"offerType": "${listing.offerType!.name}",
|
|
"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}",
|
|
"offerType": "${listing.offerType!.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'),
|
|
);
|
|
|
|
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}';
|
|
}
|
|
}
|