| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Service for managing resource providers via the API. |
| | | 8 | | /// </summary> |
| | | 9 | | public class ResourceProviderApiService : IResourceProviderApiService |
| | | 10 | | { |
| | | 11 | | private readonly HttpClient _httpClient; |
| | | 12 | | private readonly ILogger<ResourceProviderApiService> _logger; |
| | | 13 | | |
| | 0 | 14 | | public ResourceProviderApiService( |
| | 0 | 15 | | HttpClient httpClient, |
| | 0 | 16 | | ILogger<ResourceProviderApiService> logger) |
| | | 17 | | { |
| | 0 | 18 | | _httpClient = httpClient; |
| | 0 | 19 | | _logger = logger; |
| | 0 | 20 | | } |
| | | 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 | | { |
| | 0 | 31 | | var response = await _httpClient.GetAsync($"api/worlds/{worldId}/resource-providers"); |
| | | 32 | | |
| | 0 | 33 | | if (response.IsSuccessStatusCode) |
| | | 34 | | { |
| | 0 | 35 | | return await response.Content.ReadFromJsonAsync<List<WorldResourceProviderDto>>(); |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | _logger.LogWarning( |
| | 0 | 39 | | "Failed to get providers for world {WorldId}. Status: {StatusCode}", |
| | 0 | 40 | | worldId, |
| | 0 | 41 | | response.StatusCode); |
| | 0 | 42 | | return null; |
| | | 43 | | } |
| | 0 | 44 | | catch (Exception ex) |
| | | 45 | | { |
| | 0 | 46 | | _logger.LogError(ex, "Error getting providers for world {WorldId}", worldId); |
| | 0 | 47 | | return null; |
| | | 48 | | } |
| | 0 | 49 | | } |
| | | 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 | | { |
| | 0 | 62 | | var request = new ToggleResourceProviderRequestDto { Enabled = enabled }; |
| | 0 | 63 | | var response = await _httpClient.PostAsJsonAsync( |
| | 0 | 64 | | $"api/worlds/{worldId}/resource-providers/{providerCode}/toggle", |
| | 0 | 65 | | request); |
| | | 66 | | |
| | 0 | 67 | | if (response.IsSuccessStatusCode) |
| | | 68 | | { |
| | 0 | 69 | | _logger.LogDebug( |
| | 0 | 70 | | "Successfully {Action} provider {ProviderCode} for world {WorldId}", |
| | 0 | 71 | | enabled ? "enabled" : "disabled", |
| | 0 | 72 | | providerCode, |
| | 0 | 73 | | worldId); |
| | 0 | 74 | | return true; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | _logger.LogWarning( |
| | 0 | 78 | | "Failed to toggle provider {ProviderCode} for world {WorldId}. Status: {StatusCode}", |
| | 0 | 79 | | providerCode, |
| | 0 | 80 | | worldId, |
| | 0 | 81 | | response.StatusCode); |
| | 0 | 82 | | return false; |
| | | 83 | | } |
| | 0 | 84 | | catch (Exception ex) |
| | | 85 | | { |
| | 0 | 86 | | _logger.LogError( |
| | 0 | 87 | | ex, |
| | 0 | 88 | | "Error toggling provider {ProviderCode} for world {WorldId}", |
| | 0 | 89 | | providerCode, |
| | 0 | 90 | | worldId); |
| | 0 | 91 | | return false; |
| | | 92 | | } |
| | 0 | 93 | | } |
| | | 94 | | } |