using Microsoft.AspNetCore.Components.Forms; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text; using System.Threading.Tasks; using TIAMWebApp.Shared.Application.Models; namespace TIAMWebApp.Shared.Application.Services { public class TourService { private readonly HttpClient _http; public TourService(HttpClient http) { _http = http; } public async Task> GetAllAsync() => await _http.GetFromJsonAsync>("/api/tours") ?? new(); public async Task GetAsync(Guid id) => await _http.GetFromJsonAsync($"/api/tours/{id}"); public async Task CreateAsync(TourInfo tour, IBrowserFile? coverImage) { var content = new MultipartFormDataContent { { new StringContent(tour.TransferDestinationId.ToString()), nameof(TourForm.TransferDestinationId) }, { new StringContent(tour.Title ?? ""), nameof(TourForm.Title) }, { new StringContent(tour.FancyDescription ?? ""), nameof(TourForm.Description) } }; if (coverImage != null) { var stream = coverImage.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024); // adjust limit if needed var fileContent = new StreamContent(stream); fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(coverImage.ContentType); content.Add(fileContent, nameof(TourForm.CoverImage), coverImage.Name); } var response = await _http.PostAsync("/api/tours", content); response.EnsureSuccessStatusCode(); } public async Task UpdateAsync(TourInfo tour, IBrowserFile? file) { // Step 1: Update metadata var response = await _http.PutAsJsonAsync($"/api/tours/{tour.TransferDestinationId}", tour); response.EnsureSuccessStatusCode(); // Step 2: Upload new image if provided if (file is not null) { using var content = new MultipartFormDataContent(); var fileContent = new StreamContent(file.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024)); fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType); content.Add(content: fileContent, name: "file", fileName: file.Name); var uploadResponse = await _http.PostAsync($"/api/tours/{tour.TransferDestinationId}/image", content); uploadResponse.EnsureSuccessStatusCode(); } } public async Task DeleteAsync(Guid id) { var response = await _http.DeleteAsync($"/api/tours/{id}"); response.EnsureSuccessStatusCode(); } } }