Utopia-Canteen-System/Services/BackendApiMealScheduleServi...

49 lines
1.9 KiB
C#

using UtopiaCanteen.Shared;
using UtopiaCanteenSystem.Api;
using UtopiaCanteenSystem.Models;
namespace UtopiaCanteenSystem.Services;
/// <summary>
/// Meal schedule CRUD via central backend API (client never touches HRMS/MySQL).
/// </summary>
public sealed class BackendApiMealScheduleService : IMealScheduleService
{
private readonly ICanteenBackendApiClient _api;
public BackendApiMealScheduleService(ICanteenBackendApiClient api)
{
_api = api;
}
public IReadOnlyList<MealSchedule> GetActiveSchedulesForSite(string siteId)
{
var all = GetAllSchedulesAsync().GetAwaiter().GetResult();
if (string.IsNullOrWhiteSpace(siteId))
return all;
var site = siteId.Trim().Replace("SITE : ", "", StringComparison.OrdinalIgnoreCase).Trim();
return all.Where(s => string.Equals(s.LocationSiteId?.Trim(), site, StringComparison.OrdinalIgnoreCase)).ToList();
}
public async Task<IReadOnlyList<MealSchedule>> GetAllSchedulesAsync(CancellationToken cancellationToken = default)
{
var dtos = await _api.GetMealSchedulesAsync(cancellationToken).ConfigureAwait(false);
return dtos.Select(ApiDtoMapper.ToMealSchedule).ToList();
}
public async Task<long> CreateAsync(MealSchedule schedule, CancellationToken cancellationToken = default)
{
var dto = ApiDtoMapper.ToMealScheduleDto(schedule);
return await _api.CreateMealScheduleAsync(dto, cancellationToken).ConfigureAwait(false);
}
public async Task UpdateAsync(MealSchedule schedule, CancellationToken cancellationToken = default)
{
var dto = ApiDtoMapper.ToMealScheduleDto(schedule);
await _api.UpdateMealScheduleAsync(dto, cancellationToken).ConfigureAwait(false);
}
public async Task DeleteAsync(long id, CancellationToken cancellationToken = default) =>
await _api.DeleteMealScheduleAsync(id, cancellationToken).ConfigureAwait(false);
}