chat
This commit is contained in:
@@ -1,12 +1,24 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:prosappco/src/models/user_model.dart';
|
||||
|
||||
class ChatModel {
|
||||
List<MessageModel> messages;
|
||||
String professional_id;
|
||||
String user_id;
|
||||
UserModel? user;
|
||||
UserModel? professional;
|
||||
|
||||
ChatModel({required this.messages});
|
||||
ChatModel({
|
||||
required this.messages,
|
||||
required this.professional_id,
|
||||
required this.user_id,
|
||||
this.user,
|
||||
this.professional,
|
||||
});
|
||||
|
||||
static ChatModel fromDocumentSnapshot(
|
||||
DocumentSnapshot<Map<String, dynamic>> snapshot) {
|
||||
static Future<ChatModel> fromDocumentSnapshot2(
|
||||
DocumentSnapshot<Map<String, dynamic>> snapshot,
|
||||
bool fillUserModel) async {
|
||||
try {
|
||||
List<MessageModel> messages = [];
|
||||
List<dynamic> messagesData = snapshot.get('message') ?? [];
|
||||
@@ -19,12 +31,73 @@ class ChatModel {
|
||||
));
|
||||
}
|
||||
|
||||
return ChatModel(messages: messages);
|
||||
return ChatModel(
|
||||
messages: messages,
|
||||
professional_id: snapshot.get('professional_id'),
|
||||
user_id: snapshot.get('user_id'),
|
||||
user: fillUserModel
|
||||
? await UserModel.getUser(snapshot.get('user_id'))
|
||||
: null,
|
||||
);
|
||||
} catch (e) {
|
||||
print('error $e');
|
||||
return ChatModel(messages: []);
|
||||
return ChatModel(messages: [], professional_id: '', user_id: '');
|
||||
}
|
||||
}
|
||||
|
||||
static ChatModel fromDocumentSnapshot(
|
||||
DocumentSnapshot<Map<String, dynamic>> snapshot,
|
||||
) {
|
||||
try {
|
||||
List<MessageModel> messages = [];
|
||||
List<dynamic> messagesData = snapshot.get('message') ?? [];
|
||||
|
||||
for (var data in messagesData) {
|
||||
messages.add(MessageModel(
|
||||
user: data['user'] ?? '',
|
||||
content: data['content'] ?? '',
|
||||
timestamp: (data['timestamp'] as Timestamp).toDate(),
|
||||
));
|
||||
}
|
||||
|
||||
return ChatModel(
|
||||
messages: messages,
|
||||
professional_id: snapshot.get('professional_id'),
|
||||
user_id: snapshot.get('user_id'),
|
||||
);
|
||||
} catch (e) {
|
||||
print('error $e');
|
||||
return ChatModel(messages: [], professional_id: '', user_id: '');
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<ChatModel>> getChatsByProId(String userReceived) async {
|
||||
final receivedScoresQuery = FirebaseFirestore.instance
|
||||
.collection('chats')
|
||||
.where('professional_id', isEqualTo: userReceived);
|
||||
|
||||
final receivedScoresSnapshot = await receivedScoresQuery.get();
|
||||
|
||||
final receivedScores = await Future.wait(receivedScoresSnapshot.docs
|
||||
.map((doc) async => await fromDocumentSnapshot2(doc, true))
|
||||
.toList());
|
||||
|
||||
return receivedScores;
|
||||
}
|
||||
|
||||
static Future<List<ChatModel>> getChatsByUserId(String userReceived) async {
|
||||
final receivedScoresQuery = FirebaseFirestore.instance
|
||||
.collection('chats')
|
||||
.where('user_id', isEqualTo: userReceived);
|
||||
|
||||
final receivedScoresSnapshot = await receivedScoresQuery.get();
|
||||
|
||||
final receivedScores = await Future.wait(receivedScoresSnapshot.docs
|
||||
.map((doc) async => await fromDocumentSnapshot2(doc, true))
|
||||
.toList());
|
||||
|
||||
return receivedScores;
|
||||
}
|
||||
}
|
||||
|
||||
class MessageModel {
|
||||
|
||||
@@ -96,6 +96,28 @@ Future<List<Event>> getByProIdAll(String state1, String state2) async {
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Event>> getByUserIdAll(String state1, String state2) async {
|
||||
try {
|
||||
var snapshot = await FirebaseFirestore.instance
|
||||
.collection('services')
|
||||
.where('user_id', isEqualTo: uid)
|
||||
.where('status', whereIn: [state1, state2]).get();
|
||||
|
||||
List<Event> eventos = [];
|
||||
for (var element in snapshot.docs) {
|
||||
final event = Event.fromJson(element.data());
|
||||
event.scoresModel = await ScoresModel.scoreTo(event.userId, false, false);
|
||||
event.id = element.id;
|
||||
eventos.add(event);
|
||||
}
|
||||
print('eventos $eventos');
|
||||
return eventos;
|
||||
} catch (e) {
|
||||
print('Error getByUserId $e');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
class Event {
|
||||
String? id;
|
||||
String title;
|
||||
@@ -151,9 +173,17 @@ class Event {
|
||||
final snapshot = await FirebaseFirestore.instance
|
||||
.collection('services')
|
||||
.doc(uid)
|
||||
.get();
|
||||
final Map<String, dynamic>? data = snapshot.data();
|
||||
return Event.fromJson(data!);
|
||||
.get()
|
||||
.then((docSnapshot) {
|
||||
if (docSnapshot.exists) {
|
||||
print('info ${docSnapshot.data()}');
|
||||
} else {
|
||||
print('No se encontró ningún documento con la ruta especificada');
|
||||
}
|
||||
;
|
||||
});
|
||||
final Map<String, dynamic> data = snapshot.data();
|
||||
return Event.fromJson(data);
|
||||
} catch (e) {
|
||||
print('Error getting user: $e');
|
||||
return Event(
|
||||
|
||||
@@ -5,26 +5,28 @@ import 'package:prosappco/src/screens/professional.dart';
|
||||
class UserModel {
|
||||
final String name;
|
||||
final String city;
|
||||
final String? profession;
|
||||
final String? state;
|
||||
final Reference? photo;
|
||||
|
||||
UserModel(this.name, this.city, this.state, this.photo);
|
||||
UserModel(this.name, this.city, this.profession, this.state, this.photo);
|
||||
|
||||
static Future<UserModel> fromJson(
|
||||
Map<String, dynamic>? json, String uid) async {
|
||||
try {
|
||||
if (json == null) return UserModel('', '', null, null);
|
||||
if (json == null) return UserModel('', '', '', null, null);
|
||||
|
||||
String? avatar = json['photo'];
|
||||
return UserModel(
|
||||
json['name'],
|
||||
json['city'],
|
||||
json['profesion'],
|
||||
json['estado'],
|
||||
avatar != null ? storage.ref().child(avatar) : null,
|
||||
);
|
||||
} catch (e) {
|
||||
print('XD user $e');
|
||||
return UserModel('', '', null, null);
|
||||
return UserModel('', '', '', null, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +38,7 @@ class UserModel {
|
||||
return fromJson(data, uid);
|
||||
} catch (e) {
|
||||
print('Error getting user: $e');
|
||||
return UserModel('', '', null, null);
|
||||
return UserModel('', '', '', null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user