Skip to content

Commit c96098b

Browse files
committed
Add project files.
1 parent 8deba34 commit c96098b

14 files changed

+639
-0
lines changed

SSHTicTacToe.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32126.317
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SSHTicTacToe", "SSHTicTacToe\SSHTicTacToe.csproj", "{23145DD1-E180-4B7D-9F58-1AF151250CFF}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TicTacToe.TEST", "TicTacToe.TEST\TicTacToe.TEST.csproj", "{1CE32F0B-E2F5-412D-95DC-55B88F3389D7}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{23145DD1-E180-4B7D-9F58-1AF151250CFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{23145DD1-E180-4B7D-9F58-1AF151250CFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{23145DD1-E180-4B7D-9F58-1AF151250CFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{23145DD1-E180-4B7D-9F58-1AF151250CFF}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{1CE32F0B-E2F5-412D-95DC-55B88F3389D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{1CE32F0B-E2F5-412D-95DC-55B88F3389D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{1CE32F0B-E2F5-412D-95DC-55B88F3389D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{1CE32F0B-E2F5-412D-95DC-55B88F3389D7}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {DCF97879-EDE1-41CC-8065-E62254D28B57}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

SSHTicTacToe/DTO/TicTacToeGameDTO.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace SSHTicTacToe.DTO
2+
{
3+
public class TicTacToeGameDTO : CustomError
4+
{
5+
public Guid Id { get; set; }
6+
public string Board { get; set; }
7+
public string Status { get; set; }
8+
public bool UserIsCrosses { get; set; }
9+
}
10+
11+
public class TicTacToeGameResponseDTO
12+
{
13+
public Guid Id { get; set; }
14+
public string Board { get; set; }
15+
public string Status { get; set; }
16+
public bool UserIsCrosses { get; set; }
17+
}
18+
19+
public class TicTacToeGameCreationDTO
20+
{
21+
public string Board { get; set; }
22+
}
23+
24+
public class CreateGameResponseDTO
25+
{
26+
public string Location { get; set; }
27+
}
28+
29+
public class UpdateGameDTO
30+
{
31+
public string Board { get; set; }
32+
}
33+
34+
public class Winner
35+
{
36+
public bool isWin { get; set; }
37+
public char WinnerName { get; set; }
38+
}
39+
40+
public class UpdateGameResponseDTO : CustomError
41+
{
42+
public TicTacToeGameDTO TicTacToeGameDTO { get; set; }
43+
}
44+
45+
public class CustomError
46+
{
47+
public string Message { get; set; }
48+
public bool isError { get; set; }
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using SSHTicTacToe.DTO;
2+
using System.Net;
3+
using System.Text.Json;
4+
namespace SSHTicTacToe.ErrorMiddleware
5+
{
6+
public class ErrorHandlerMiddleware
7+
{
8+
private readonly RequestDelegate _next;
9+
public ErrorHandlerMiddleware(RequestDelegate next)
10+
{
11+
_next = next;
12+
}
13+
public async Task Invoke(HttpContext context)
14+
{
15+
try
16+
{
17+
await _next(context);
18+
}
19+
catch (Exception error)
20+
{
21+
var response = context.Response;
22+
response.ContentType = "application/json";
23+
24+
switch (error)
25+
{
26+
case KeyNotFoundException e:
27+
// not found error
28+
response.StatusCode = (int)HttpStatusCode.NotFound;
29+
break;
30+
default:
31+
// unhandled error
32+
response.StatusCode = (int)HttpStatusCode.InternalServerError;
33+
break;
34+
}
35+
36+
var result = JsonSerializer.Serialize(new { message = error?.Message });
37+
await response.WriteAsync(result);
38+
}
39+
}
40+
}
41+
}
42+
43+

SSHTicTacToe/Model/TicTacToeGame.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace SSHTicTacToe
2+
{
3+
public class TicTacToeGame
4+
{
5+
public Guid Id { get; set; }
6+
public string Board { get; set; }
7+
public GameStatues Status { get; set; }
8+
}
9+
10+
public enum GameStatues
11+
{
12+
RUNNING = 10,
13+
X_WON = 20,
14+
O_WON = 30,
15+
DRAW = 40,
16+
}
17+
}

SSHTicTacToe/Program.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using SSHTicTacToe.ErrorMiddleware;
2+
using SSHTicTacToe.Services;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Add services to the container.
7+
builder.Services.AddScoped<ITicTacToeGameService, TicTacToeGameService>();
8+
builder.Services.AddControllers();
9+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
10+
builder.Services.AddEndpointsApiExplorer();
11+
builder.Services.AddSwaggerGen();
12+
13+
var app = builder.Build();
14+
15+
// Configure the HTTP request pipeline.
16+
if (app.Environment.IsDevelopment())
17+
{
18+
app.UseSwagger();
19+
app.UseSwaggerUI();
20+
}
21+
22+
app.UseHttpsRedirection();
23+
24+
// global error handler
25+
app.UseMiddleware<ErrorHandlerMiddleware>();
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllers();
30+
31+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:46548",
8+
"sslPort": 44369
9+
}
10+
},
11+
"profiles": {
12+
"SSHTicTacToe": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "https://localhost:7211;http://localhost:5211",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"IIS Express": {
23+
"commandName": "IISExpress",
24+
"launchBrowser": true,
25+
"launchUrl": "swagger",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}

SSHTicTacToe/SSHTicTacToe.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
11+
</ItemGroup>
12+
13+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using SSHTicTacToe.DTO;
2+
3+
namespace SSHTicTacToe.Services
4+
{
5+
public interface ITicTacToeGameService
6+
{
7+
public Task<List<TicTacToeGameDTO>> GetTicTacToeGameList();
8+
public Task<TicTacToeGameDTO> GetTicTacToeGameById(Guid uuid);
9+
public Task<CreateGameResponseDTO> CreateTicTacToeGame(TicTacToeGameCreationDTO ticTacToeGameCreationDTO);
10+
public Task<UpdateGameResponseDTO> UpdateTicTacToeGame(Guid uuid, UpdateGameDTO updateGameDTO);
11+
public void DeleteTicTacToeGame(Guid uuid);
12+
}
13+
}

0 commit comments

Comments
 (0)