< Summary

Information
Class: Chronicis.Client.Services.ResourceProviderApiService
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/ResourceProviderApiService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 43
Coverable lines: 43
Total lines: 94
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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%
GetWorldProvidersAsync()0%620%
ToggleProviderAsync()0%2040%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/ResourceProviderApiService.cs

#LineLine coverage
 1using System.Net.Http.Json;
 2using Chronicis.Shared.DTOs;
 3
 4namespace Chronicis.Client.Services;
 5
 6/// <summary>
 7/// Service for managing resource providers via the API.
 8/// </summary>
 9public class ResourceProviderApiService : IResourceProviderApiService
 10{
 11    private readonly HttpClient _httpClient;
 12    private readonly ILogger<ResourceProviderApiService> _logger;
 13
 014    public ResourceProviderApiService(
 015        HttpClient httpClient,
 016        ILogger<ResourceProviderApiService> logger)
 17    {
 018        _httpClient = httpClient;
 019        _logger = logger;
 020    }
 21
 22    /// <summary>
 23    /// Gets all resource providers with their enabled status for a specific world.
 24    /// </summary>
 25    /// <param name="worldId">The world ID</param>
 26    /// <returns>List of providers with enabled status</returns>
 27    public async Task<List<WorldResourceProviderDto>?> GetWorldProvidersAsync(Guid worldId)
 28    {
 29        try
 30        {
 031            var response = await _httpClient.GetAsync($"api/worlds/{worldId}/resource-providers");
 32
 033            if (response.IsSuccessStatusCode)
 34            {
 035                return await response.Content.ReadFromJsonAsync<List<WorldResourceProviderDto>>();
 36            }
 37
 038            _logger.LogWarning(
 039                "Failed to get providers for world {WorldId}. Status: {StatusCode}",
 040                worldId,
 041                response.StatusCode);
 042            return null;
 43        }
 044        catch (Exception ex)
 45        {
 046            _logger.LogError(ex, "Error getting providers for world {WorldId}", worldId);
 047            return null;
 48        }
 049    }
 50
 51    /// <summary>
 52    /// Enables or disables a resource provider for a specific world.
 53    /// </summary>
 54    /// <param name="worldId">The world ID</param>
 55    /// <param name="providerCode">The provider code</param>
 56    /// <param name="enabled">Whether to enable or disable</param>
 57    /// <returns>True if successful, false otherwise</returns>
 58    public async Task<bool> ToggleProviderAsync(Guid worldId, string providerCode, bool enabled)
 59    {
 60        try
 61        {
 062            var request = new ToggleResourceProviderRequestDto { Enabled = enabled };
 063            var response = await _httpClient.PostAsJsonAsync(
 064                $"api/worlds/{worldId}/resource-providers/{providerCode}/toggle",
 065                request);
 66
 067            if (response.IsSuccessStatusCode)
 68            {
 069                _logger.LogDebug(
 070                    "Successfully {Action} provider {ProviderCode} for world {WorldId}",
 071                    enabled ? "enabled" : "disabled",
 072                    providerCode,
 073                    worldId);
 074                return true;
 75            }
 76
 077            _logger.LogWarning(
 078                "Failed to toggle provider {ProviderCode} for world {WorldId}. Status: {StatusCode}",
 079                providerCode,
 080                worldId,
 081                response.StatusCode);
 082            return false;
 83        }
 084        catch (Exception ex)
 85        {
 086            _logger.LogError(
 087                ex,
 088                "Error toggling provider {ProviderCode} for world {WorldId}",
 089                providerCode,
 090                worldId);
 091            return false;
 92        }
 093    }
 94}