初始化项目
This commit is contained in:
41
bin/domain/repositories/album_repository.dart
Normal file
41
bin/domain/repositories/album_repository.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
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: name.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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user