Refactor database layer: convert to DatabaseProvider class with initialization
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
import 'package:bcrypt/bcrypt.dart';
|
||||
import '../database/database_provider.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
class AuthRoutes {
|
||||
final DatabaseProvider database;
|
||||
|
||||
AuthRoutes(this.database);
|
||||
|
||||
Router get routes {
|
||||
final router = Router();
|
||||
router.post('/login', _login);
|
||||
router.get('/watch', _watch);
|
||||
return router;
|
||||
}
|
||||
|
||||
Future<Response> _login(Request request) async {
|
||||
final body = await request.readAsString();
|
||||
final data = jsonDecode(body);
|
||||
|
||||
final login = data['login'];
|
||||
final password = data['password'];
|
||||
|
||||
final user = await database.findUserByLogin(login);
|
||||
|
||||
if (user == null || !BCrypt.checkpw(password, user.pwdHash)) {
|
||||
return Response(401, body: 'Invalid credentials');
|
||||
}
|
||||
|
||||
return Response(200, body: jsonEncode({'user': user.toMap()}));
|
||||
}
|
||||
|
||||
Future<Response> _watch(Request request) async {
|
||||
final uniqueId = request.url.queryParameters['unique_id'];
|
||||
|
||||
final userId = database.getUserIdByShareId(uniqueId!);
|
||||
|
||||
if (userId == null) {
|
||||
return Response(404, body: 'Share link not found');
|
||||
}
|
||||
|
||||
final position = await database.getLatestPosition(userId);
|
||||
|
||||
if (position == null) {
|
||||
return Response(404, body: 'No position available');
|
||||
}
|
||||
|
||||
return Response(200, body: position.toJson());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
import '../database/database_provider.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
class GeoRoutes {
|
||||
final DatabaseProvider database;
|
||||
|
||||
GeoRoutes(this.database);
|
||||
|
||||
Router get routes {
|
||||
final router = Router();
|
||||
router.post('/geo', _createPosition);
|
||||
router.put('/geo', _updatePosition);
|
||||
router.post('/share', _createShare);
|
||||
return router;
|
||||
}
|
||||
|
||||
Future<Response> _createPosition(Request request) async {
|
||||
final body = await request.readAsString();
|
||||
final data = jsonDecode(body);
|
||||
|
||||
final userId = data['user_id'];
|
||||
final x = data['x'];
|
||||
final y = data['y'];
|
||||
final lifetimeSeconds = data['lifetime'];
|
||||
final lifetime = Duration(seconds: lifetimeSeconds);
|
||||
|
||||
final position = await database.createPosition(userId, x, y, lifetime);
|
||||
return Response(201, body: position.toJson());
|
||||
}
|
||||
|
||||
Future<Response> _updatePosition(Request request) async {
|
||||
final body = await request.readAsString();
|
||||
final data = jsonDecode(body);
|
||||
|
||||
final userId = data['user_id'];
|
||||
final x = data['x'];
|
||||
final y = data['y'];
|
||||
final lifetimeSeconds = data['lifetime'];
|
||||
final lifetime = Duration(seconds: lifetimeSeconds);
|
||||
|
||||
final position = await database.updatePosition(userId, x, y, lifetime);
|
||||
return Response(200, body: position.toJson());
|
||||
}
|
||||
|
||||
Future<Response> _createShare(Request request) async {
|
||||
final body = await request.readAsString();
|
||||
final data = jsonDecode(body);
|
||||
|
||||
final userId = data['user_id'];
|
||||
final shareId = database.createShareId(userId);
|
||||
|
||||
return Response(200, body: jsonEncode({'share_id': shareId}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
import '../database/database_provider.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
class UserRoutes {
|
||||
final DatabaseProvider database;
|
||||
|
||||
UserRoutes(this.database);
|
||||
|
||||
Router get routes {
|
||||
final router = Router();
|
||||
router.get('/user', _getAllUsers);
|
||||
router.post('/user', _createUser);
|
||||
router.put('/user/<id>', _updateUser);
|
||||
router.delete('/user/<id>', _deleteUser);
|
||||
return router;
|
||||
}
|
||||
|
||||
Future<Response> _getAllUsers(Request request) async {
|
||||
final users = await database.getAllUsers();
|
||||
return Response(200, body: jsonEncode(users.map((u) => u.toMap()).toList()));
|
||||
}
|
||||
|
||||
Future<Response> _createUser(Request request) async {
|
||||
final body = await request.readAsString();
|
||||
final data = jsonDecode(body);
|
||||
|
||||
final login = data['login'];
|
||||
final password = data['password'];
|
||||
|
||||
final user = await database.createUser(login, password);
|
||||
return Response(201, body: jsonEncode(user.toMap()));
|
||||
}
|
||||
|
||||
Future<Response> _updateUser(Request request) async {
|
||||
final id = int.parse(request.params['id']!);
|
||||
|
||||
final body = await request.readAsString();
|
||||
final data = jsonDecode(body);
|
||||
|
||||
final login = data['login'];
|
||||
final password = data['password'];
|
||||
|
||||
final user = await database.updateUser(id, login, password);
|
||||
return Response(200, body: jsonEncode(user.toMap()));
|
||||
}
|
||||
|
||||
Future<Response> _deleteUser(Request request) async {
|
||||
final id = int.parse(request.params['id']!);
|
||||
await database.deleteUser(id);
|
||||
return Response(204);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user