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