< Summary

Information
Class: Api.Files.Setup
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Files/Setup.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 68
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RegisterServices(...)100%11100%
MapEndpoints(...)100%44100%
SeedDb()75%44100%

File(s)

/home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Files/Setup.cs

#LineLine coverage
 1// SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5using System.Linq;
 6using System.Text.Json;
 7using Api.Database;
 8using Api.Files.Endpoints;
 9using Api.Files.FileProviders;
 10using Api.Files.Models;
 11using Microsoft.EntityFrameworkCore;
 12
 13namespace Api.Files;
 14
 15public class Setup
 16{
 17    // Mostly Ai Generated - Start
 18    public static void RegisterServices(IServiceCollection services)
 119    {
 120        services.AddSingleton<FileProviderFactory>();
 121        services.AddScoped<UserFileRouter>();
 122    }
 23
 24    public static void MapEndpoints(IEndpointRouteBuilder route)
 125    {
 126        route.MapGet("/{id:guid}", GetFileEndpoint.GetFileAsync);
 127        route
 128            .MapPost("/{id:guid}", UploadFileEndpoint.UploadFileAsync)
 129            .DisableAntiforgery();
 130    }
 31
 32    // Mostly Ai Generated - End
 33
 34    public static async Task SeedDb(PGContext db, IConfiguration config)
 135    {
 136        var defaultConfigExists = await db
 137            .FileProviderBackendConfigs.Where(cfg =>
 138                cfg.InstanceWide == true && cfg.FallbackDefault == true
 139            )
 140            .AsNoTracking()
 141            .AnyAsync();
 142        if (!defaultConfigExists)
 143        {
 144            var now = Now();
 145            var fsCfg = new FileProviderBackendConfig()
 146            {
 147                Id = Guid.CreateVersion7()
 148                    .WithPostfix(FileProviderBackendConfig.IdPostfix),
 149                MetadataAddedAt = now,
 150                MetadataUpdatedAt = now,
 151                OwnerId = null,
 152
 153                InstanceWide = true,
 154                FallbackDefault = true,
 155                DefaultFor = [],
 156                ProviderId = FileProviderId.LocalFsProvider,
 157                ProviderConfig = JsonSerializer.SerializeToDocument(
 158                    new LocalFSFileProviderConfig(
 159                        $"./BlobStorage{(config["PR_TestPrefix"] is { } prefix ? $"_{prefix}" : "")}"
 160                    )
 161                ),
 162            };
 63
 164            await db.FileProviderBackendConfigs.AddAsync(fsCfg);
 165            await db.SaveChangesAsync();
 166        }
 167    }
 68}