| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Service for handwritten note API operations. |
| | | 8 | | /// Uses HttpClientExtensions for consistent error handling and logging. |
| | | 9 | | /// </summary> |
| | | 10 | | public class HandwrittenNoteApiService : IHandwrittenNoteApiService |
| | | 11 | | { |
| | | 12 | | private readonly HttpClient _http; |
| | | 13 | | private readonly ILogger<HandwrittenNoteApiService> _logger; |
| | | 14 | | |
| | | 15 | | public HandwrittenNoteApiService(HttpClient http, ILogger<HandwrittenNoteApiService> logger) |
| | | 16 | | { |
| | 20 | 17 | | _http = http; |
| | 20 | 18 | | _logger = logger; |
| | 20 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<HandwrittenNoteSaveResultDto?> SaveHandwrittenNoteAsync(Guid articleId, byte[] imageBytes) |
| | | 22 | | { |
| | | 23 | | return await _http.PostEntityAsync<HandwrittenNoteSaveResultDto>( |
| | | 24 | | $"articles/{articleId}/handwritten-note", |
| | | 25 | | new { ImageBytes = imageBytes }, |
| | | 26 | | _logger, |
| | | 27 | | $"handwritten note for article {articleId}"); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<HandwrittenNoteTranscribeResultDto?> TranscribeHandwrittenNoteAsync(Guid articleId, byte[] imageBy |
| | | 31 | | { |
| | | 32 | | return await _http.PostEntityAsync<HandwrittenNoteTranscribeResultDto>( |
| | | 33 | | $"articles/{articleId}/handwritten-note/transcribe", |
| | | 34 | | new { ImageBytes = imageBytes }, |
| | | 35 | | _logger, |
| | | 36 | | $"handwritten note transcription for article {articleId}"); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<string?> GetHandwrittenNoteUrlAsync(Guid articleId) |
| | | 40 | | { |
| | | 41 | | try |
| | | 42 | | { |
| | | 43 | | var response = await _http.GetAsync($"articles/{articleId}/handwritten-note"); |
| | | 44 | | |
| | | 45 | | if (response.StatusCode == System.Net.HttpStatusCode.NotFound || |
| | | 46 | | response.StatusCode == System.Net.HttpStatusCode.NoContent) |
| | | 47 | | { |
| | | 48 | | return null; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | if (response.IsSuccessStatusCode) |
| | | 52 | | { |
| | | 53 | | var result = await response.Content.ReadFromJsonAsync<HandwrittenNoteDownloadUrlDto>(); |
| | | 54 | | return result?.DownloadUrl; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | _logger.LogWarning("Failed to get handwritten note URL for article {ArticleId}: {StatusCode}", |
| | | 58 | | articleId, response.StatusCode); |
| | | 59 | | return null; |
| | | 60 | | } |
| | | 61 | | catch (Exception ex) |
| | | 62 | | { |
| | | 63 | | _logger.LogError(ex, "Error getting handwritten note URL for article {ArticleId}", articleId); |
| | | 64 | | return null; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task<HandwrittenNoteTranscribeResultDto?> TranscribeExistingAsync(Guid articleId) |
| | | 69 | | { |
| | | 70 | | return await _http.PostEntityAsync<HandwrittenNoteTranscribeResultDto>( |
| | | 71 | | $"articles/{articleId}/handwritten-note/transcribe-existing", |
| | | 72 | | new { }, |
| | | 73 | | _logger, |
| | | 74 | | $"transcribe existing handwritten note for article {articleId}"); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public async Task<bool> DeleteHandwrittenNoteAsync(Guid articleId) |
| | | 78 | | { |
| | | 79 | | return await _http.DeleteEntityAsync( |
| | | 80 | | $"articles/{articleId}/handwritten-note", |
| | | 81 | | _logger, |
| | | 82 | | $"handwritten note for article {articleId}"); |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Internal DTO for deserializing the download URL response from the GET endpoint. |
| | | 88 | | /// </summary> |
| | | 89 | | internal class HandwrittenNoteDownloadUrlDto |
| | | 90 | | { |
| | | 91 | | public string DownloadUrl { get; set; } = string.Empty; |
| | | 92 | | } |