Skip to content

Commit

Permalink
more cr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
chebizarro committed Nov 20, 2024
1 parent 09f44ee commit 60f9840
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/data/models/nostr_event.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
25 changes: 15 additions & 10 deletions lib/data/models/payment_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@ class PaymentRequest implements Content {
throw ArgumentError('At least one parameter must be provided');
}
}

@override
Map<String, dynamic> toJson() {
final typeKey = type;
final List<dynamic> 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<dynamic> 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
Expand Down
73 changes: 73 additions & 0 deletions lib/data/models/rating.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic>;
final rating = json['rating'] as Map<String, dynamic>?;

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<String, dynamic> 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<String, dynamic> 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();
}
}
34 changes: 0 additions & 34 deletions lib/data/rating.dart

This file was deleted.

0 comments on commit 60f9840

Please sign in to comment.