import 'dart:convert'; import 'dart:io'; import 'dart:async'; import 'package:shelf/shelf.dart'; import 'package:shelf_router/shelf_router.dart'; import 'domain/repositories/photo_repository.dart'; import 'domain/repositories/album_repository.dart'; final albumRepository = AlbumRepository(basePath: 'data'); final photoRepository = PhotoRepository('data'); Response jsonResponse(dynamic data) { return Response.ok( jsonEncode(data), headers: {'content-type': 'application/json'}, ); } Router createRouter() { final router = Router() ..get('/', _rootHandler) ..get("/album", _listAlbumHandler) ..get("/album/", _getAlbumHandler) ..get("/album//photo", _getPhotosOfAlbumHander) ..get('/photo/', _getPhotoHandler) ..get('/photo//file', _getPhotoFileHandler) ..get('/echo/', _echoHandler); return router; } Response _rootHandler(Request req) { return Response.ok('Hello, World!\n'); } Response _echoHandler(Request request) { final message = request.params['message']; return Response.ok('$message\n'); } Future _listAlbumHandler(Request req) async { final albums = await albumRepository.getAllAlbums(); final albumList = albums .map( (a) => { 'id': a.id, 'name': a.name, 'createdAt': a.createdAt.toIso8601String(), 'updatedAt': a.updatedAt.toIso8601String(), }, ) .toList(); return jsonResponse(albumList); } Future _getAlbumHandler(Request req) async { final idParam = req.params['id']; if (idParam == null) { return Response.badRequest(body: 'Missing album id'); } final id = int.tryParse(idParam); if (id == null) { return Response.badRequest(body: 'Invalid album id: $idParam'); } final album = await albumRepository.getAlbumById(id); if (album == null) { return Response.notFound('Album not found'); } return jsonResponse({ 'id': album.id, 'name': album.name, }); } Future _getPhotoHandler(Request req) async { final idParam = req.params['id']; if (idParam == null) { return Response.badRequest(body: 'Missing photo id'); } final id = int.tryParse(idParam); if (id == null) { return Response.badRequest(body: 'Invalid photo id: $idParam'); } final photo = await photoRepository.getPhotoById(id); if (photo == null) { return Response.notFound('Photo not found'); } return jsonResponse(photo); } Future _getPhotoFileHandler(Request req) async { final idParam = req.params['id']; if (idParam == null) { return Response.badRequest(body: 'Missing photo id'); } final id = int.tryParse(idParam); if (id == null) { return Response.badRequest(body: 'Invalid photo id: $idParam'); } final photo = await photoRepository.getPhotoById(id); if (photo == null) { return Response.notFound('Photo not found'); } final filePath = photo.filePath; final file = File(filePath); if (!await file.exists()) { return Response.notFound('File not found'); } return Response.ok( file.openRead(), headers: { 'Content-Type': photo.mimeType, 'Content-Length': photo.fileSize.toString(), }, ); } Future _getPhotosOfAlbumHander(Request req) async { final idParam = req.params['id']; if (idParam == null) { return Response.badRequest(body: 'Missing album id'); } final albumId = int.tryParse(idParam); if (albumId == null) { return Response.badRequest(body: 'Invalid album id: $idParam'); } final photos = await photoRepository.getPhotosByAlbumId(albumId); return jsonResponse(photos); }