2026-12-02 13:50:01 +01:00
|
|
|
|
using System.Globalization;
|
2026-12-02 17:34:58 +01:00
|
|
|
|
using System.Net.Http.Headers;
|
2026-12-02 00:26:42 +01:00
|
|
|
|
using System.Net.Http.Json;
|
2026-12-02 13:50:01 +01:00
|
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
2026-12-02 00:26:42 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Web.Api;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Endpointy API pro přihlášeného uživatele; access token přidává <see cref="BearerTokenHandler"/>.</summary>
|
|
|
|
|
|
public sealed class HelpdeskApiClient(HttpClient http)
|
|
|
|
|
|
{
|
2026-12-02 00:31:07 +01:00
|
|
|
|
public async Task<UserDto> GetMeAsync(CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.GetAsync("users/me", ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<UserDto>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<UserDto> UpdateMeAsync(UpdateProfileRequest request, CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PutAsJsonAsync("users/me", request, ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<UserDto>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 13:50:01 +01:00
|
|
|
|
public async Task<PagedResult<IssueSummary>> GetIssuesAsync(IssueListQuery query, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
var parameters = new Dictionary<string, string?>
|
|
|
|
|
|
{
|
|
|
|
|
|
["status"] = query.Status,
|
|
|
|
|
|
["mine"] = query.Mine ? "true" : null,
|
|
|
|
|
|
["sort"] = query.Sort,
|
|
|
|
|
|
["page"] = query.Page.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
|
["pageSize"] = query.PageSize.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
|
};
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.GetAsync(QueryHelpers.AddQueryString("issues", parameters.Where(p => !string.IsNullOrEmpty(p.Value))), ct);
|
2026-12-02 13:50:01 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<PagedResult<IssueSummary>>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 17:34:58 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Nahlásí závadu i s přílohami jedním požadavkem (multipart). Soubory se z formuláře
|
|
|
|
|
|
/// posílají rovnou dál jako proud, web je celé nenačítá do paměti.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<IssueDetail> CreateIssueAsync(CreateIssueRequest request, IReadOnlyCollection<UploadFile> files, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
using var content = new MultipartFormDataContent
|
|
|
|
|
|
{
|
|
|
|
|
|
{ new StringContent(request.Title), "title" },
|
|
|
|
|
|
{ new StringContent(request.Description), "description" },
|
|
|
|
|
|
{ new StringContent(request.CategoryId.ToString(CultureInfo.InvariantCulture)), "categoryId" },
|
|
|
|
|
|
};
|
|
|
|
|
|
if (request.BuildingId is { } buildingId)
|
|
|
|
|
|
content.Add(new StringContent(buildingId.ToString(CultureInfo.InvariantCulture)), "buildingId");
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.LocationDetail))
|
|
|
|
|
|
content.Add(new StringContent(request.LocationDetail), "locationDetail");
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
var part = new StreamContent(file.OpenRead());
|
|
|
|
|
|
part.Headers.ContentType = MediaTypeHeaderValue.TryParse(file.ContentType, out var type)
|
|
|
|
|
|
? type
|
|
|
|
|
|
: new MediaTypeHeaderValue("application/octet-stream");
|
|
|
|
|
|
content.Add(part, "files", file.FileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsync("issues", content, ct);
|
2026-12-02 17:34:58 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 13:50:01 +01:00
|
|
|
|
public async Task<IReadOnlyList<CategoryDto>> GetIssueCategoriesAsync(CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.GetAsync("issue-categories", ct);
|
2026-12-02 13:50:01 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<List<CategoryDto>>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<BuildingDto>> GetBuildingsAsync(CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.GetAsync("buildings", ct);
|
2026-12-02 13:50:01 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<List<BuildingDto>>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 00:31:07 +01:00
|
|
|
|
/// <summary>Odhlásí ze všech zařízení – API zneplatní všechny tokeny uživatele.</summary>
|
|
|
|
|
|
public async Task LogoutAllAsync(CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsync("auth/logout-all", content: null, ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 00:26:42 +01:00
|
|
|
|
/// <summary>Odhlásí toto zařízení – API zneplatní aktuální access token i předaný refresh token.</summary>
|
|
|
|
|
|
public async Task LogoutAsync(string refreshToken, CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsJsonAsync("auth/logout", new RefreshTokenRequest(refreshToken), ct);
|
2026-12-02 00:26:42 +01:00
|
|
|
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|