Skip to content

Commit

Permalink
Switch on Null analysis and don't load config in constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEadie committed Aug 1, 2023
1 parent 0200554 commit 476b99a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
17 changes: 8 additions & 9 deletions src/hub/src/Worms.Gateway/Announcers/Slack/SlackAnnouncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@ namespace Worms.Gateway.Announcers.Slack;

internal class SlackAnnouncer : ISlackAnnouncer
{
private readonly IConfiguration _configuration;
private readonly ILogger<SlackAnnouncer> _logger;
private readonly string _webHookUrl;

public SlackAnnouncer(IConfiguration configuration, ILogger<SlackAnnouncer> logger)
{
_configuration = configuration;
_logger = logger;
_webHookUrl = configuration.GetValue<string>("SlackWebHookURL");
}

public async Task AnnounceGameStarting(string hostName)
{
if (string.IsNullOrWhiteSpace(_webHookUrl))
var webHookUrl = _configuration.GetValue<string>("SlackWebHookURL");

if (string.IsNullOrWhiteSpace(webHookUrl))
{
_logger.LogWarning("A Slack web hook must be configured to announce a game");
return;
}

#if DEBUG
var slackMessage = new SlackMessage
{
Text = $"Debug: This is a test run from local dev. Hosting at wa://{hostName}"
};
var slackMessage = new SlackMessage($"Debug: This is a test run from local dev. Hosting at wa://{hostName}");
#else
var slackMessage = new SlackMessage { Text = $"<!here> Hosting at: wa://{hostName}" };
var slackMessage = new SlackMessage($"<!here> Hosting at: wa://{hostName}");
#endif

_logger.LogInformation("Announcing game starting to Slack");
using var client = new HttpClient();
var slackUrl = new System.Uri(_webHookUrl);
var slackUrl = new System.Uri(webHookUrl);
var body = JsonSerializer.Serialize(slackMessage);
var content = new StringContent(body, Encoding.UTF8, "application/json");

Expand Down
11 changes: 3 additions & 8 deletions src/hub/src/Worms.Gateway/Announcers/Slack/SlackMessage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System.Text.Json.Serialization;

namespace Worms.Gateway.Announcers.Slack
{
public sealed class SlackMessage
{
[JsonPropertyName("text")]
public string Text { get; set; }
}
}
namespace Worms.Gateway.Announcers.Slack;

public sealed record SlackMessage([property: JsonPropertyName("text")] string Text);
13 changes: 8 additions & 5 deletions src/hub/src/Worms.Gateway/Database/GamesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ namespace Worms.Gateway.Database;

public class GamesRepository : IRepository<GameDto>
{
private readonly string _connectionString;
private readonly IConfiguration _configuration;

public GamesRepository(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("Database");
_configuration = configuration;
}

public IReadOnlyCollection<GameDto> Get()
{
using var connection = new NpgsqlConnection(_connectionString);
var connectionString = _configuration.GetConnectionString("Database");
using var connection = new NpgsqlConnection(connectionString);

var dbObjects = connection.Query<GamesDb>("SELECT id, status, hostmachine FROM games");
return dbObjects.Select(x => new GameDto(x.Id.ToString(), x.Status, x.HostMachine)).ToList();
}

public GameDto Create(GameDto item)
{
using var connection = new NpgsqlConnection(_connectionString);
var connectionString = _configuration.GetConnectionString("Database");
using var connection = new NpgsqlConnection(connectionString);
const string sql = "INSERT INTO games (status, hostmachine) VALUES (@status, @hostmachine) RETURNING id";

var parameters = new
Expand All @@ -41,7 +43,8 @@ public GameDto Create(GameDto item)

public void Update(GameDto item)
{
using var connection = new NpgsqlConnection(_connectionString);
var connectionString = _configuration.GetConnectionString("Database");
using var connection = new NpgsqlConnection(connectionString);
const string sql = "UPDATE games SET status = @status, hostmachine = @hostmachine WHERE id = @id";

var parameters = new
Expand Down
13 changes: 8 additions & 5 deletions src/hub/src/Worms.Gateway/Database/ReplaysRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ namespace Worms.Gateway.Database;

public class ReplaysRepository : IRepository<Replay>
{
private readonly string _connectionString;
private readonly IConfiguration _configuration;

public ReplaysRepository(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("Database");
_configuration = configuration;
}

public IReadOnlyCollection<Replay> Get()
{
using var connection = new NpgsqlConnection(_connectionString);
var connectionString = _configuration.GetConnectionString("Database");
using var connection = new NpgsqlConnection(connectionString);

var dbObjects = connection.Query<ReplayDb>("SELECT id, name, status, filename FROM replays");
return dbObjects.Select(x => new Replay(x.Id.ToString(), x.Name, x.Status, x.Filename)).ToList();
}

public Replay Create(Replay item)
{
using var connection = new NpgsqlConnection(_connectionString);
var connectionString = _configuration.GetConnectionString("Database");
using var connection = new NpgsqlConnection(connectionString);
const string sql = "INSERT INTO replays "
+ "(name, status, filename) "
+ "VALUES (@name, @status, @filename) "
Expand All @@ -45,7 +47,8 @@ public Replay Create(Replay item)

public void Update(Replay item)
{
using var connection = new NpgsqlConnection(_connectionString);
var connectionString = _configuration.GetConnectionString("Database");
using var connection = new NpgsqlConnection(connectionString);
const string sql = "UPDATE replays SET "
+ "name = @name, "
+ "status = @status, "
Expand Down
9 changes: 5 additions & 4 deletions src/hub/src/Worms.Gateway/Worms.Gateway.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.143" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Npgsql" Version="7.0.4" />
<PackageReference Include="Dapper" Version="2.0.143"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.9"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0"/>
<PackageReference Include="Npgsql" Version="7.0.4"/>
</ItemGroup>
</Project>

0 comments on commit 476b99a

Please sign in to comment.