1
+ using Microsoft . AspNetCore . Mvc ;
2
+ using SSHTicTacToe . DTO ;
3
+ using SSHTicTacToe . Services ;
4
+
5
+ namespace SSHTicTacToe . Controllers
6
+ {
7
+ [ ApiController ]
8
+ [ Route ( "api/v1/games" ) ]
9
+ public class TicTacToeGamesController : ControllerBase
10
+ {
11
+ private readonly ILogger < TicTacToeGamesController > _logger ;
12
+ private readonly ITicTacToeGameService _ticTacToeGameService ;
13
+
14
+ public TicTacToeGamesController (
15
+ ILogger < TicTacToeGamesController > logger ,
16
+ ITicTacToeGameService ticTacToeGameService )
17
+ {
18
+ _logger = logger ;
19
+ _ticTacToeGameService = ticTacToeGameService ;
20
+ }
21
+
22
+ [ HttpGet ]
23
+ public async Task < IActionResult > GetGames ( )
24
+ {
25
+ List < TicTacToeGameDTO > gamesList = await _ticTacToeGameService . GetTicTacToeGameList ( ) ;
26
+
27
+ //Here normally an automated mapper will be used
28
+ List < TicTacToeGameResponseDTO > gamesDTO = new ( ) ;
29
+
30
+ foreach ( var item in gamesList )
31
+ {
32
+ gamesDTO . Add ( new TicTacToeGameResponseDTO
33
+ {
34
+ Id = item . Id ,
35
+ Board = item . Board ,
36
+ Status = item . Status ,
37
+ UserIsCrosses = item . UserIsCrosses
38
+ } ) ;
39
+ }
40
+
41
+ return Ok ( gamesDTO ) ;
42
+ }
43
+
44
+ [ HttpPost ]
45
+ public async Task < IActionResult > CreateGame ( TicTacToeGameCreationDTO ticTacToeGameCreationDTO )
46
+ {
47
+ var game = await _ticTacToeGameService . CreateTicTacToeGame ( ticTacToeGameCreationDTO ) ;
48
+ //in case of error, the error middleware will catch and handle it
49
+ return Ok ( game ) ;
50
+ }
51
+
52
+ [ HttpGet ( "{game_id:Guid}" ) ]
53
+ public async Task < IActionResult > GetGameById ( Guid game_id )
54
+ {
55
+ var game = await _ticTacToeGameService . GetTicTacToeGameById ( game_id ) ;
56
+
57
+ if ( game . isError )
58
+ {
59
+ return NotFound ( game . Message ) ;
60
+ }
61
+ //usually an auto mapper will be here
62
+ return Ok ( new TicTacToeGameResponseDTO { Id = game . Id , Board = game . Board , Status = game . Status } ) ;
63
+ }
64
+ [ HttpPut ( "{game_id:Guid}" ) ]
65
+ public async Task < IActionResult > UpdateGame ( Guid game_id , UpdateGameDTO updateGameDTO )
66
+ {
67
+ var game = await _ticTacToeGameService . UpdateTicTacToeGame ( game_id , updateGameDTO ) ;
68
+ if ( game . isError )
69
+ {
70
+ return BadRequest ( game . Message ) ;
71
+ }
72
+ //usually an auto mapper will be here
73
+ return Ok ( new TicTacToeGameResponseDTO { Id = game . TicTacToeGameDTO . Id , Board = game . TicTacToeGameDTO . Board , Status = game . TicTacToeGameDTO . Status , UserIsCrosses = game . TicTacToeGameDTO . UserIsCrosses } ) ;
74
+ }
75
+
76
+ [ HttpDelete ( "{game_id:Guid}" ) ]
77
+ public async Task < IActionResult > DeleteGame ( Guid game_id )
78
+ {
79
+ _ticTacToeGameService . DeleteTicTacToeGame ( game_id ) ;
80
+ //in case or error the error middleware should handle it
81
+ return Ok ( ) ;
82
+ }
83
+ }
84
+ }
0 commit comments