using System.Net.Http.Json;
using RobotNet10.FleetManager.Shared.DTOs.Robot;
using RobotNet10.FleetManager.Shared.DTOs.Responses;
namespace RobotNet10.FleetManager.Client.Services;
///
/// API service for robot operations.
/// Provides methods to interact with the robot API endpoints.
///
public class RobotApiService
{
private readonly HttpClient _httpClient;
private readonly ILogger? _logger;
public RobotApiService(HttpClient httpClient, ILogger? logger = null)
{
_httpClient = httpClient;
_logger = logger;
}
///
/// Get all robots with optional filters
///
public async Task> GetAllAsync(Guid? modelId = null, Guid? mapId = null)
{
try
{
var queryParams = new List();
if (modelId.HasValue)
queryParams.Add($"modelId={modelId.Value}");
if (mapId.HasValue)
queryParams.Add($"mapId={mapId.Value}");
var queryString = queryParams.Count > 0 ? "?" + string.Join("&", queryParams) : "";
var response = await _httpClient.GetAsync($"/api/robots{queryString}");
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync>() ?? new List();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error getting all robots");
throw;
}
}
///
/// Get robot by ID
///
public async Task GetByIdAsync(Guid id)
{
try
{
var response = await _httpClient.GetAsync($"/api/robots/{id}");
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error getting robot {Id}", id);
throw;
}
}
///
/// Get robot by RobotId (string identifier)
///
public async Task GetByRobotIdAsync(string robotId)
{
try
{
var response = await _httpClient.GetAsync($"/api/robots/robotId/{Uri.EscapeDataString(robotId)}");
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error getting robot with RobotId {RobotId}", robotId);
throw;
}
}
///
/// Search robots
///
public async Task> SearchAsync(string query)
{
try
{
var response = await _httpClient.GetAsync($"/api/robots/search?query={Uri.EscapeDataString(query)}");
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync>() ?? new List();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error searching robots with query {Query}", query);
throw;
}
}
///
/// Get all robots by model ID
///
public async Task> GetByModelIdAsync(Guid modelId)
{
try
{
var response = await _httpClient.GetAsync($"/api/robots/model/{modelId}");
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync>() ?? new List();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error getting robots by model {ModelId}", modelId);
throw;
}
}
///
/// Create a new robot
///
public async Task CreateAsync(CreateRobotRequest request)
{
try
{
var response = await _httpClient.PostAsJsonAsync("/api/robots", request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync()
?? throw new InvalidOperationException("Failed to deserialize created robot");
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error creating robot");
throw;
}
}
///
/// Update an existing robot
///
public async Task UpdateAsync(Guid id, UpdateRobotRequest request)
{
try
{
var response = await _httpClient.PutAsJsonAsync($"/api/robots/{id}", request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync()
?? throw new InvalidOperationException("Failed to deserialize updated robot");
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error updating robot {Id}", id);
throw;
}
}
///
/// Delete a robot
///
public async Task DeleteAsync(Guid id)
{
try
{
var response = await _httpClient.DeleteAsync($"/api/robots/{id}");
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error deleting robot {Id}", id);
throw;
}
}
}