< Summary

Information
Class: Chronicis.Api.Services.ResourceProviderService
Assembly: Chronicis.Api
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Api/Services/ResourceProviderService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 101
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetAllProvidersAsync()100%210%
GetWorldProvidersAsync()0%4260%
SetProviderEnabledAsync()0%7280%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Api/Services/ResourceProviderService.cs

#LineLine coverage
 1using Chronicis.Api.Data;
 2using Chronicis.Api.Repositories;
 3using Chronicis.Shared.Extensions;
 4using Chronicis.Shared.Models;
 5using Microsoft.EntityFrameworkCore;
 6
 7namespace Chronicis.Api.Services;
 8
 9/// <summary>
 10/// Service implementation for managing resource providers with authorization.
 11/// </summary>
 12public class ResourceProviderService : IResourceProviderService
 13{
 14    private readonly IResourceProviderRepository _repository;
 15    private readonly ChronicisDbContext _context;
 16    private readonly ILogger<ResourceProviderService> _logger;
 17
 018    public ResourceProviderService(
 019        IResourceProviderRepository repository,
 020        ChronicisDbContext context,
 021        ILogger<ResourceProviderService> logger)
 22    {
 023        _repository = repository;
 024        _context = context;
 025        _logger = logger;
 026    }
 27
 28    /// <inheritdoc/>
 29    public async Task<List<ResourceProvider>> GetAllProvidersAsync()
 30    {
 031        return await _repository.GetAllProvidersAsync();
 032    }
 33
 34    /// <inheritdoc/>
 35    public async Task<List<(ResourceProvider Provider, bool IsEnabled)>> GetWorldProvidersAsync(Guid worldId, Guid userI
 36    {
 37        // Verify world exists and user has access
 038        var world = await _context.Worlds
 039            .AsNoTracking()
 040            .FirstOrDefaultAsync(w => w.Id == worldId);
 41
 042        if (world == null)
 43        {
 044            _logger.LogWarning("World {WorldId} not found", worldId);
 045            throw new KeyNotFoundException($"World {worldId} not found");
 46        }
 47
 48        // Check if user is owner or member
 049        var isOwner = world.OwnerId == userId;
 050        var isMember = await _context.WorldMembers
 051            .AnyAsync(wm => wm.WorldId == worldId && wm.UserId == userId);
 52
 053        if (!isOwner && !isMember)
 54        {
 055            _logger.LogWarning("User {UserId} unauthorized to access world {WorldId}", userId, worldId);
 056            throw new UnauthorizedAccessException($"User does not have access to world {worldId}");
 57        }
 58
 059        return await _repository.GetWorldProvidersAsync(worldId);
 060    }
 61
 62    /// <inheritdoc/>
 63    public async Task<bool> SetProviderEnabledAsync(Guid worldId, string providerCode, bool enabled, Guid userId)
 64    {
 65        // Verify world exists
 066        var world = await _context.Worlds
 067            .AsNoTracking()
 068            .FirstOrDefaultAsync(w => w.Id == worldId);
 69
 070        if (world == null)
 71        {
 072            _logger.LogWarning("World {WorldId} not found", worldId);
 073            throw new KeyNotFoundException($"World {worldId} not found");
 74        }
 75
 76        // Check if user is the owner (only owners can modify settings)
 077        if (world.OwnerId != userId)
 78        {
 079            _logger.LogWarning("User {UserId} is not owner of world {WorldId}", userId, worldId);
 080            throw new UnauthorizedAccessException($"Only the world owner can modify resource provider settings");
 81        }
 82
 83        // Attempt to enable/disable the provider
 084        var result = await _repository.SetProviderEnabledAsync(worldId, providerCode, enabled, userId);
 85
 086        if (!result)
 87        {
 088            _logger.LogWarning("Provider {ProviderCode} not found or inactive", providerCode);
 089            throw new KeyNotFoundException($"Resource provider '{providerCode}' not found or inactive");
 90        }
 91
 092        _logger.LogDebugSanitized(
 093            "User {UserId} {Action} provider {ProviderCode} for world {WorldId}",
 094            userId,
 095            enabled ? "enabled" : "disabled",
 096            providerCode,
 097            worldId);
 98
 099        return true;
 0100    }
 101}