Skip to content

Commit

Permalink
Merge pull request #7 from AaronSaikovski/v1.0.0-beta3
Browse files Browse the repository at this point in the history
V1.0.0 beta3
  • Loading branch information
AaronSaikovski authored Apr 8, 2024
2 parents 6d60024 + 9e297d4 commit 72db3d9
Show file tree
Hide file tree
Showing 85 changed files with 1,336 additions and 1,464 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@

- Major solution and project restructure to follow a more Clean architecture and easier to follow decoupled object hierarchy.
- Refactored for performance and better memory usage.

## v1.0.0-Beta3 2024-XX-XX)

-
-
1 change: 0 additions & 1 deletion Crossword.API/Crossword.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<RootNamespace>CrosswordData</RootNamespace>
<Version></Version>
<AssemblyVersion>1.0.1</AssemblyVersion>
<FileVersion>1.0.1</FileVersion>
Expand Down
21 changes: 21 additions & 0 deletions Crossword.API/Data/PuzzleData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Crossword.Shared.ParserUtils;

namespace Crossword.API.Data;

public static class PuzzleData
{
/// <summary>
/// Reads a random file from the file system and returns it as a string
/// </summary>
/// <param name="puzzleDataFile"></param>
/// <returns></returns>
public static string? GetCrosswordPuzzleData(string? puzzleDataFile)
{

ArgumentException.ThrowIfNullOrEmpty(puzzleDataFile);

//get the data file
var fileResult = ParserHelper.GetRandomDataFile(puzzleDataFile);
return fileResult;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using Crossword.API.Data;

namespace Crossword.API.Endpoints;

public static class CrosswordData
{
#region RegisterCrosswordEndpoints

//Ref: https://blog.treblle.com/how-to-structure-your-minimal-api-in-net/

/// <summary>
/// RegisterCrosswordDataEndpoint
/// </summary>
/// <param name="routes"></param>
public static void RegisterCrosswordEndpoints(this IEndpointRouteBuilder routes)
{
var crosswordData = routes.MapGroup("/api/v1/crosswordpuzzledata");

crosswordData.MapGet("/getcrosswordpuzzledata", (IConfiguration configuration) => PuzzleData.GetCrosswordPuzzleData(configuration["DatafilePath"])).WithName("GetCrosswordPuzzleData");
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Crossword.API.Middleware;

namespace Crossword.API.Extensions;

//Source: https://blog.treblle.com/how-to-structure-your-minimal-api-in-net/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

using Crossword.Shared.Constants;

namespace Crossword.API;
namespace Crossword.API.Middleware;

public class ApiKeyMiddleware(RequestDelegate next)
{
public async Task InvokeAsync(HttpContext context) {
public async Task InvokeAsync(HttpContext context)
{
if (!context.Request.Headers.TryGetValue(ApiConstants.ApiKeyName, out
var extractedApiKey)) {
var extractedApiKey))
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Api Key was not provided ");
return;
}
var appSettings = context.RequestServices.GetRequiredService < IConfiguration > ();
var apiKey = appSettings.GetValue < string > (ApiConstants.ApiKeyName);
if (apiKey is not null && !apiKey.Equals(extractedApiKey)) {
var appSettings = context.RequestServices.GetRequiredService<IConfiguration>();
var apiKey = appSettings.GetValue<string>(ApiConstants.ApiKeyName);
if (apiKey is not null && !apiKey.Equals(extractedApiKey))
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Unauthorized API Request.");
return;
Expand Down
6 changes: 1 addition & 5 deletions Crossword.API/src/Program.cs → Crossword.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


//Init the logger and get the active config
var logger = new SerilogLogger(ConfigurationHelper.ActiveConfiguration);
using var logger = new SerilogLogger(ConfigurationHelper.ActiveConfiguration);

try
{
Expand All @@ -23,7 +23,3 @@
{
logger.LogFatal(ex, ex.Message);
}
finally
{
logger.Dispose();
}
26 changes: 0 additions & 26 deletions Crossword.API/src/Data/PuzzleData.cs

This file was deleted.

45 changes: 0 additions & 45 deletions Crossword.Data/GetCrosswordDataAsync.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@

using Crossword.Shared.Config;
using Crossword.Shared.Logger;
using Crossword.Shared.Constants;
using Crossword.Shared.Logger;
using Crossword.Shared.Config;

namespace Crossword.Data;

/// <summary>
/// Fetches data from the Data API
/// Calls the Crossword.API to get the Puzzledata as a string
/// </summary>
public partial class FetchCrosswordData
public static class GetPuzzleDataAsync
{
#region CallDataApiAsync

/// <summary>
/// CallDataApiAsync
/// CallDataApiAsync
/// </summary>
/// <returns></returns>
private static async Task<string?> CallDataApiAsync()
{
//Init the logger and get the active config
var logger = new SerilogLogger(ConfigurationHelper.ActiveConfiguration);
using var logger = new SerilogLogger(ConfigurationHelper.ActiveConfiguration);

//Use the HttpClient
using var client = new HttpClient();
Expand All @@ -45,11 +45,9 @@ public partial class FetchCrosswordData
//get the response from the API call result as a string
return response.Content.ReadAsStringAsync().Result;
}
else
{
throw new Exception($"Failed to call the API. Status code: {response.StatusCode}");
}


throw new Exception($"Failed to call the API. Status code: {response.StatusCode}");

}
//catch http request exception
catch (HttpRequestException httpex)
Expand All @@ -63,10 +61,40 @@ public partial class FetchCrosswordData
logger.LogError(ex, ex.Message);
return string.Empty;
}
finally

}

#endregion


#region GetCrosswordDataAsync

/// <summary>
/// GetCrosswordDataAsync
/// </summary>
/// <returns></returns>
public static async Task<string?> GetCrosswordDataAsync()
{
//Init the logger and get the active config
using var logger = new SerilogLogger(ConfigurationHelper.ActiveConfiguration);

//Call the API to get the puzzledata....otherwise use default values
try
{
logger.Dispose();
logger.LogInformation("Start GetCrosswordDataAsync()");

//call the API
var apiResponse = await CallDataApiAsync();

//check what was returned
return string.IsNullOrEmpty(apiResponse) ? GameConstants.DefaultPuzzleData : apiResponse;
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
throw;
}

}

#endregion
Expand Down
1 change: 0 additions & 1 deletion Crossword.Entities/Crossword.Entities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Crosssword.Entities</RootNamespace>
</PropertyGroup>

</Project>
28 changes: 12 additions & 16 deletions Crossword.Entities/CrosswordData.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
//////////////////////////////////////////////////////////////////////////////
// //
// Module: CrosswordData.cs //
// Authors: Aaron Saikovski & Bryan Richards //
// Date: 23/01/97 //
// Version: 1.0 //
// Purpose: Utilizes a String Tokenizer to parse the crossword //
// puzzle components from a data set string. //
// //
//////////////////////////////////////////////////////////////////////////////



namespace Crossword.Entities;

public sealed class CrosswordData
public sealed record CrosswordData
{
#region Fields

//Instance variables for holding parsed QuickCrossword data
public string? PuzzleType { get; set; }
public int NumCols { get; set; }
public int NumRows { get; set; }
public int NumAcross { get; set; }
public int NumDown { get; set; }

//public int NumAcross { get; }
//public int NumDown { get; }
public int PuzzleId { get; set; }
public int[]? ColRef { get; set; }
public int[]? RowRef { get; set; }
public int[]? IsAcross { get; set; }
public int[]? QuesNum { get; set; }
public string[]? Clues { get; set; }
public string[]? Answers { get; set; }

public int[]? Costs { get; set; }

//public int[]? Costs { get; set; }
public int[]? Costs { get; }

public string? GetLetters { get; set; }
public string? Blurb { get; set; }

public int NumQuestions { get; set; }

public int NumBytes { get; set; }



#endregion

}
12 changes: 6 additions & 6 deletions Crossword.Entities/CrosswordState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public sealed class CrosswordState(int coordAcross, int coordDown, string answer
{
#region getters_setters

public int CoordAcross { get;} = coordAcross;
public int CoordDown { get; } = coordDown;
public string Answer { get; } = answer;
public string Clue { get; } = clue;
public bool IsAcross { get; } = isAcross;
public int CoordAcross { get; } = coordAcross;
public int CoordDown { get; } = coordDown;
public string Answer { get; } = answer;
public string Clue { get; } = clue;
public bool IsAcross { get; } = isAcross;
public int QuestionNum { get; } = questionNum;

#endregion
}
2 changes: 1 addition & 1 deletion Crossword.Parser/GetAnswers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;


namespace Crossword.Parser;

Expand Down
2 changes: 1 addition & 1 deletion Crossword.Parser/GetBlurb.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;


namespace Crossword.Parser;

Expand Down
Loading

0 comments on commit 72db3d9

Please sign in to comment.