43 lines
1.3 KiB
Dart
43 lines
1.3 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import '../models/informationModel.dart';
|
|
|
|
class InformationService {
|
|
final String baseUrl = "https://group-10-15.pvt.dsv.su.se/information/";
|
|
|
|
// Hämtar alla information
|
|
Future<List<InformationModel>> fetchInformation() async {
|
|
final response = await http.get(Uri.parse('$baseUrl/all'));
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> jsonList = json.decode(response.body);
|
|
return jsonList.map((e) => InformationModel.fromJson(e)).toList();
|
|
} else {
|
|
throw Exception("Misslyckades att hämta information");
|
|
}
|
|
}
|
|
|
|
// Skapar ny information
|
|
Future<void> createInformation(InformationModel info) async {
|
|
final response = await http.post(
|
|
Uri.parse(baseUrl),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: json.encode(info.toJson()),
|
|
);
|
|
|
|
if (response.statusCode != 201) {
|
|
throw Exception("Misslyckades att skapa information");
|
|
}
|
|
}
|
|
|
|
// Tar bort information
|
|
Future<void> deleteInformation(InformationModel info) async {
|
|
final response = await http.delete(
|
|
Uri.parse('$baseUrl/${info.title}'), // Förutsätter att meddelandet identifieras med title (kan justeras)
|
|
);
|
|
|
|
if (response.statusCode != 200) {
|
|
throw Exception("Misslyckades att ta bort information");
|
|
}
|
|
}
|
|
} |