83 lines
1.9 KiB
Dart
83 lines
1.9 KiB
Dart
import 'package:please_pay_me/data/models/json.dart';
|
|
|
|
class Expense {
|
|
const Expense({
|
|
required this.id,
|
|
required this.budgetId,
|
|
required this.amount,
|
|
required this.spentAt,
|
|
this.note,
|
|
});
|
|
|
|
factory Expense.fromJson(Map<String, dynamic> json) {
|
|
return Expense(
|
|
id: asInt(json['id']),
|
|
budgetId: asInt(json['budget_id']),
|
|
amount: asDouble(json['amount']),
|
|
spentAt: asDate(json['spent_at']),
|
|
note: asStringOrNull(json['note']),
|
|
);
|
|
}
|
|
|
|
final int id;
|
|
final int budgetId;
|
|
final double amount;
|
|
final DateTime spentAt;
|
|
final String? note;
|
|
}
|
|
|
|
class ExpensesPage {
|
|
const ExpensesPage({
|
|
required this.page,
|
|
required this.totalPages,
|
|
required this.totalCount,
|
|
required this.pageSize,
|
|
required this.totalSum,
|
|
required this.items,
|
|
this.budgetId,
|
|
});
|
|
|
|
factory ExpensesPage.fromJson(Map<String, dynamic> json) {
|
|
return ExpensesPage(
|
|
page: asInt(json['page'], fallback: 1),
|
|
totalPages: asInt(json['total_pages'], fallback: 1),
|
|
totalCount: asInt(json['total_count']),
|
|
pageSize: asInt(json['page_size'], fallback: 20),
|
|
totalSum: asDouble(json['total_sum']),
|
|
budgetId: json['budget_id'] == null ? null : asInt(json['budget_id']),
|
|
items: asList(json['items']).map(Expense.fromJson).toList(),
|
|
);
|
|
}
|
|
|
|
static const empty = ExpensesPage(
|
|
page: 1,
|
|
totalPages: 1,
|
|
totalCount: 0,
|
|
pageSize: 20,
|
|
totalSum: 0,
|
|
items: [],
|
|
);
|
|
|
|
final int page;
|
|
final int totalPages;
|
|
final int totalCount;
|
|
final int pageSize;
|
|
final double totalSum;
|
|
final int? budgetId;
|
|
final List<Expense> items;
|
|
|
|
bool get hasMore => page < totalPages;
|
|
|
|
ExpensesPage copyWithItems(List<Expense> items, {int? page}) {
|
|
return ExpensesPage(
|
|
page: page ?? this.page,
|
|
totalPages: totalPages,
|
|
totalCount: totalCount,
|
|
pageSize: pageSize,
|
|
totalSum: totalSum,
|
|
budgetId: budgetId,
|
|
items: items,
|
|
);
|
|
}
|
|
}
|