2025-06-26 19:20:39 +05:30
|
|
|
import 'package:drift/drift.dart';
|
|
|
|
|
import 'package:immich_mobile/domain/models/album/album.model.dart';
|
|
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_album.entity.drift.dart';
|
2025-07-10 11:59:15 -05:00
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_album_asset.entity.drift.dart';
|
2025-06-26 19:20:39 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
|
|
|
|
|
2025-07-10 11:59:15 -05:00
|
|
|
enum SortRemoteAlbumsBy { id, updatedAt }
|
2025-06-26 19:20:39 +05:30
|
|
|
|
|
|
|
|
class DriftRemoteAlbumRepository extends DriftDatabaseRepository {
|
|
|
|
|
final Drift _db;
|
|
|
|
|
const DriftRemoteAlbumRepository(this._db) : super(_db);
|
|
|
|
|
|
2025-07-10 10:13:46 -05:00
|
|
|
Future<List<RemoteAlbum>> getAll({
|
2025-07-10 11:59:15 -05:00
|
|
|
Set<SortRemoteAlbumsBy> sortBy = const {SortRemoteAlbumsBy.updatedAt},
|
2025-07-10 10:13:46 -05:00
|
|
|
}) {
|
2025-06-30 21:24:50 -05:00
|
|
|
final assetCount = _db.remoteAlbumAssetEntity.assetId.count();
|
|
|
|
|
|
|
|
|
|
final query = _db.remoteAlbumEntity.select().join([
|
|
|
|
|
leftOuterJoin(
|
|
|
|
|
_db.remoteAlbumAssetEntity,
|
|
|
|
|
_db.remoteAlbumAssetEntity.albumId.equalsExp(_db.remoteAlbumEntity.id),
|
|
|
|
|
useColumns: false,
|
|
|
|
|
),
|
2025-07-08 21:54:29 +08:00
|
|
|
leftOuterJoin(
|
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
|
_db.remoteAssetEntity.id.equalsExp(_db.remoteAlbumAssetEntity.assetId),
|
|
|
|
|
useColumns: false,
|
|
|
|
|
),
|
2025-06-30 21:24:50 -05:00
|
|
|
leftOuterJoin(
|
|
|
|
|
_db.userEntity,
|
|
|
|
|
_db.userEntity.id.equalsExp(_db.remoteAlbumEntity.ownerId),
|
2025-07-08 21:54:29 +08:00
|
|
|
useColumns: false,
|
2025-06-30 21:24:50 -05:00
|
|
|
),
|
|
|
|
|
]);
|
|
|
|
|
query
|
2025-07-08 21:54:29 +08:00
|
|
|
..where(_db.remoteAssetEntity.deletedAt.isNull())
|
2025-06-30 21:24:50 -05:00
|
|
|
..addColumns([assetCount])
|
2025-07-08 21:54:29 +08:00
|
|
|
..addColumns([_db.userEntity.name])
|
2025-06-30 21:24:50 -05:00
|
|
|
..groupBy([_db.remoteAlbumEntity.id]);
|
2025-06-26 19:20:39 +05:30
|
|
|
|
|
|
|
|
if (sortBy.isNotEmpty) {
|
2025-06-30 21:24:50 -05:00
|
|
|
final orderings = <OrderingTerm>[];
|
2025-06-26 19:20:39 +05:30
|
|
|
for (final sort in sortBy) {
|
|
|
|
|
orderings.add(
|
|
|
|
|
switch (sort) {
|
2025-06-30 21:24:50 -05:00
|
|
|
SortRemoteAlbumsBy.id => OrderingTerm.asc(_db.remoteAlbumEntity.id),
|
2025-07-10 11:59:15 -05:00
|
|
|
SortRemoteAlbumsBy.updatedAt =>
|
|
|
|
|
OrderingTerm.desc(_db.remoteAlbumEntity.updatedAt),
|
2025-06-26 19:20:39 +05:30
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
query.orderBy(orderings);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 21:24:50 -05:00
|
|
|
return query
|
|
|
|
|
.map(
|
|
|
|
|
(row) => row.readTable(_db.remoteAlbumEntity).toDto(
|
|
|
|
|
assetCount: row.read(assetCount) ?? 0,
|
2025-07-08 21:54:29 +08:00
|
|
|
ownerName: row.read(_db.userEntity.name)!,
|
2025-06-30 21:24:50 -05:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.get();
|
2025-06-26 19:20:39 +05:30
|
|
|
}
|
2025-07-10 11:59:15 -05:00
|
|
|
|
|
|
|
|
Future<void> create(
|
|
|
|
|
RemoteAlbum album,
|
|
|
|
|
List<String> assetIds,
|
|
|
|
|
) async {
|
|
|
|
|
await _db.transaction(() async {
|
|
|
|
|
final entity = RemoteAlbumEntityCompanion(
|
|
|
|
|
id: Value(album.id),
|
|
|
|
|
name: Value(album.name),
|
|
|
|
|
ownerId: Value(album.ownerId),
|
|
|
|
|
createdAt: Value(album.createdAt),
|
|
|
|
|
updatedAt: Value(album.updatedAt),
|
|
|
|
|
description: Value(album.description),
|
|
|
|
|
thumbnailAssetId: Value(album.thumbnailAssetId),
|
|
|
|
|
isActivityEnabled: Value(album.isActivityEnabled),
|
|
|
|
|
order: Value(album.order),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await _db.remoteAlbumEntity.insertOne(entity);
|
|
|
|
|
|
|
|
|
|
if (assetIds.isNotEmpty) {
|
|
|
|
|
final albumAssets = assetIds.map(
|
|
|
|
|
(assetId) => RemoteAlbumAssetEntityCompanion(
|
|
|
|
|
albumId: Value(album.id),
|
|
|
|
|
assetId: Value(assetId),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await _db.batch((batch) {
|
|
|
|
|
batch.insertAll(
|
|
|
|
|
_db.remoteAlbumAssetEntity,
|
|
|
|
|
albumAssets,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-06-26 19:20:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension on RemoteAlbumEntityData {
|
2025-07-10 10:13:46 -05:00
|
|
|
RemoteAlbum toDto({int assetCount = 0, required String ownerName}) {
|
|
|
|
|
return RemoteAlbum(
|
2025-06-26 19:20:39 +05:30
|
|
|
id: id,
|
|
|
|
|
name: name,
|
|
|
|
|
ownerId: ownerId,
|
|
|
|
|
createdAt: createdAt,
|
|
|
|
|
updatedAt: updatedAt,
|
|
|
|
|
description: description,
|
|
|
|
|
thumbnailAssetId: thumbnailAssetId,
|
|
|
|
|
isActivityEnabled: isActivityEnabled,
|
|
|
|
|
order: order,
|
2025-06-30 21:24:50 -05:00
|
|
|
assetCount: assetCount,
|
|
|
|
|
ownerName: ownerName,
|
2025-06-26 19:20:39 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|