99 lines
2.5 KiB
Dart
99 lines
2.5 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:prosappco/src/authentication/authentication_repository.dart';
|
|
|
|
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
|
|
|
class EventoService {
|
|
Future<void> createEvent(String title, String description, String day,
|
|
String range1Hour1, String range1Hour2, String professionalId) async {
|
|
try {
|
|
await FirebaseFirestore.instance.collection('services').add({
|
|
'user_id': uid,
|
|
'title': title,
|
|
'description': description,
|
|
'day': day,
|
|
'range1Hour1': range1Hour1,
|
|
'range1Hour2': range1Hour2,
|
|
'professional_id': professionalId
|
|
}).then((value) {
|
|
FirebaseFirestore.instance.collection('users').doc(uid).update({
|
|
'services': FieldValue.arrayUnion([value.id])
|
|
});
|
|
});
|
|
} catch (e) {
|
|
print('Evento $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<List<Event>> getByProId(String day) async {
|
|
try {
|
|
var snapshot = await FirebaseFirestore.instance
|
|
.collection('services')
|
|
.where('professional_id', isEqualTo: uid)
|
|
.where('day', isEqualTo: day.toString())
|
|
.get();
|
|
|
|
List<Event> eventos = [];
|
|
for (var element in snapshot.docs) {
|
|
eventos.add(Event.fromJson(element.data()));
|
|
}
|
|
print('eventos $eventos');
|
|
return eventos;
|
|
} catch (e) {
|
|
print('Error getByProId $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
class Event {
|
|
String? title;
|
|
String? description;
|
|
String? day;
|
|
String? range1Hour1;
|
|
String? range1Hour2;
|
|
String? userId;
|
|
String? professionalId;
|
|
|
|
Event({
|
|
this.title,
|
|
this.description,
|
|
this.day,
|
|
this.range1Hour1,
|
|
this.range1Hour2,
|
|
this.userId,
|
|
this.professionalId,
|
|
});
|
|
|
|
factory Event.fromJson(Map<String, dynamic> json) {
|
|
return Event(
|
|
title: json['title'],
|
|
description: json['description'],
|
|
day: json['day'],
|
|
range1Hour1: json['range1Hour1'],
|
|
range1Hour2: json['range1Hour2'],
|
|
userId: json['user_id'],
|
|
professionalId: json['professional_id'],
|
|
);
|
|
}
|
|
|
|
static Future<List<Event>> getEventsAllById(String uid) async {
|
|
try {
|
|
var snapshot = await FirebaseFirestore.instance
|
|
.collection('services')
|
|
.where('professional_id', isEqualTo: uid)
|
|
.get();
|
|
|
|
List<Event> eventos = [];
|
|
for (var element in snapshot.docs) {
|
|
eventos.add(Event.fromJson(element.data()));
|
|
}
|
|
print('eventos $eventos');
|
|
return eventos;
|
|
} catch (e) {
|
|
print('Error getByProId $e');
|
|
return [];
|
|
}
|
|
}
|
|
}
|