144 lines
4.9 KiB
Dart
144 lines
4.9 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:firebase_storage/firebase_storage.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
import 'package:em2rp/models/event_model.dart';
|
|
import 'package:em2rp/models/event_type_model.dart';
|
|
import 'package:em2rp/models/user_model.dart';
|
|
import 'dart:developer' as developer;
|
|
|
|
class EventFormService {
|
|
static Future<List<EventType>> fetchEventTypes() async {
|
|
developer.log('Fetching event types from Firestore...', name: 'EventFormService');
|
|
try {
|
|
final snapshot = await FirebaseFirestore.instance.collection('eventTypes').get();
|
|
final eventTypes = snapshot.docs.map((doc) => EventType.fromFirestore(doc)).toList();
|
|
developer.log('${eventTypes.length} event types loaded.', name: 'EventFormService');
|
|
return eventTypes;
|
|
} catch (e, s) {
|
|
developer.log('Error fetching event types', name: 'EventFormService', error: e, stackTrace: s);
|
|
throw Exception("Could not load event types. Please check Firestore permissions.");
|
|
}
|
|
}
|
|
|
|
static Future<List<UserModel>> fetchUsers() async {
|
|
try {
|
|
final snapshot = await FirebaseFirestore.instance.collection('users').get();
|
|
return snapshot.docs.map((doc) => UserModel.fromMap(doc.data(), doc.id)).toList();
|
|
} catch (e) {
|
|
developer.log('Error fetching users', name: 'EventFormService', error: e);
|
|
throw Exception("Could not load users.");
|
|
}
|
|
}
|
|
|
|
static Future<List<Map<String, String>>> uploadFiles(List<PlatformFile> files) async {
|
|
List<Map<String, String>> uploadedFiles = [];
|
|
|
|
for (final file in files) {
|
|
final fileBytes = file.bytes;
|
|
final fileName = file.name;
|
|
|
|
if (fileBytes != null) {
|
|
final ref = FirebaseStorage.instance.ref().child(
|
|
'events/temp/${DateTime.now().millisecondsSinceEpoch}_$fileName');
|
|
final uploadTask = await ref.putData(fileBytes);
|
|
final url = await uploadTask.ref.getDownloadURL();
|
|
uploadedFiles.add({'name': fileName, 'url': url});
|
|
} else {
|
|
throw Exception("Impossible de lire le fichier $fileName");
|
|
}
|
|
}
|
|
|
|
return uploadedFiles;
|
|
}
|
|
|
|
static Future<String?> moveEventFileHttp({
|
|
required String sourcePath,
|
|
required String destinationPath,
|
|
}) async {
|
|
final url = Uri.parse('https://us-central1-em2rp-951dc.cloudfunctions.net/moveEventFileV2');
|
|
final user = FirebaseAuth.instance.currentUser;
|
|
final idToken = await user?.getIdToken();
|
|
|
|
final response = await http.post(
|
|
url,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
if (idToken != null) 'Authorization': 'Bearer $idToken',
|
|
},
|
|
body: jsonEncode({
|
|
'data': {
|
|
'sourcePath': sourcePath,
|
|
'destinationPath': destinationPath,
|
|
}
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
if (data['url'] != null) {
|
|
return data['url'] as String;
|
|
} else if (data['result'] != null && data['result']['url'] != null) {
|
|
return data['result']['url'] as String;
|
|
}
|
|
return null;
|
|
} else {
|
|
print('Erreur Cloud Function: \n${response.body}');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<String> createEvent(EventModel event) async {
|
|
final docRef = await FirebaseFirestore.instance.collection('events').add(event.toMap());
|
|
return docRef.id;
|
|
}
|
|
|
|
static Future<void> updateEvent(EventModel event) async {
|
|
final docRef = FirebaseFirestore.instance.collection('events').doc(event.id);
|
|
await docRef.update(event.toMap());
|
|
}
|
|
|
|
static Future<List<Map<String, String>>> moveFilesToEvent(
|
|
List<Map<String, String>> tempFiles, String eventId) async {
|
|
List<Map<String, String>> newFiles = [];
|
|
|
|
for (final file in tempFiles) {
|
|
final fileName = file['name']!;
|
|
final oldUrl = file['url']!;
|
|
|
|
String sourcePath;
|
|
final tempPattern = RegExp(r'events/temp/[^?]+');
|
|
final match = tempPattern.firstMatch(oldUrl);
|
|
if (match != null) {
|
|
sourcePath = match.group(0)!;
|
|
} else {
|
|
final tempFileName = Uri.decodeComponent(oldUrl.split('/').last.split('?').first);
|
|
sourcePath = tempFileName;
|
|
}
|
|
|
|
final destinationPath = 'events/$eventId/$fileName';
|
|
final newUrl = await moveEventFileHttp(
|
|
sourcePath: sourcePath,
|
|
destinationPath: destinationPath,
|
|
);
|
|
|
|
if (newUrl != null) {
|
|
newFiles.add({'name': fileName, 'url': newUrl});
|
|
} else {
|
|
newFiles.add({'name': fileName, 'url': oldUrl});
|
|
}
|
|
}
|
|
|
|
return newFiles;
|
|
}
|
|
|
|
static Future<void> updateEventDocuments(String eventId, List<Map<String, String>> documents) async {
|
|
await FirebaseFirestore.instance
|
|
.collection('events')
|
|
.doc(eventId)
|
|
.update({'documents': documents});
|
|
}
|
|
}
|