-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathProgram.cs
120 lines (100 loc) · 3.44 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.Text.Json.Serialization;
using Codebreaker.Utilities;
using Microsoft.AspNetCore.Http.Json;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// two different JsonOptions, swagger description uses Microsoft.AspNetCore.Mvc.Json
builder.Services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
});
// typed results use Microsoft.AspNetCore.Http.Json
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
});
builder.Services.AddSingleton<IGamesRepository, InMemoryGamesRepository>();
builder.Services.AddSingleton<GamesFactory>();
builder.Services.AddTransient<IGamesService, GamesService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/games", async (IGamesService gamesService) =>
{
IEnumerable<Game> games = await gamesService.GetGamesAsync();
return Results.Ok(games);
})
.WithName("GetGames")
.Produces<IEnumerable<Game>>(StatusCodes.Status200OK)
.WithTags("Info");
// Get game by id
app.MapGet("/games/{gameId:guid}", async (Guid gameId, IGamesService gameService) =>
{
Game? game = await gameService.GetGameAsync(gameId);
if (game is null)
return Results.NotFound();
return Results.Ok(game);
})
.WithName("GetGame")
.Produces<Game>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status404NotFound)
.WithTags("Info");
// Start a game - create a game object
app.MapPost("/games", async (CreateGameRequest request, IGamesService gamesService) =>
{
Game? game = null;
try
{
game = await gamesService.CreateGameAsync(request.GameType, request.PlayerName);
}
catch (GameException ex) when (ex.HResult == 4000)
{
app.Logger.LogError("Game Type not found {gametype}", request.GameType);
return Results.BadRequest();
}
CreateGameResponse createGameResponse = new(game.GameId, game.GameType, game.PlayerName, game.Holes, game.MaxMoves);
return Results.Created($"/{game.GameId}", createGameResponse);
})
.WithName("CreateGame")
.Produces<CreateGameResponse>(StatusCodes.Status201Created)
.Produces(StatusCodes.Status400BadRequest)
.WithTags("Play");
// Create a move for a game
app.MapPost("/games/{gameId:guid}/moves", async (Guid gameId, SetMoveRequest request, IGamesService gamesService) =>
{
if (gameId != request.GameId)
{
return Results.BadRequest();
}
try
{
SetMoveResponse response = await gamesService.SetMoveAsync(request);
return Results.Ok(response);
}
catch (GameException ex) when (ex.HResult is > 4200 and < 4300)
{
return Results.BadRequest();
}
catch (GameException ex) when (ex.HResult == 4400)
{
return Results.NotFound();
}
catch (Exception ex)
{
app.Logger.LogError(ex, "Unexpected error");
return Results.StatusCode(StatusCodes.Status500InternalServerError);
}
})
.WithName("SetMove")
.Produces<SetMoveResponse>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status400BadRequest)
.Produces(StatusCodes.Status404NotFound)
.WithTags("Play");
app.Run();