Skip to content

Commit 68776ad

Browse files
committed
Add SQL Storage option
This upload files as chunks using NUGET Files.EntityFrameworkCore.Extensions
1 parent 57013b0 commit 68776ad

22 files changed

Lines changed: 1006 additions & 269 deletions

Contracts/Contracts.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<TargetFramework>net10.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
9+
</ItemGroup>
10+
711
<ItemGroup>
812
<ProjectReference Include="..\Models\Models.csproj" />
913
</ItemGroup>

FilesAPI/FilesAPI.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
<ItemGroup>
99
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.1">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1" />
1016
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
1117
</ItemGroup>
1218

FilesAPI/Program.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.DependencyInjection;
24
using Microsoft.Extensions.Hosting;
5+
using Services;
6+
using System;
7+
using System.Diagnostics;
38
using System.Threading.Tasks;
49

510
namespace FilesAPI
611
{
7-
public class Program
8-
{
9-
public static async Task Main(string[] args)
10-
{
11-
var host = CreateHostBuilder(args).Build();
12+
public class Program
13+
{
14+
public static async Task Main(string[] args)
15+
{
16+
var host = CreateHostBuilder(args).Build();
17+
try
18+
{
19+
var dbContext = host.Services.CreateScope().ServiceProvider.GetRequiredService<FilesDbContext>();
20+
await dbContext.Database.MigrateAsync();
21+
}
22+
catch (Exception ex)
23+
{
24+
Trace.TraceError($"Error during database migration: {ex.Message}");
25+
}
26+
await host.RunAsync();
27+
}
1228

13-
await host.RunAsync();
14-
}
15-
16-
public static IHostBuilder CreateHostBuilder(string[] args) =>
17-
Host.CreateDefaultBuilder(args)
18-
.ConfigureWebHostDefaults(webBuilder =>
19-
{
20-
webBuilder.UseStartup<Startup>();
21-
// URLs are configured via environment variables (ASPNETCORE_URLS)
22-
});
23-
}
29+
public static IHostBuilder CreateHostBuilder(string[] args) =>
30+
Host.CreateDefaultBuilder(args)
31+
.ConfigureWebHostDefaults(webBuilder =>
32+
{
33+
webBuilder.UseStartup<Startup>();
34+
// URLs are configured via environment variables (ASPNETCORE_URLS)
35+
});
36+
}
2437
}

FilesAPI/Startup.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Http.Features;
77
using Microsoft.AspNetCore.Server.Kestrel.Core;
8+
using Microsoft.EntityFrameworkCore;
89
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Hosting;
@@ -84,6 +85,9 @@ public void ConfigureServices(IServiceCollection services)
8485
var useEmbeddedDatabase = Configuration.GetValue<bool>("USE_EMBEDDED_DATABASE", false) ||
8586
Environment.GetEnvironmentVariable("USE_EMBEDDED_DATABASE") == "true";
8687

88+
var useSqlDatabase = Configuration.GetValue<bool>("USE_SQL_DATABASE", false) ||
89+
Environment.GetEnvironmentVariable("USE_SQL_DATABASE") == "true";
90+
8791
if (useEmbeddedDatabase)
8892
{
8993
// Use LiteDB for self-contained operation
@@ -99,11 +103,19 @@ public void ConfigureServices(IServiceCollection services)
99103
services.AddScoped<IDownloadAnalyticsRepository>(provider =>
100104
new Services.Repositories.LiteDbDownloadAnalyticsRepository(databasePath));
101105
}
106+
else if (useSqlDatabase)
107+
{
108+
// Use SQL database for operation
109+
services.AddDbContext<FilesDbContext>(options => options.UseSqlite("Data Source=database.db"));
110+
services.AddScoped<IStorageRepository, SqlStorageRepository>();
111+
services.AddScoped<IFileDetailsRepository, SqlFileDetailsRepository>();
112+
services.AddScoped<IDownloadAnalyticsRepository, SqlDbDownloadAnalyticsRepository>();
113+
}
102114
else
103115
{
104116
// Use MongoDB for traditional operation
105-
services.AddScoped<IStorageRepository, StorageRepository>();
106-
services.AddScoped<IFileDetailsRepository, FileDetailsRepository>();
117+
services.AddScoped<IStorageRepository, MongoStorageRepository>();
118+
services.AddScoped<IFileDetailsRepository, MongoFileDetailsRepository>();
107119
services.AddScoped<IDownloadAnalyticsRepository, Services.Repositories.MongoDbDownloadAnalyticsRepository>();
108120
}
109121

FilesAPI/appsettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
},
1111
"LiteDBAppSettings": {
1212
"ConnectionString": "Filename=D:\\NoSqlDBs\\LiteBD\\filesAPI_database.db;"
13-
}
14-
}
13+
},
14+
"USE_SQL_DATABASE": true
15+
}

FilesAPI/database.db

76 KB
Binary file not shown.

Models/FileEntity.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Files.EntityFrameworkCore.Extensions;
2+
using System;
3+
4+
namespace Models;
5+
6+
public class FileEntity : IFileEntity
7+
{
8+
public Guid Id { get; set; }
9+
public Guid FileId { get; set; }
10+
public string Name { get; set; }
11+
public string MimeType { get; set; }
12+
public DateTimeOffset TimeStamp { get; set; }
13+
public Guid? NextId { get; set; }
14+
public int ChunkBytesLength { get; set; }
15+
public long TotalBytesLength { get; set; }
16+
public byte[] Data { get; set; }
17+
}

Models/Models.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88
<FrameworkReference Include="Microsoft.AspNetCore.App" />
99
</ItemGroup>
1010

11+
<ItemGroup>
12+
<PackageReference Include="Files.EntityFrameworkCore.Extensions" Version="2.3.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
14+
</ItemGroup>
15+
1116
</Project>

Services.Tests/Services.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
1011
<PackageReference Include="NSubstitute" Version="5.3.0" />
1112
<PackageReference Include="NUnit" Version="4.4.0" />
1213
<PackageReference Include="NUnit3TestAdapter" Version="6.0.1" />

Services/Migrations/20251228154756_InitialCreate.Designer.cs

Lines changed: 143 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)