apply static method

Future<void> apply({
  1. required UAdminMediaDraft draft,
  2. required List<UMediaResponse> existing,
  3. String? hotelId,
  4. String? hotelRoomId,
  5. String? dormId,
  6. String? dormRoomId,
  7. String? dormBedId,
})

Implementation

static Future<void> apply({
  required UAdminMediaDraft draft,
  required List<UMediaResponse> existing,
  String? hotelId,
  String? hotelRoomId,
  String? dormId,
  String? dormRoomId,
  String? dormBedId,
}) async {
  if (draft.isEmpty) return;

  for (final String id in draft.deletedIds) {
    await UServices.media.delete(
      p: UIdParams(id: id),
      onOk: (_) {},
      onError: (_) {},
      onException: (_) {},
    );
  }

  final List<String?> uploadedIds = <String?>[];
  for (int i = 0; i < draft.newFiles.length; i++) {
    final int? category = draft.newFileCategories[i];
    final (UResponse<String>? ok, UEmptyResponse? _, String? _) = await UServices.media.create(
      p: UMediaCreateParams(
        file: draft.newFiles[i],
        tag1: TagMedia.image.number,
        tag2: category,
        tag3: draft.coverNewIndex == i ? TagMedia.cover.number : null,
        hotelId: hotelId,
        hotelRoomId: hotelRoomId,
        dormId: dormId,
        dormRoomId: dormRoomId,
        dormBedId: dormBedId,
      ),
      onOk: (_) {},
      onError: (_) {},
      onException: (_) {},
    );
    uploadedIds.add(ok?.result);
  }

  // The cover mark lives on exactly one photo.
  final String? newCoverId = draft.coverExistingId ?? (draft.coverNewIndex == null ? null : uploadedIds[draft.coverNewIndex!]);
  if (newCoverId == null) return;
  for (final UMediaResponse m in existing.where((UMediaResponse m) => m.tags.contains(TagMedia.cover.number) && m.id != newCoverId && !draft.deletedIds.contains(m.id))) {
    await UServices.media.update(
      p: UMediaUpdateParams(id: m.id, removeTags: <int>[TagMedia.cover.number]),
      onOk: (_) {},
      onError: (_) {},
      onException: (_) {},
    );
  }
  if (draft.coverExistingId != null) {
    await UServices.media.update(
      p: UMediaUpdateParams(id: newCoverId, addTags: <int>[TagMedia.cover.number]),
      onOk: (_) {},
      onError: (_) {},
      onException: (_) {},
    );
  }
}