Skip to content

Commit

Permalink
Begin work on SQL implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Aug 31, 2024
1 parent 150c4d5 commit 0762d53
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 56 deletions.
34 changes: 0 additions & 34 deletions Gangs.sln

This file was deleted.

7 changes: 0 additions & 7 deletions GangsAPI/Services/IStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,4 @@ public interface IStatManager : IPluginBehavior {
/// <param name="id"></param>
/// <returns></returns>
Task<bool> UnregisterStat(string id);

/// <summary>
/// Updates a statistic with the manager.
/// </summary>
/// <param name="stat"></param>
/// <returns></returns>
Task<bool> UpdateStat(IStat stat);
}
7 changes: 0 additions & 7 deletions GangsImpl.Mock/MockStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,4 @@ public Task<bool> UnregisterStat(string id) {
foreach (var stat in matches) stats.Remove(stat);
return Task.FromResult(matches.Count > 0);
}

public Task<bool> UpdateStat(IStat stat) {
var matches = stats.Where(s => s.StatId == stat.StatId).ToList();
foreach (var match in matches) stats.Remove(match);
stats.Add(stat);
return Task.FromResult(matches.Count > 0);
}
}
18 changes: 18 additions & 0 deletions GangsImpl.SQL/GangsImpl.SQL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\GangsAPI\GangsAPI.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions GangsImpl.SQL/SQLStat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using GangsAPI.Data.Stat;

namespace GangsImpl.SQL;

public class SQLStat : IStat {
public string StatId { get; set; }
public string Name { get; set; }
public string? Description { get; set; }
}
75 changes: 75 additions & 0 deletions GangsImpl.SQL/SQLStatManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Data;
using Dapper;
using GangsAPI.Data.Stat;
using GangsAPI.Services;
using MySqlConnector;

namespace GangsImpl.SQL;

public class SQLStatManager(string connectionString,
string table = "gang_stats") : IStatManager {
private readonly HashSet<IStat> stats = [];
private MySqlConnection connection = null!;

public void Start() {
connection = new MySqlConnection(connectionString);

connection.Open();
var command = connection.CreateCommand();
command.CommandText =
$"CREATE TABLE IF NOT EXISTS {table} (StatId VARCHAR(255) PRIMARY KEY, Name VARCHAR(255), Description TEXT)";
command.ExecuteNonQuery();

connection.Query<SQLStat>($"SELECT * FROM {table}")
.ToList()
.ForEach(stat => stats.Add(stat));
}

public void Dispose() {
var command = connection.CreateCommand();
command.CommandText = $"DROP TABLE IF EXISTS {table}";
command.ExecuteNonQuery();
connection.Close();
connection.Dispose();
}

public async Task<IEnumerable<IStat>> GetStats() {
return await Task.FromResult<IEnumerable<IStat>>(stats);
}

public Task<IStat?> GetStat(string id) {
return Task.FromResult(stats.FirstOrDefault(stat => stat.StatId == id));
}

public async Task<IStat?> CreateStat(string id, string name,
string? description = null) {
var stat = await GetStat(id);
if (stat != null) return stat;
stat = new SQLStat { StatId = id, Name = name, Description = description };
return stat;
}

public async Task<bool> RegisterStat(IStat stat) {
var sqlStat = (SQLStat)stat;
var command = connection.CreateCommand();
command.CommandText =
$"INSERT INTO {table} (StatId, Name, Description) VALUES (@StatId, @Name, @Description)";
command.Parameters.AddWithValue("@StatId", sqlStat.StatId);
command.Parameters.AddWithValue("@Name", sqlStat.Name);
command.Parameters.AddWithValue("@Description", sqlStat.Description);
await command.ExecuteNonQueryAsync();
return stats.Add(stat);
}

public async Task<bool> UnregisterStat(string id) {
var matches = stats.Where(stat => stat.StatId == id).ToList();
foreach (var stat in matches) stats.Remove(stat);

var command = connection.CreateCommand();
command.CommandText = $"DELETE FROM {table} WHERE StatId = @StatId";
command.Parameters.AddWithValue("@StatId", id);
await command.ExecuteNonQueryAsync();

return await Task.FromResult(matches.Count > 0);
}
}
16 changes: 9 additions & 7 deletions GangsTest/GangsTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FuzzDotNet.MSTest" Version="0.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Xunit.DependencyInjection" Version="9.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="FuzzDotNet.MSTest" Version="0.1.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Xunit.DependencyInjection" Version="9.3.1"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit"/>
<!-- Disable parallelism for xunit tests -->
</ItemGroup>

<ItemGroup>
Expand All @@ -28,7 +29,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GangsImpl.Mock\GangsImpl.Mock.csproj" />
<ProjectReference Include="..\GangsImpl.Mock\GangsImpl.Mock.csproj"/>
<ProjectReference Include="..\GangsImpl.SQL\GangsImpl.SQL.csproj"/>
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion GangsTest/StatTests/StatManagerData.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Collections;
using GangsAPI;
using GangsImpl.Memory;
using GangsImpl.SQL;

namespace GangsTest.StatTests;

public class StatManagerData : IEnumerable<object[]> {
private readonly IBehavior[] behaviors = [new MockStatManager()];
private readonly IBehavior[] behaviors = [
new MockStatManager(),
new SQLStatManager("Server=localhost;User=root;Database=gang", "gang_unit_test")
];

public StatManagerData() {
foreach (var behavior in behaviors) behavior.Start();
Expand Down

0 comments on commit 0762d53

Please sign in to comment.