格式化代码
Some checks failed
Dart CI / build (push) Failing after 11s
Docker Build / docker (push) Successful in 9m6s

This commit is contained in:
2026-04-04 13:36:58 +08:00
parent 22cab82727
commit 120278aa85
11 changed files with 3488 additions and 223 deletions

View File

@@ -22,29 +22,63 @@ Response jsonResponse(dynamic data) {
);
}
/// 创建 API v1 版本的路由
Router createApiV1Router() {
final router = Router()
..get('/', _rootHandler)
..get("/album", _listAlbumHandler)
..get("/album/<id>", _getAlbumHandler)
..get("/album/<id>/photo", _getPhotosOfAlbumHandler)
..get('/photo/<id>', _getPhotoHandler)
..get('/photo/<id>/file', _getPhotoFileHandler)
..get('/photo/<id>/preview', _getPhotoPreviewHandler)
..get('/echo/<message>', _echoHandler);
/// 路由依赖注入容器
///
/// 允许在测试中使用 mock 或临时目录,而不是全局单例。
class AppRouter {
final AlbumRepository albumRepository;
final PhotoRepository photoRepository;
final Vips vips;
return router;
AppRouter({
AlbumRepository? albumRepository,
PhotoRepository? photoRepository,
Vips? vips,
}) : albumRepository =
albumRepository ?? AlbumRepository(basePath: _config.dataDir),
photoRepository = photoRepository ?? PhotoRepository(_config.dataDir),
vips = vips ?? Vips(cacheDir: _config.cacheDir);
/// 创建 API v1 版本的路由
Router createApiV1Router() {
final router = Router()
..get('/', _rootHandler)
..get('/album', (req) => _listAlbumHandler(req, albumRepository))
..get('/album/<id>', (req) => _getAlbumHandler(req, albumRepository))
..get(
'/album/<id>/photo',
(req) => _getPhotosOfAlbumHandler(req, photoRepository),
)
..get('/photo/<id>', (req) => _getPhotoHandler(req, photoRepository))
..get(
'/photo/<id>/file',
(req) => _getPhotoFileHandler(req, photoRepository),
)
..get(
'/photo/<id>/preview',
(req) => _getPhotoPreviewHandler(req, photoRepository, vips),
)
..get('/echo/<message>', _echoHandler);
return router;
}
/// 创建主路由器,将 API v1 挂载到 /api/v1 路径下
Router createRouter() {
final router = Router();
router.mount('/api/v1', createApiV1Router().call);
return router;
}
}
/// 创建主路由器,将 API v1 挂载到 /api/v1 路径下
/// 创建 API v1 版本的路由(使用全局单例,保持向后兼容)
Router createApiV1Router() {
return AppRouter().createApiV1Router();
}
/// 创建主路由器,将 API v1 挂载到 /api/v1 路径下(使用全局单例,保持向后兼容)
Router createRouter() {
final router = Router();
// 将 API v1 路由挂载到 /api/v1 路径下
router.mount('/api/v1', createApiV1Router().call);
return router;
return AppRouter().createRouter();
}
Response _rootHandler(Request req) {
@@ -56,8 +90,8 @@ Response _echoHandler(Request request) {
return Response.ok('$message\n');
}
Future<Response> _listAlbumHandler(Request req) async {
final albums = await albumRepository.getAllAlbums();
Future<Response> _listAlbumHandler(Request req, AlbumRepository repo) async {
final albums = await repo.getAllAlbums();
final albumList = albums
.map(
(a) => {
@@ -71,7 +105,7 @@ Future<Response> _listAlbumHandler(Request req) async {
return jsonResponse(albumList);
}
Future<Response> _getAlbumHandler(Request req) async {
Future<Response> _getAlbumHandler(Request req, AlbumRepository repo) async {
final idParam = req.params['id'];
if (idParam == null) {
return Response.badRequest(body: 'Missing album id');
@@ -80,14 +114,14 @@ Future<Response> _getAlbumHandler(Request req) async {
if (id == null) {
return Response.badRequest(body: 'Invalid album id: $idParam');
}
final album = await albumRepository.getAlbumById(id);
final album = await repo.getAlbumById(id);
if (album == null) {
return Response.notFound('Album not found');
}
return jsonResponse({'id': album.id, 'name': album.name});
}
Future<Response> _getPhotoHandler(Request req) async {
Future<Response> _getPhotoHandler(Request req, PhotoRepository repo) async {
final idParam = req.params['id'];
if (idParam == null) {
return Response.badRequest(body: 'Missing photo id');
@@ -97,14 +131,14 @@ Future<Response> _getPhotoHandler(Request req) async {
return Response.badRequest(body: 'Invalid photo id: $idParam');
}
final photo = await photoRepository.getPhotoById(id);
final photo = await repo.getPhotoById(id);
if (photo == null) {
return Response.notFound('Photo not found');
}
return jsonResponse(photo);
}
Future<Response> _getPhotoFileHandler(Request req) async {
Future<Response> _getPhotoFileHandler(Request req, PhotoRepository repo) async {
final idParam = req.params['id'];
if (idParam == null) {
return Response.badRequest(body: 'Missing photo id');
@@ -114,7 +148,7 @@ Future<Response> _getPhotoFileHandler(Request req) async {
return Response.badRequest(body: 'Invalid photo id: $idParam');
}
final photo = await photoRepository.getPhotoById(id);
final photo = await repo.getPhotoById(id);
if (photo == null) {
return Response.notFound('Photo not found');
}
@@ -153,7 +187,10 @@ Future<Response> _getPhotoFileHandler(Request req) async {
);
}
Future<Response> _getPhotosOfAlbumHandler(Request req) async {
Future<Response> _getPhotosOfAlbumHandler(
Request req,
PhotoRepository repo,
) async {
final idParam = req.params['id'];
if (idParam == null) {
return Response.badRequest(body: 'Missing album id');
@@ -162,11 +199,15 @@ Future<Response> _getPhotosOfAlbumHandler(Request req) async {
if (albumId == null) {
return Response.badRequest(body: 'Invalid album id: $idParam');
}
final photos = await photoRepository.getPhotosByAlbumId(albumId);
final photos = await repo.getPhotosByAlbumId(albumId);
return jsonResponse(photos);
}
Future<Response> _getPhotoPreviewHandler(Request req) async {
Future<Response> _getPhotoPreviewHandler(
Request req,
PhotoRepository repo,
Vips vips,
) async {
final idParam = req.params['id'];
if (idParam == null) {
return Response.badRequest(body: 'Missing photo id');
@@ -176,7 +217,7 @@ Future<Response> _getPhotoPreviewHandler(Request req) async {
return Response.badRequest(body: 'Invalid photo id: $idParam');
}
final photo = await photoRepository.getPhotoById(id);
final photo = await repo.getPhotoById(id);
if (photo == null) {
return Response.notFound('Photo not found');
}