From 60f9840ce7b4f58cb5aa5253b2553a9a1d213a51 Mon Sep 17 00:00:00 2001 From: Chris Daley Date: Tue, 19 Nov 2024 22:08:25 -0800 Subject: [PATCH] more cr suggestions --- lib/data/models/nostr_event.dart | 2 +- lib/data/models/payment_request.dart | 25 ++++++---- lib/data/models/rating.dart | 73 ++++++++++++++++++++++++++++ lib/data/rating.dart | 34 ------------- 4 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 lib/data/models/rating.dart delete mode 100644 lib/data/rating.dart diff --git a/lib/data/models/nostr_event.dart b/lib/data/models/nostr_event.dart index 9e3d05cb..5ca0d78f 100644 --- a/lib/data/models/nostr_event.dart +++ b/lib/data/models/nostr_event.dart @@ -1,6 +1,6 @@ import 'dart:convert'; import 'package:mostro_mobile/data/models/enums/order_type.dart'; -import 'package:mostro_mobile/data/rating.dart'; +import 'package:mostro_mobile/data/models/rating.dart'; import 'package:timeago/timeago.dart' as timeago; import 'package:dart_nostr/dart_nostr.dart'; import 'package:mostro_mobile/data/models/order.dart'; diff --git a/lib/data/models/payment_request.dart b/lib/data/models/payment_request.dart index 070ab9cf..df39bb68 100644 --- a/lib/data/models/payment_request.dart +++ b/lib/data/models/payment_request.dart @@ -16,31 +16,36 @@ class PaymentRequest implements Content { throw ArgumentError('At least one parameter must be provided'); } } - + @override Map toJson() { final typeKey = type; final List values = []; - + values.add(order?.toJson()); values.add(lnInvoice); - + if (amount != null) { values.add(amount); } - - final result = { - typeKey: values - }; + + final result = {typeKey: values}; return result; } factory PaymentRequest.fromJson(List json) { + if (json.length < 2) { + throw FormatException('Invalid JSON format: insufficient elements'); + } + final orderJson = json[0]; + final Order? order = orderJson != null ? Order.fromJson(orderJson) : null; + final amount = json.length > 2 ? json[2] as int? : null; return PaymentRequest( - order: json[0] ?? Order.fromJson(json[0]), - lnInvoice: json[1], - amount: json[2]); + order: order, + lnInvoice: json[1], + amount: amount, + ); } @override diff --git a/lib/data/models/rating.dart b/lib/data/models/rating.dart new file mode 100644 index 00000000..8d43a972 --- /dev/null +++ b/lib/data/models/rating.dart @@ -0,0 +1,73 @@ +import 'dart:convert'; + +class Rating { + final int totalReviews; + final double totalRating; + final int lastRating; + final int maxRate; + final int minRate; + + Rating( + {required this.totalReviews, + required this.totalRating, + required this.lastRating, + required this.maxRate, + required this.minRate}); + + factory Rating.deserialized(String data) { + if (data.isEmpty) { + throw FormatException('Empty data string provided'); + } + if (data == 'none') { + return Rating( + totalReviews: 0, + totalRating: 0.0, + lastRating: 0, + maxRate: 0, + minRate: 0); + } + try { + final json = jsonDecode(data) as Map; + final rating = json['rating'] as Map?; + + if (rating == null) { + throw FormatException('Missing rating object in JSON'); + } + + return Rating( + totalReviews: _parseIntField(rating, 'total_reviews'), + totalRating: _parseDoubleField(rating, 'total_rating'), + lastRating: _parseIntField(rating, 'last_rating'), + maxRate: _parseIntField(rating, 'max_rate'), + minRate: _parseIntField(rating, 'min_rate'), + ); + } on FormatException { + rethrow; + } catch (e) { + throw FormatException('Failed to parse rating data: $e'); + } + } + static int _parseIntField(Map json, String field) { + final value = json[field]; + if (value == null) { + throw FormatException('Missing required field: $field'); + } + if (value is! num) { + throw FormatException( + 'Invalid type for $field: expected number, got ${value.runtimeType}'); + } + return value.toInt(); + } + + static double _parseDoubleField(Map json, String field) { + final value = json[field]; + if (value == null) { + throw FormatException('Missing required field: $field'); + } + if (value is! num) { + throw FormatException( + 'Invalid type for $field: expected number, got ${value.runtimeType}'); + } + return value.toDouble(); + } +} diff --git a/lib/data/rating.dart b/lib/data/rating.dart deleted file mode 100644 index 96f7ea3a..00000000 --- a/lib/data/rating.dart +++ /dev/null @@ -1,34 +0,0 @@ -import 'dart:convert'; - -class Rating { - final int totalReviews; - final double totalRating; - final int lastRating; - final int maxRate; - final int minRate; - - Rating( - {required this.totalReviews, - required this.totalRating, - required this.lastRating, - required this.maxRate, - required this.minRate}); - - factory Rating.deserialized(String data) { - if (data == 'none') { - return Rating( - totalReviews: 0, - totalRating: 0.0, - lastRating: 0, - maxRate: 0, - minRate: 0); - } - final json = jsonDecode(data) as Map; - return Rating( - totalReviews: json['rating']['total_reviews'], - totalRating: json['rating']['total_rating'], - lastRating: json['rating']['last_rating'], - maxRate: json['rating']['max_rate'], - minRate: json['rating']['min_rate']); - } -}