128 lines
4.2 KiB
Dart
128 lines
4.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
import 'package:insparkspokalen_ui/models/userModel.dart';
|
|
import 'package:insparkspokalen_ui/services/teamUserSyncService.dart';
|
|
import 'package:insparkspokalen_ui/services/googleAuthService.dart';
|
|
|
|
class UserService {
|
|
final String baseUrlUser = 'https://group-10-15.pvt.dsv.su.se/user';
|
|
final TeamUserSyncService _syncService = TeamUserSyncService();
|
|
//final String baseUrl = 'http://localhost:8080';
|
|
|
|
Future<List<UserModel>> showUsers() async {
|
|
final endpoint = Uri.parse('$baseUrlUser/all');
|
|
//final endpoint = Uri.parse('$baseUrl/all');
|
|
|
|
try {
|
|
final response = await http.get(endpoint);
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = jsonDecode(response.body);
|
|
return data.map((json) => UserModel.fromJson(json)).toList();
|
|
} else {
|
|
return [];
|
|
}
|
|
} catch (e) {
|
|
print("Error fetching users: $e");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<void> createOrFetchUser(GoogleSignInAccount account) async {
|
|
final service = UserService();
|
|
final email = account.email;
|
|
|
|
try {
|
|
final getUserEndpoint = Uri.parse('${service.baseUrlUser}/by-email/$email');
|
|
final getUserResponse = await http.get(getUserEndpoint);
|
|
|
|
UserModel user;
|
|
|
|
if (getUserResponse.statusCode == 200 && getUserResponse.body.isNotEmpty) {
|
|
user = UserModel.fromJson(jsonDecode(getUserResponse.body));
|
|
print("User fetched: ${user.toJson()}");
|
|
} else {
|
|
user = UserModel(
|
|
email,
|
|
account.displayName ?? 'Unknown'
|
|
// account.photoUrl ?? '',
|
|
);
|
|
|
|
final createUserEndpoint = Uri.parse('${service.baseUrlUser}/create-user');
|
|
final createUserResponse = await http.post(
|
|
createUserEndpoint,
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode(user.toJson()),
|
|
);
|
|
// print('Status: ${createUserResponse.statusCode}'); //debug
|
|
// print('Body: ${createUserResponse.body}'); //debug
|
|
if (!(createUserResponse.statusCode == 200 || createUserResponse.statusCode == 201)) {
|
|
throw Exception("Failed to create user");
|
|
}
|
|
GoogleAuthService.setCurrentUser(user);
|
|
print("User created: ${user.toJson()}");
|
|
} //else
|
|
|
|
// Update current user in both services
|
|
GoogleAuthService.setCurrentUser(user);
|
|
|
|
// This will trigger team synchronization
|
|
final teamUserSync = TeamUserSyncService();
|
|
teamUserSync.setCurrentUser(user);
|
|
|
|
// // Cache user
|
|
// final prefs = await SharedPreferences.getInstance();
|
|
// prefs.setString('user', jsonEncode(user.toJson()));
|
|
} catch (e) {
|
|
print("UserService error: $e");
|
|
}
|
|
}
|
|
|
|
static Future<String> getUserRole(String email) async {
|
|
final service = UserService();
|
|
final endpoint = Uri.parse('${service.baseUrlUser}/get-role/$email');
|
|
|
|
final response = await http.get(endpoint);
|
|
|
|
if (response.statusCode == 200) {
|
|
return response.body.replaceAll('"', '').toUpperCase();
|
|
} else {
|
|
throw Exception("Failed to retrieve role for $email");
|
|
}
|
|
}
|
|
|
|
Future<UserModel> getUserByEmail(String email) async {
|
|
final url = Uri.parse('$baseUrlUser/by-email/$email');
|
|
final response = await http.get(url);
|
|
|
|
if (response.statusCode == 200) {
|
|
return UserModel.fromJson(jsonDecode(response.body));
|
|
} else {
|
|
throw Exception('Failed to load user');
|
|
}
|
|
}
|
|
|
|
// Use the sync service to add team to user
|
|
Future<bool> addTeamToUser(int teamId) async {
|
|
return await _syncService.joinTeam(teamId);
|
|
}
|
|
|
|
Future<bool> removeUserFromTeam() async {
|
|
return await _syncService.leaveTeam();
|
|
}
|
|
|
|
|
|
// Retry mechanism for adding a user to a team
|
|
Future<bool> _retryAddTeamToUser(String encodedEmail, int teamId) async {
|
|
try {
|
|
// Try with a timeout parameter
|
|
final url = Uri.parse('$baseUrlUser/join-team/$encodedEmail/$teamId');
|
|
final response = await http.get(url).timeout(Duration(seconds: 10));
|
|
return response.statusCode == 200;
|
|
} catch (e) {
|
|
print("Retry join team error: $e");
|
|
return false;
|
|
}
|
|
}
|
|
} |