puntacion

This commit is contained in:
Felipe
2024-04-17 00:48:46 -05:00
parent 61d517aaea
commit 141d11a141
20 changed files with 1078 additions and 77 deletions
@@ -0,0 +1,4 @@
library chat_repository;
export 'src/entities/entities.dart';
export 'src/repositories/firebase_score_repository.dart';
@@ -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));
}
}
}
}
+306
View File
@@ -0,0 +1,306 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_flutterfire_internals:
dependency: transitive
description:
name: _flutterfire_internals
sha256: "4eec93681221723a686ad580c2e7d960e1017cf1a4e0a263c2573c2c6b0bf5cd"
url: "https://pub.dev"
source: hosted
version: "1.3.25"
async:
dependency: transitive
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
url: "https://pub.dev"
source: hosted
version: "2.11.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
characters:
dependency: transitive
description:
name: characters
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
clock:
dependency: transitive
description:
name: clock
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
url: "https://pub.dev"
source: hosted
version: "1.1.1"
cloud_firestore:
dependency: "direct main"
description:
name: cloud_firestore
sha256: "31cfa4d65d6e9ea837234fffe121304034c30c9214c06207b4a35867e3757900"
url: "https://pub.dev"
source: hosted
version: "4.15.8"
cloud_firestore_platform_interface:
dependency: transitive
description:
name: cloud_firestore_platform_interface
sha256: a0097a26569b015faf8142e159e855241609ea9a1738b5fd1c40bfe8411b41a0
url: "https://pub.dev"
source: hosted
version: "6.1.9"
cloud_firestore_web:
dependency: transitive
description:
name: cloud_firestore_web
sha256: ed680ece29a5750985119c09cdc276b460c3a2fa80e8c12f9b7241f6b4a7ca16
url: "https://pub.dev"
source: hosted
version: "3.10.8"
collection:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.18.0"
equatable:
dependency: "direct main"
description:
name: equatable
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
url: "https://pub.dev"
source: hosted
version: "2.0.5"
fake_async:
dependency: transitive
description:
name: fake_async
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
url: "https://pub.dev"
source: hosted
version: "1.3.1"
firebase_auth:
dependency: "direct main"
description:
name: firebase_auth
sha256: "17b841e1b000c3441b8ffceca88f468e078d0443db9643e77541bdfb7a3fd16b"
url: "https://pub.dev"
source: hosted
version: "4.17.8"
firebase_auth_platform_interface:
dependency: transitive
description:
name: firebase_auth_platform_interface
sha256: f294ceef40409a36c819a14280ca864fe487b44033e5276443377c66cb448310
url: "https://pub.dev"
source: hosted
version: "7.1.8"
firebase_auth_web:
dependency: transitive
description:
name: firebase_auth_web
sha256: "1f231da900fe7ff9f2974f8adcbdb3363c410c24725978afa5dc33e1e7e62e06"
url: "https://pub.dev"
source: hosted
version: "5.9.8"
firebase_core:
dependency: "direct main"
description:
name: firebase_core
sha256: "53316975310c8af75a96e365f9fccb67d1c544ef0acdbf0d88bbe30eedd1c4f9"
url: "https://pub.dev"
source: hosted
version: "2.27.0"
firebase_core_platform_interface:
dependency: transitive
description:
name: firebase_core_platform_interface
sha256: c437ae5d17e6b5cc7981cf6fd458a5db4d12979905f9aafd1fea930428a9fe63
url: "https://pub.dev"
source: hosted
version: "5.0.0"
firebase_core_web:
dependency: transitive
description:
name: firebase_core_web
sha256: c8e1d59385eee98de63c92f961d2a7062c5d9a65e7f45bdc7f1b0b205aab2492
url: "https://pub.dev"
source: hosted
version: "2.11.5"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_lints:
dependency: "direct dev"
description:
name: flutter_lints
sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04
url: "https://pub.dev"
source: hosted
version: "2.0.3"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
url: "https://pub.dev"
source: hosted
version: "4.0.2"
js:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
source: hosted
version: "0.6.7"
lints:
dependency: transitive
description:
name: lints
sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
matcher:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
url: "https://pub.dev"
source: hosted
version: "0.12.16"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
url: "https://pub.dev"
source: hosted
version: "1.10.0"
path:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_span:
dependency: transitive
description:
name: source_span
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.10.0"
stack_trace:
dependency: transitive
description:
name: stack_trace
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
url: "https://pub.dev"
source: hosted
version: "1.11.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
url: "https://pub.dev"
source: hosted
version: "2.1.2"
string_scanner:
dependency: transitive
description:
name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
url: "https://pub.dev"
source: hosted
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
url: "https://pub.dev"
source: hosted
version: "1.3.2"
vector_math:
dependency: transitive
description:
name: vector_math
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.3.0"
sdks:
dart: ">=3.2.0 <4.0.0"
flutter: ">=3.3.0"
+26
View File
@@ -0,0 +1,26 @@
name: score_repository
description: Dart package for the score repository.
publish_to: 'none'
version: 1.0.11+11
environment:
sdk: ">=2.19.3 <3.0.0"
dependencies:
flutter:
sdk: flutter
equatable: ^2.0.5
# Firebase
cloud_firestore: ^4.15.4
firebase_core: ^2.25.4
firebase_auth: ^4.17.4
dev_dependencies:
flutter_lints: ^2.0.0
flutter_test:
sdk: flutter
flutter:
uses-material-design: true