Cant join team if in team Profile page resets properly Post button spam no longer leads to crash due to duplicate calls Housekeeping: Made some name changes moved homescreenpages from layout -> homeFeed Deleted 2 unused classes, authService.dart and old loginPage.dart then renamed startPage.dart -> loginPage.dart Set up openApi on backend and added URL in Service classes
76 lines
2.2 KiB
Dart
76 lines
2.2 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:insparkspokalen_ui/services/googleAuthService.dart';
|
|
import 'package:insparkspokalen_ui/models/activityModel.dart';
|
|
|
|
|
|
/// API docs activityMS
|
|
/// https://group-10-15.pvt.dsv.su.se/calendar/swagger-ui/index.html
|
|
class ActivityService {
|
|
final String baseUrl = 'https://group-10-15.pvt.dsv.su.se/calendar';
|
|
//final String baseUrl = 'http://localhost:8080/activity';
|
|
|
|
// Hämtar alla aktiviteter
|
|
Future<List<ActivityModel>> fetchActivities() async {
|
|
try {
|
|
final response = await http.get(Uri.parse('$baseUrl/all'));
|
|
|
|
print("Response body: ${response.body}");
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> jsonResponse = json.decode(response.body);
|
|
return jsonResponse
|
|
.map((activity) => ActivityModel.fromJson(activity))
|
|
.toList();
|
|
} else {
|
|
throw Exception(
|
|
'Fel vid hämtning av aktiviteter: ${response.statusCode}',
|
|
);
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Ett fel uppstod vid hämtning av aktiviteter: $e');
|
|
}
|
|
}
|
|
|
|
//Hämta en specifik aktivitet
|
|
//kod
|
|
|
|
// Skapa en ny aktivitet med post
|
|
Future<void> createActivity(ActivityModel activity) async {
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/add'),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode(activity.toJson()),
|
|
);
|
|
|
|
if (response.statusCode != 201) {
|
|
throw Exception(
|
|
'Fel vid skapande av aktivitet: ${response.statusCode}',
|
|
);
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Ett fel uppstod vid skapande av aktivitet: $e');
|
|
}
|
|
}
|
|
|
|
// Tar bort en aktivitet baserat på modellen
|
|
Future<void> deleteActivity(ActivityModel activity) async {
|
|
final id = activity.id;
|
|
if (id == null) {
|
|
throw Exception("Aktivitet saknar ID och kan inte tas bort");
|
|
}
|
|
|
|
final url = Uri.parse('$baseUrl/${activity.id}');
|
|
print("debug delete url $url ");
|
|
final response = await http.delete(url);
|
|
|
|
if (response.statusCode == 200) {
|
|
print('Activity deleted successfully');
|
|
} else {
|
|
print('Failed to delete activity');
|
|
throw Exception('Failed to delete activity');
|
|
}
|
|
}
|
|
}
|