42 lines
1.1 KiB
Dart
42 lines
1.1 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:insparkspokalen_ui/models/missionModel.dart';
|
|
|
|
class MissionService {
|
|
final String baseUrl = 'https://group-10-15.pvt.dsv.su.se/mission/';
|
|
|
|
|
|
Future<List<MissionModel>> fetchMissions() async {
|
|
final response = await http.get(Uri.parse('$baseUrl/all'));
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = jsonDecode(response.body);
|
|
return data.map((json) => MissionModel.fromJson(json)).toList();
|
|
} else {
|
|
throw Exception('Misslyckades med att hämta uppdrag');
|
|
}
|
|
}
|
|
|
|
Future<void> addMission(MissionModel mission) async {
|
|
final response = await http.post(
|
|
Uri.parse(baseUrl),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode(mission.toJson()),
|
|
);
|
|
|
|
if (response.statusCode != 201) {
|
|
throw Exception('Misslyckades med att skapa uppdrag');
|
|
}
|
|
|
|
}
|
|
|
|
Future<void> deleteMission(int id) async {
|
|
final response = await http.delete(
|
|
Uri.parse('$baseUrl/$id'),
|
|
);
|
|
|
|
if (response.statusCode != 200) {
|
|
throw Exception('Misslyckades med att ta bort uppdrag');
|
|
}
|
|
}
|
|
} |