bingo.service.ts (854B)
1 import { Service, Inject } from 'typedi'; 2 import { Board } from './model/board'; 3 import { Game } from './model/game'; 4 5 @Service() 6 export class BingoService { 7 8 constructor( 9 @Inject('db') private db 10 ) {} 11 12 async all(): Promise<Game[]> { 13 return this.db.game; 14 } 15 16 async get(game: string|Partial<Game>): Promise<Game> { 17 if ('string' === typeof game) return this.db.game.find(g => g.uuid === game); 18 if ('uuid' in game) return this.db.game.find(g => g.uuid === game.uuid); 19 throw new Error("Could not fetch game: invalid identifier"); 20 } 21 22 async getGameBoards(game: string|Partial<Game>): Promise<Board[]> { 23 const found = await this.get(game); 24 if (!found) return []; 25 return found.board.map(board => { 26 if (board instanceof Board) return board; 27 return this.db.board.find(g => g.uuid === game); 28 });; 29 } 30 31 }