FBLA24/fbla_ui/lib/api_logic.dart
2024-06-17 16:56:35 +00:00

326 lines
10 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:fbla_ui/shared.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 fetchBusinessDataOverview({List<JobType>? typeFilters}) async {
try {
String? typeString =
typeFilters?.map((jobType) => jobType.name).toList().join(',');
Uri uri =
Uri.parse('$apiAddress/businessdata/overview?filters=$typeString');
if (typeFilters == null || typeFilters.isEmpty) {
uri = Uri.parse('$apiAddress/businessdata/overview');
}
var response = await http.get(uri).timeout(const Duration(seconds: 20));
if (response.statusCode == 200) {
var decodedResponse = json.decode(response.body);
Map<JobType, List<Business>> groupedBusinesses = {};
for (String stringType in decodedResponse.keys) {
List<Business> businesses = [];
for (Map<String, dynamic> 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 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 createBusiness(Business business) async {
var json = '''
{
"id": ${business.id},
"name": "${business.name}",
"description": "${business.description}",
"website": "${business.website}",
"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}",
"wage": "${listing.wage}",
"link": "${listing.link}"
}
''';
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 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}",
"website": "${business.website}",
"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}",
"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}';
}
}