Files
nimbusflow/frontend/Services/ApiService.cs

182 lines
8.0 KiB
C#

using System.Text;
using System.Text.Json;
using NimbusFlow.Frontend.Models;
namespace NimbusFlow.Frontend.Services
{
public class ApiService : IApiService
{
private readonly HttpClient _httpClient;
private readonly JsonSerializerOptions _jsonOptions;
public ApiService(HttpClient httpClient)
{
_httpClient = httpClient;
_jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
}
// Member operations
public async Task<List<Member>> GetMembersAsync()
{
var response = await _httpClient.GetAsync("members");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<Member>>(json, _jsonOptions) ?? new List<Member>();
}
public async Task<Member?> GetMemberAsync(int id)
{
var response = await _httpClient.GetAsync($"members/{id}");
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
return null;
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Member>(json, _jsonOptions);
}
public async Task<Member> CreateMemberAsync(Member member)
{
var json = JsonSerializer.Serialize(member, _jsonOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("members", content);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Member>(responseJson, _jsonOptions) ?? member;
}
public async Task<Member> UpdateMemberAsync(Member member)
{
var json = JsonSerializer.Serialize(member, _jsonOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PutAsync($"members/{member.MemberId}", content);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Member>(responseJson, _jsonOptions) ?? member;
}
public async Task<bool> DeleteMemberAsync(int id)
{
var response = await _httpClient.DeleteAsync($"members/{id}");
return response.IsSuccessStatusCode;
}
// Classification operations
public async Task<List<Classification>> GetClassificationsAsync()
{
var response = await _httpClient.GetAsync("classifications");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<Classification>>(json, _jsonOptions) ?? new List<Classification>();
}
// Service operations
public async Task<List<Service>> GetServicesAsync()
{
var response = await _httpClient.GetAsync("services");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<Service>>(json, _jsonOptions) ?? new List<Service>();
}
public async Task<Service?> GetServiceAsync(int id)
{
var response = await _httpClient.GetAsync($"services/{id}");
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
return null;
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Service>(json, _jsonOptions);
}
public async Task<Service> CreateServiceAsync(Service service)
{
var json = JsonSerializer.Serialize(service, _jsonOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("services", content);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Service>(responseJson, _jsonOptions) ?? service;
}
// Schedule operations
public async Task<List<Schedule>> GetSchedulesAsync()
{
var response = await _httpClient.GetAsync("schedules");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<Schedule>>(json, _jsonOptions) ?? new List<Schedule>();
}
public async Task<List<Schedule>> GetMemberSchedulesAsync(int memberId)
{
var response = await _httpClient.GetAsync($"members/{memberId}/schedules");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<Schedule>>(json, _jsonOptions) ?? new List<Schedule>();
}
public async Task<Schedule?> GetScheduleAsync(int id)
{
var response = await _httpClient.GetAsync($"schedules/{id}");
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
return null;
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Schedule>(json, _jsonOptions);
}
public async Task<Schedule> AcceptScheduleAsync(int scheduleId)
{
var response = await _httpClient.PostAsync($"schedules/{scheduleId}/accept", null);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Schedule>(responseJson, _jsonOptions) ?? new Schedule();
}
public async Task<Schedule> DeclineScheduleAsync(int scheduleId, string? reason = null)
{
var content = new StringContent(
JsonSerializer.Serialize(new { reason }, _jsonOptions),
Encoding.UTF8,
"application/json"
);
var response = await _httpClient.PostAsync($"schedules/{scheduleId}/decline", content);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Schedule>(responseJson, _jsonOptions) ?? new Schedule();
}
public async Task<bool> RemoveScheduleAsync(int scheduleId)
{
var response = await _httpClient.DeleteAsync($"schedules/{scheduleId}");
return response.IsSuccessStatusCode;
}
public async Task<Schedule?> ScheduleNextMemberAsync(int serviceId, List<int> classificationIds)
{
var content = new StringContent(
JsonSerializer.Serialize(new { serviceId, classificationIds }, _jsonOptions),
Encoding.UTF8,
"application/json"
);
var response = await _httpClient.PostAsync("schedules/schedule-next", content);
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
return null;
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Schedule>(responseJson, _jsonOptions);
}
public async Task<List<ServiceType>> GetServiceTypesAsync()
{
var response = await _httpClient.GetAsync("service-types");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<ServiceType>>(json, _jsonOptions) ?? new List<ServiceType>();
}
}
}