56 lines
1.6 KiB
Dart
56 lines
1.6 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import '../../models/informationModel.dart';
|
|
|
|
///API docs InfoMS
|
|
///https://group-10-15.pvt.dsv.su.se/information/swagger-ui/index.html
|
|
class InformationService {
|
|
final String baseUrl = 'https://group-10-15.pvt.dsv.su.se/information/';
|
|
|
|
Future<List<InformationModel>> fetchInformation() async {
|
|
final url = Uri.parse('$baseUrl/all');
|
|
final response = await http.get(url);
|
|
|
|
print('Status Code: ${response.statusCode}');
|
|
print('Response Body: ${response.body}');
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = json.decode(response.body);
|
|
return data.map((json) => InformationModel.fromJson(json)).toList();
|
|
} else {
|
|
throw Exception('Failed to load information');
|
|
}
|
|
}
|
|
|
|
Future<void> deleteInformation(InformationModel info) async {
|
|
final url = Uri.parse('$baseUrl/${info.id}');
|
|
final response = await http.delete(url);
|
|
|
|
if (response.statusCode == 200) {
|
|
print('Information deleted successfully');
|
|
} else {
|
|
print('Failed to delete information');
|
|
throw Exception('Failed to delete information');
|
|
}
|
|
}
|
|
|
|
Future<void> createInformation(InformationModel info) async {
|
|
final url = Uri.parse(baseUrl);
|
|
final body = json.encode(info.toJson());
|
|
print('POST to $baseUrl with body: $body');
|
|
|
|
final response = await http.post(
|
|
url,
|
|
headers: {"Content-Type": "application/json"},
|
|
body: body,
|
|
);
|
|
|
|
print('Create status: ${response.statusCode}');
|
|
print('Response: ${response.body}');
|
|
|
|
if (response.statusCode != 201) {
|
|
throw Exception('Failed to create information');
|
|
}
|
|
}
|
|
}
|