Initial commit
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
using System.Net.Http.Json;
|
||||
using RobotNet10.FleetManager.Shared.DTOs.Robot;
|
||||
using RobotNet10.FleetManager.Shared.DTOs.Responses;
|
||||
|
||||
namespace RobotNet10.FleetManager.Client.Services;
|
||||
|
||||
/// <summary>
|
||||
/// API service for robot operations.
|
||||
/// Provides methods to interact with the robot API endpoints.
|
||||
/// </summary>
|
||||
public class RobotApiService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<RobotApiService>? _logger;
|
||||
|
||||
public RobotApiService(HttpClient httpClient, ILogger<RobotApiService>? logger = null)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all robots with optional filters
|
||||
/// </summary>
|
||||
public async Task<List<RobotDto>> GetAllAsync(Guid? modelId = null, Guid? mapId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var queryParams = new List<string>();
|
||||
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<List<RobotDto>>() ?? new List<RobotDto>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error getting all robots");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get robot by ID
|
||||
/// </summary>
|
||||
public async Task<RobotDto?> 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<RobotDto>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error getting robot {Id}", id);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get robot by RobotId (string identifier)
|
||||
/// </summary>
|
||||
public async Task<RobotDto?> 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<RobotDto>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error getting robot with RobotId {RobotId}", robotId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Search robots
|
||||
/// </summary>
|
||||
public async Task<List<RobotDto>> SearchAsync(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"/api/robots/search?query={Uri.EscapeDataString(query)}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<List<RobotDto>>() ?? new List<RobotDto>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error searching robots with query {Query}", query);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all robots by model ID
|
||||
/// </summary>
|
||||
public async Task<List<RobotDto>> GetByModelIdAsync(Guid modelId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"/api/robots/model/{modelId}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<List<RobotDto>>() ?? new List<RobotDto>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error getting robots by model {ModelId}", modelId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new robot
|
||||
/// </summary>
|
||||
public async Task<RobotDto> CreateAsync(CreateRobotRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync("/api/robots", request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<RobotDto>()
|
||||
?? throw new InvalidOperationException("Failed to deserialize created robot");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error creating robot");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update an existing robot
|
||||
/// </summary>
|
||||
public async Task<RobotDto> UpdateAsync(Guid id, UpdateRobotRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PutAsJsonAsync($"/api/robots/{id}", request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<RobotDto>()
|
||||
?? throw new InvalidOperationException("Failed to deserialize updated robot");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error updating robot {Id}", id);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete a robot
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user