From 06ae9f3b45785419d1a1ebc9482b9ff4d011e69c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20Syd=C3=A4nmaa?= <L4072@student.jamk.fi>
Date: Tue, 9 Jul 2019 13:55:50 +0300
Subject: [PATCH] Can list games according to one or more statuses

---
 src/game/game.controller.ts |  9 +++++++--
 src/game/game.dto.ts        |  2 +-
 src/game/game.service.ts    | 30 +++++++++++++++++++++++++-----
 3 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts
index 8d7494b..a0b85a2 100644
--- a/src/game/game.controller.ts
+++ b/src/game/game.controller.ts
@@ -55,8 +55,13 @@ export class GameController {
   }
 
   @Get('listgames')
-  async listGames() {
-    return this.gameservice.listGames();
+  async listGames(state) {
+    return this.gameservice.listGames(state);
+  }
+
+  @Get('listgames/:state')
+  async listGamesState(@Param('state') state: string) {
+    return this.gameservice.listGames(state);
   }
 
   // ClassSerializerInterceptor removes excluded columns set in Entities
diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts
index 266a8b1..abf1742 100644
--- a/src/game/game.dto.ts
+++ b/src/game/game.dto.ts
@@ -76,7 +76,7 @@ export class newGameDTO {
 export class GameStateDTO {
   @IsUUID('4')
   id: string;
-  @IsIn(['CREATED', 'STARTED', 'PAUSED', 'ENDED'])
+  @IsIn(['CREATED', 'STARTED', 'PAUSED', 'ENDED', 'ONGOING'])
   state: string;
 }
 
diff --git a/src/game/game.service.ts b/src/game/game.service.ts
index 5027c6e..475027c 100644
--- a/src/game/game.service.ts
+++ b/src/game/game.service.ts
@@ -174,11 +174,31 @@ export class GameService {
   }
 
   // returns name and id of each game
-  async listGames() {
-    const games = await this.gameRepository.find();
-    return games.map(game => {
-      return game.gameObject();
-    });
+  async listGames(state) {
+    if(state == null){
+      const games = await this.gameRepository.find();
+      return games.map(game => {
+        return game.gameObject();
+      });
+    }
+    else if(state == 'ONGOING'){
+      const games = await this.gameRepository.find({
+        where: [
+          {state: 'CREATED'}, {state: 'STARTED'}, {state: 'PAUSED'},
+        ]
+      });
+      return games.map(game => {
+        return game.gameObject();
+      });
+    }
+    else{
+      const games = await this.gameRepository.find({
+        where: {state: state}
+      });
+      return games.map(game => {
+        return game.gameObject();
+      });
+    }
   }
 
   // returns information about a game identified by id
-- 
GitLab