fartstack/backend/lib/service/db_access.dart

53 lines
1.8 KiB
Dart

import 'package:backend/database.dart';
import 'package:drift/drift.dart';
import 'package:logging/logging.dart';
import 'package:uuid/uuid.dart';
final log = Logger('Db');
class Db {
static final _db = AppDatabase();
static Future<User?> getUserById(String uuid) {
log.finer('Getting user $uuid');
return _db.managers.users.filter((f) => f.uuid.equals(uuid)).getSingleOrNull();
}
static Future<User?> createUser({required String username, required String roomCode}) async {
final room = await _db.managers.gameRooms
.filter((f) => f.code.equals(roomCode) & f.status.isIn([GameStatus.open, GameStatus.running]))
.getSingleOrNull()
.catchError((Object err) {
log.info('Failed to find available room:$roomCode', err, StackTrace.current);
return null;
});
if (room == null) return null;
return _db.managers.users
.createReturningOrNull(
(o) => o(createdAt: Value(DateTime.now()), uuid: const Uuid().v4(), name: username, gameRoomUuid: room.uuid),
)
.catchError((Object err) {
log.severe('Failed to create user', err, StackTrace.current);
throw Exception(err.toString());
});
}
static Future<GameRoom?> createRoom({required String roomCode}) => _db.managers.gameRooms
.createReturningOrNull(
(o) => o(createdAt: Value(DateTime.now()), status: GameStatus.open, uuid: const Uuid().v4(), code: roomCode),
)
.catchError(
(Object err) {
log.severe('Failed to create room', err, StackTrace.current);
return null;
},
);
static Future<GameRoom?> getRoomByCode(String? roomCode) async {
final room = await _db.managers.gameRooms
.filter((f) => f.code.equals(roomCode) & f.status.isIn([GameStatus.open, GameStatus.running]))
.getSingleOrNull();
return room;
}
}