FBLA24/fbla_ui/lib/api_logic.dart
2024-06-13 15:27:13 -05:00

200 lines
6.1 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 client = http.Client();
// var apiAddress = '192.168.0.114:8000';
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 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<JobType, List<Business>> 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}",
"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 deleteBusiness(int id, String jwt) 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 editBusiness(Business business, String jwt) 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 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}';
}
}