using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Kiota.Abstractions.Serialization; namespace Marechai.App.Services; /// /// Service for fetching and managing computers from the Marechai API /// public class ComputersService { private readonly ApiClient _apiClient; private readonly ILogger _logger; public ComputersService(ApiClient apiClient, ILogger logger) { _apiClient = apiClient; _logger = logger; } /// /// Fetches the total count of computers from the API /// /// Total number of computers, or 0 if API call fails public async Task GetComputersCountAsync() { try { _logger.LogInformation("Fetching computers count from API"); UntypedNode result = await _apiClient.Computers.Count.GetAsync(); // Extract integer value from UntypedNode // UntypedNode wraps a JsonElement, we need to parse it int count = ExtractIntFromUntypedNode(result); _logger.LogInformation("Successfully fetched computers count: {Count}", count); return count; } catch(Exception ex) { _logger.LogError(ex, "Error fetching computers count from API"); return 0; } } /// /// Fetches the minimum year of computers from the API /// /// Minimum year, or 0 if API call fails public async Task GetMinimumYearAsync() { try { _logger.LogInformation("Fetching minimum year from API"); UntypedNode result = await _apiClient.Computers.MinimumYear.GetAsync(); // Extract integer value from UntypedNode int year = ExtractIntFromUntypedNode(result); _logger.LogInformation("Successfully fetched minimum year: {Year}", year); return year; } catch(Exception ex) { _logger.LogError(ex, "Error fetching minimum year from API"); return 0; } } /// /// Fetches the maximum year of computers from the API /// /// Maximum year, or 0 if API call fails public async Task GetMaximumYearAsync() { try { _logger.LogInformation("Fetching maximum year from API"); UntypedNode result = await _apiClient.Computers.MaximumYear.GetAsync(); // Extract integer value from UntypedNode int year = ExtractIntFromUntypedNode(result); _logger.LogInformation("Successfully fetched maximum year: {Year}", year); return year; } catch(Exception ex) { _logger.LogError(ex, "Error fetching maximum year from API"); return 0; } } /// /// Helper method to extract an integer from an UntypedNode /// private int ExtractIntFromUntypedNode(UntypedNode node) { if(node == null) return 0; try { // Cast to UntypedInteger to access the Value property if(node is UntypedInteger intNode) return intNode.GetValue(); // Fallback: try to parse ToString() result var stringValue = node.ToString(); if(!string.IsNullOrWhiteSpace(stringValue) && int.TryParse(stringValue, out int result)) return result; return 0; } catch { return 0; } } /// /// Fetches computers filtered by starting letter from the API /// public async Task> GetComputersByLetterAsync(char letter) { try { _logger.LogInformation("Fetching computers starting with '{Letter}' from API", letter); List computers = await _apiClient.Computers.ByLetter[letter.ToString()].GetAsync(); if(computers == null) return new List(); _logger.LogInformation("Successfully fetched {Count} computers starting with '{Letter}'", computers.Count, letter); return computers; } catch(Exception ex) { _logger.LogError(ex, "Error fetching computers by letter '{Letter}' from API", letter); return new List(); } } /// /// Fetches computers filtered by year from the API /// public async Task> GetComputersByYearAsync(int year) { try { _logger.LogInformation("Fetching computers from year {Year} from API", year); List computers = await _apiClient.Computers.ByYear[year].GetAsync(); if(computers == null) return new List(); _logger.LogInformation("Successfully fetched {Count} computers from year {Year}", computers.Count, year); return computers; } catch(Exception ex) { _logger.LogError(ex, "Error fetching computers by year {Year} from API", year); return new List(); } } /// /// Fetches all computers from the API /// public async Task> GetAllComputersAsync() { try { _logger.LogInformation("Fetching all computers from API"); List computers = await _apiClient.Computers.GetAsync(); if(computers == null) return new List(); _logger.LogInformation("Successfully fetched {Count} total computers", computers.Count); return computers; } catch(Exception ex) { _logger.LogError(ex, "Error fetching all computers from API"); return new List(); } } }