Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Gridly-Client/publish/
Gridly-Client/dist
Gridly-Client/package-lock.json
.vs/*
Gridly.csproj.lscache

# Node
Gridly-Client/node_modules
Expand All @@ -26,6 +25,8 @@ yarn-error.log
*.launch
.settings/
*.sublime-workspace
Gridly.csproj.lscache
Gridly.csproj.user

# Visual Studio Code
!.vscode/settings.json
Expand Down
10 changes: 5 additions & 5 deletions Data/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Dapper;
using Microsoft.Data.Sqlite;
using System.Data;

namespace Gridly.Data;

public class DbInitializer
{
private SqliteConnection connection;
private readonly IDbConnection connection;

public DbInitializer(IConfiguration configuration)
public DbInitializer(IDbConnection connection)
{
connection = new SqliteConnection(configuration.GetConnectionString("GridlyDb"));
this.connection = connection;
}

public async Task EnsureTablesCreatedAsync()
Expand Down Expand Up @@ -47,4 +47,4 @@ CREATE TABLE IF NOT EXISTS Settings(
FOREIGN KEY(CardId) REFERENCES Card(Id));",
commandTimeout:150);
}
}
}
11 changes: 11 additions & 0 deletions Gridly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@
<UpToDateCheckInput Remove="Views\Main\Index.cshtml" />
</ItemGroup>

<ItemGroup>
<Content Update="wwwroot\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Assets\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="dapper" Version="2.1.66" />
Expand Down
25 changes: 13 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
using Gridly.Repositories;
using Gridly.Services;
using Gridly.Data;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.FileProviders;
using System.Data;

var builder = WebApplication.CreateBuilder(args);
var appDirectory = Path.GetDirectoryName(Environment.ProcessPath) ?? AppContext.BaseDirectory;
Directory.SetCurrentDirectory(appDirectory);

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = appDirectory
});

builder.Services.AddControllersWithViews();
builder.Services.AddControllers();
await builder.Services.AddTokenBucketRateLimiter();

builder.Services.AddScoped<DbInitializer>();
builder.Services.AddScoped<System.Data.IDbConnection>(sp =>
new SqliteConnection(builder.Configuration.GetConnectionString("GridlyDb")));
builder.Services.AddScoped(sp =>
sp.GetRequiredService<IDbConnectionServices>().CreateConnection());
builder.Services.AddScoped<IVersionEndPoint, VersionEndPoint>();
builder.Services.AddScoped<ICardRepository,CardRepository>();
builder.Services.AddScoped<ISettingsRepository,SettingsRepository>();
builder.Services.AddScoped<IIconRepository,IconRepository>();
builder.Services.AddScoped<IIconConnectedRepository,IconConnectedRepository>();

builder.Services.AddSingleton<IDbConnectionServices,DbConnectionServices>();
builder.Services.AddSingleton<IMemoryCashingService, MemoryCashingServices>();
builder.Services.AddSingleton<IHttpClientServices, HttpClientServices>();
builder.Services.AddSingleton<IFileService, FileService>();
Expand All @@ -32,12 +39,6 @@

app.UseStaticFiles();

app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Assets/Icons")),
RequestPath = "/Assets/Icons"
});

app.MapDefaultControllerRoute().RequireRateLimiting("fixed");

app.MapControllerRoute(
Expand All @@ -59,4 +60,4 @@
endpoints.MapFallbackToFile("index.html");
});

app.Run();
app.Run();
37 changes: 37 additions & 0 deletions Services/DbConnectionServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Data;
using Microsoft.Data.Sqlite;

namespace Gridly.Data;

public class DbConnectionServices : IDbConnectionServices
{
private readonly string connectionString;

public DbConnectionServices(IConfiguration configuration, IHostEnvironment environment)
{
var configuredConnectionString = configuration.GetConnectionString("GridlyDb");
if (string.IsNullOrWhiteSpace(configuredConnectionString))
{
throw new InvalidOperationException("Connection string 'GridlyDb' is missing.");
}

var sqliteBuilder = new SqliteConnectionStringBuilder(configuredConnectionString);
if (!Path.IsPathRooted(sqliteBuilder.DataSource))
{
sqliteBuilder.DataSource = Path.Combine(environment.ContentRootPath, sqliteBuilder.DataSource);
}

var dbDirectory = Path.GetDirectoryName(sqliteBuilder.DataSource);
if (!string.IsNullOrWhiteSpace(dbDirectory))
{
Directory.CreateDirectory(dbDirectory);
}

connectionString = sqliteBuilder.ConnectionString;
}

public IDbConnection CreateConnection()
{
return new SqliteConnection(connectionString);
}
}
7 changes: 7 additions & 0 deletions Services/IDbConnectionServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Data;
namespace Gridly.Data;

public interface IDbConnectionServices
{
public IDbConnection CreateConnection();
}
Loading