Files
loongyan/bin/domain/repositories/album_repository.dart
2026-04-04 14:24:18 +08:00

41 lines
902 B
Dart

import 'dart:io';
import 'package:path/path.dart' as p;
import '../entities/album.dart';
class AlbumRepository {
final String basePath;
AlbumRepository({required this.basePath});
Future<List<Album>> getAllAlbums() async {
final dir = Directory(basePath);
if (!await dir.exists()) {
return [];
}
var albums = await dir
.list()
.where((d) => d is Directory)
.map((d) => d as Directory)
.map((dir) {
final name = p.basename(dir.path);
return Album(
id: dir.path.hashCode,
name: name,
createdAt: dir.statSync().changed,
updatedAt: dir.statSync().changed,
);
})
.toList();
return albums;
}
Future<Album?> getAlbumById(int id) async {
final albums = await getAllAlbums();
return albums.where((a) => a.id == id).firstOrNull;
}
}