Files
oott/frontend/lib/utils/api/oott_api_devices.dart
T
rzuastiandClaude Opus 4.8 844d7d189c Add "go to last page" and page count to notifications and devices lists
The list endpoints now return a total count alongside the page so the
front-end can show how many pages exist and offer a last-page jump.

Backend: add count(is_new) and count_devices(...) (sharing a WHERE-builder
with list_devices so page and count can't drift), wrap both list responses
in {items, total_count} structs, and register them with utoipa.

Front-end: parse the wrapper shape (dropping the fetch-one-extra trick),
add a Last-page button and a responsive "Page X of Y" / "X / Y" label to
the shared PaginationBar, and track the total in both lists. Notifications
re-sync the count on every fetch and decrement it locally on mark-read/
unread removals so the count stays accurate without a re-fetch.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 21:09:25 -04:00

108 lines
2.8 KiB
Dart

part of '../oott_api.dart';
/// Device endpoints: lookup, listing, registration, updates and event history.
extension DeviceApi on BackendAPI {
Future<Device> getDevice(String macAddress) =>
_getModel('/devices/$macAddress', Device.fromJson);
Future<({List<Device> items, int totalCount})> listDevices({
bool? isRegistered,
String? owner,
DeviceType? deviceType,
String? sortBy,
bool? sortAscending,
int page = 0,
int perPage = 10,
CancelToken? cancelToken,
}) async {
final params = <String, dynamic>{};
if (isRegistered != null) params['is_registered'] = isRegistered;
if (owner != null && owner.isNotEmpty) params['owner'] = owner;
if (deviceType != null) {
params['device_type'] = deviceType == DeviceType.unknown
? ''
: deviceType.apiName;
}
if (sortBy != null) params['sort_by'] = sortBy;
if (sortAscending != null) {
params['sort_order'] = sortAscending ? 'asc' : 'desc';
}
params['page_offset'] = page * perPage;
params['page_limit'] = perPage;
final response = await _dio.get(
'/devices',
queryParameters: params,
cancelToken: cancelToken,
);
return _paginate(
response.data as Map<String, dynamic>,
(item) => Device.fromJson(item as Map<String, dynamic>),
);
}
Future<void> registerDevice(
String macAddress,
String owner,
String deviceType, {
String? name,
}) async {
await _dio.put(
'/devices',
data: {
'mac_address': macAddress,
'owner': owner,
'device_type': deviceType,
'name': ?name,
},
);
}
Future<void> updateDevice(
String macAddress,
String owner,
String deviceType,
String vendor, {
String? name,
}) async {
await _dio.put(
'/devices/$macAddress',
data: {
'owner': owner,
'device_type': deviceType,
'vendor': vendor,
'name': name,
},
);
}
Future<void> forgetDevice(String macAddress) async {
await _dio.delete('/devices/$macAddress');
}
Future<List<DeviceEvent>> getDeviceEvents(
String macAddress, {
DateTime? createdFrom,
}) async {
final queryParams = <String, dynamic>{};
if (createdFrom != null) {
queryParams['created_from'] = createdFrom.toUtc().toIso8601String();
}
final response = await _dio.get(
'/devices/$macAddress/events',
queryParameters: queryParams.isEmpty ? null : queryParams,
);
return (response.data as List)
.map((item) => DeviceEvent.fromJson(item as Map<String, dynamic>))
.toList();
}
Future<DeviceSummary> getDeviceSummary({CancelToken? cancelToken}) =>
_getModel(
'/devices/summary',
DeviceSummary.fromJson,
cancelToken: cancelToken,
);
}