139 lines
3.6 KiB
Dart
139 lines
3.6 KiB
Dart
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/<id>", _getAlbumHandler)
|
|
..get("/album/<id>/photo", _getPhotosOfAlbumHander)
|
|
..get('/photo/<id>', _getPhotoHandler)
|
|
..get('/photo/<id>/file', _getPhotoFileHandler)
|
|
..get('/echo/<message>', _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<Response> _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<Response> _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<Response> _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<Response> _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<Response> _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);
|
|
}
|