66 lines
2.1 KiB
C#
66 lines
2.1 KiB
C#
using System.Net.Http.Json;
|
|
using timetracker.Shared;
|
|
|
|
namespace timetracker.Client.Services;
|
|
|
|
public class ClientTimetrackerService : ITimetrackerService
|
|
{
|
|
private readonly HttpClient _http;
|
|
|
|
public ClientTimetrackerService(HttpClient http)
|
|
{
|
|
_http = http;
|
|
}
|
|
|
|
public async Task<List<WorkDay>> GetWeekAsync(int userId, DateOnly monday)
|
|
{
|
|
return await _http.GetFromJsonAsync<List<WorkDay>>($"api/tracker/week?userId={userId}&monday={monday:yyyy-MM-dd}") ?? [];
|
|
}
|
|
|
|
public async Task UpsertWorkDayAsync(WorkDay workDay)
|
|
{
|
|
await _http.PostAsJsonAsync("api/tracker/workday", workDay);
|
|
}
|
|
|
|
public async Task<AppSettings> GetSettingsAsync(int userId)
|
|
{
|
|
return await _http.GetFromJsonAsync<AppSettings>($"api/tracker/settings/{userId}") ?? new AppSettings { UserId = userId };
|
|
}
|
|
|
|
public async Task SaveSettingsAsync(AppSettings settings)
|
|
{
|
|
await _http.PostAsJsonAsync("api/tracker/settings", settings);
|
|
}
|
|
|
|
public async Task<List<VacationDay>> GetVacationDaysAsync(int userId, int year)
|
|
{
|
|
return await _http.GetFromJsonAsync<List<VacationDay>>($"api/tracker/vacation/{userId}/{year}") ?? [];
|
|
}
|
|
|
|
public async Task AddVacationDayAsync(VacationDay vacationDay)
|
|
{
|
|
await _http.PostAsJsonAsync("api/tracker/vacation", vacationDay);
|
|
}
|
|
|
|
public async Task RemoveVacationDayAsync(int userId, int id)
|
|
{
|
|
await _http.DeleteAsync($"api/tracker/vacation/{userId}/{id}");
|
|
}
|
|
|
|
public async Task<TimeSpan> GetTotalOvertimeAsync(int userId, AppSettings settings)
|
|
{
|
|
var response = await _http.PostAsJsonAsync($"api/tracker/overtime/{userId}", settings);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var hours = await response.Content.ReadFromJsonAsync<double>();
|
|
return TimeSpan.FromHours(hours);
|
|
}
|
|
return TimeSpan.Zero;
|
|
}
|
|
|
|
public async Task<List<WorkDay>> GetMonthAsync(int userId, int year, int month)
|
|
{
|
|
return await _http.GetFromJsonAsync<List<WorkDay>>($"api/tracker/month?userId={userId}&year={year}&month={month}") ?? [];
|
|
}
|
|
}
|