advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

bingo.controller.ts (1528B)


      1 import { Service, Inject } from 'typedi';
      2 import { Request, Response } from 'express';
      3 
      4 import { Controller, Get } from '../interface/rest';
      5 import { BingoService } from './bingo.service';
      6 
      7 @Controller('bingo')
      8 @Service()
      9 export class BingoController {
     10 
     11   constructor(
     12     private bingoService: BingoService
     13   ) {}
     14 
     15   // List game uuids
     16   @Get('games/:uuid/boards')
     17   async getGameBoards({ res, uuid }: { res: Response, uuid: string }) {
     18     const found = await this.bingoService.getGameBoards(uuid);
     19     if (!found) return res.end(JSON.stringify({
     20       ok    : false,
     21       error : 'not-found',
     22     }));
     23     res.end(JSON.stringify({
     24       ok   : true,
     25       data : found.map(board => ({
     26         uuid   : board.uuid,
     27         values : board.values,
     28       }))
     29     }, null, 2));
     30   }
     31 
     32   // Fetch a single game
     33   @Get('games/:uuid')
     34   async getGame({ res, uuid }: { res: Response, uuid: string }) {
     35     const found = await this.bingoService.get(uuid);
     36     if (!found) return res.end(JSON.stringify({
     37       ok    : false,
     38       error : 'not-found',
     39     }));
     40     res.end(JSON.stringify({
     41       ok   : true,
     42       data : {
     43         uuid  : found.uuid,
     44         name  : found.name,
     45         drawn : found.drawn,
     46       },
     47     }, null, 2));
     48   }
     49 
     50   // List game uuids
     51   @Get('games')
     52   async listGames({ res }: { res: Response }) {
     53     const found = await this.bingoService.all();
     54     res.end(JSON.stringify({
     55       ok   : true,
     56       data : found.map(game => ({
     57         uuid : game.uuid,
     58         name : game.name,
     59       })),
     60     }));
     61   }
     62 
     63 }