puntacion
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class CommentEntity extends Equatable {
|
||||
final String authorId;
|
||||
final String destinationId;
|
||||
final String serviceId;
|
||||
final String content;
|
||||
final double score;
|
||||
final bool isFromUser;
|
||||
final Timestamp createdAt;
|
||||
|
||||
const CommentEntity({
|
||||
required this.authorId,
|
||||
required this.destinationId,
|
||||
required this.serviceId,
|
||||
required this.content,
|
||||
required this.score,
|
||||
required this.isFromUser,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
static CommentEntity fromDocument(Map<String, dynamic> doc) {
|
||||
return CommentEntity(
|
||||
authorId: doc['author_id'] as String,
|
||||
destinationId: doc['destination_id'] as String,
|
||||
serviceId: doc['service_id'] as String,
|
||||
content: doc['content'] as String,
|
||||
score: doc['score'] as double,
|
||||
isFromUser: doc['is_from_user'] as bool,
|
||||
createdAt: doc['created_at'] as Timestamp,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toDocument() {
|
||||
return {
|
||||
'author_id': authorId,
|
||||
'destination_id': destinationId,
|
||||
'service_id': serviceId,
|
||||
'content': content,
|
||||
'score': score,
|
||||
'is_from_user': isFromUser,
|
||||
'created_at': createdAt,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
authorId,
|
||||
destinationId,
|
||||
serviceId,
|
||||
content,
|
||||
score,
|
||||
isFromUser,
|
||||
createdAt,
|
||||
];
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '''CommentEntity{
|
||||
authorId: $authorId,
|
||||
destinationId: $destinationId,
|
||||
serviceId: $serviceId,
|
||||
content: $content,
|
||||
score: $score,
|
||||
isFromUser: $isFromUser,
|
||||
createdAt: $createdAt
|
||||
}''';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export '/src/entities/score_entity.dart';
|
||||
export '/src/entities/comment_entity.dart';
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class ReputationEntity extends Equatable {
|
||||
final int total;
|
||||
final double average;
|
||||
final int totalPro;
|
||||
final double averagePro;
|
||||
|
||||
const ReputationEntity({
|
||||
required this.total,
|
||||
required this.average,
|
||||
required this.totalPro,
|
||||
required this.averagePro,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [total, average, totalPro, averagePro];
|
||||
|
||||
static ReputationEntity fromDocument(Map<String, dynamic> doc) {
|
||||
final total = doc['total'] ?? 0;
|
||||
final totalPro = doc['total_pro'] ?? 0;
|
||||
final average = doc['average'] ?? 0.0;
|
||||
final averagePro = doc['average_pro'] ?? 0.0;
|
||||
|
||||
return ReputationEntity(
|
||||
total: int.parse(total.toString()),
|
||||
average: double.parse(average.toString()),
|
||||
totalPro: int.parse(totalPro.toString()),
|
||||
averagePro: double.parse(averagePro.toString()),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toDocument() {
|
||||
return {
|
||||
'total': total,
|
||||
'average': average,
|
||||
'total_pro': totalPro,
|
||||
'average_pro': averagePro,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:score_repository/score_repository.dart';
|
||||
import 'package:score_repository/src/entities/comment_entity.dart';
|
||||
|
||||
class FirebaseScoreRepository {
|
||||
final reputationsCollection =
|
||||
FirebaseFirestore.instance.collection('reputations');
|
||||
|
||||
final commentsCollection = FirebaseFirestore.instance.collection('comments');
|
||||
|
||||
ReputationEntity? _reputation;
|
||||
final StreamController<ReputationEntity> _reputationController =
|
||||
StreamController<ReputationEntity>.broadcast();
|
||||
|
||||
Stream<ReputationEntity> streamReputation() {
|
||||
return _reputationController.stream;
|
||||
}
|
||||
|
||||
FirebaseScoreRepository() {
|
||||
FirebaseAuth.instance.userChanges().listen((user) async {
|
||||
if (user != null) {
|
||||
final reputation = await getReputationByUserId(user.uid);
|
||||
_reputation = reputation;
|
||||
} else {
|
||||
_reputation = null;
|
||||
}
|
||||
_reputationController.add(_reputation ??
|
||||
const ReputationEntity(
|
||||
total: 0,
|
||||
average: 0,
|
||||
totalPro: 0,
|
||||
averagePro: 0,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
ReputationEntity getReputation() {
|
||||
return _reputation ??
|
||||
const ReputationEntity(
|
||||
total: 0,
|
||||
average: 0,
|
||||
totalPro: 0,
|
||||
averagePro: 0,
|
||||
);
|
||||
}
|
||||
|
||||
Future<ReputationEntity> getReputationByUserId(String userId) async {
|
||||
try {
|
||||
final snap = await reputationsCollection.doc(userId).get();
|
||||
return ReputationEntity.fromDocument(snap.data()!);
|
||||
} catch (e) {
|
||||
log("siteriespierdes4" + e.toString());
|
||||
return const ReputationEntity(
|
||||
total: 0,
|
||||
average: 0,
|
||||
totalPro: 0,
|
||||
averagePro: 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
addComment(CommentEntity comment) async {
|
||||
await commentsCollection.add(comment.toDocument());
|
||||
|
||||
final query = await commentsCollection
|
||||
.where('destination_id', isEqualTo: comment.destinationId)
|
||||
.where('is_from_user', isEqualTo: comment.isFromUser)
|
||||
.get();
|
||||
|
||||
var total = 0.0;
|
||||
var count = 0;
|
||||
for (var doc in query.docs) {
|
||||
final comment = CommentEntity.fromDocument(doc.data());
|
||||
total += comment.score;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
final average = total / count;
|
||||
if (comment.isFromUser) {
|
||||
await reputationsCollection.doc(comment.destinationId).set(
|
||||
{'total_pro': count, 'average_pro': average},
|
||||
SetOptions(merge: true));
|
||||
} else {
|
||||
await reputationsCollection.doc(comment.destinationId).set({
|
||||
'total': count,
|
||||
'average': average,
|
||||
}, SetOptions(merge: true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user