@page "/sysadmin/manage-tours"
@using System.ComponentModel.DataAnnotations
@using TIAM.Entities.Transfers
@using TIAMWebApp.Shared.Application.Interfaces
@using TIAMWebApp.Shared.Application.Models
@using TIAMWebApp.Shared.Application.Services
@using TIAMSharedUI.Shared
@inject ITransferDataService TransferDataService
@inject TourService TourService
@inject NavigationManager Navigation
@layout AdminLayout
Manage Tours
Manage Tours
Create New Tour
@foreach (var dest in TransferDestinations)
{
}
@if (!string.IsNullOrEmpty(UploadStatus))
{
@UploadStatus
}
@if (IsEditing)
{
}
Existing Tours
@if (Tours.Any())
{
@foreach (var tour in Tours)
{
var dest = TransferDestinations.FirstOrDefault(d => d.Id == tour.TransferDestinationId);
-
@tour?.Title — @tour.FancyDescription
}
}
else
{
No tours available.
}
@code {
private List TransferDestinations = [];
private List Tours = [];
private TourFormModel newTour = new();
private IBrowserFile? uploadedFile;
private string UploadStatus = string.Empty;
private bool IsEditing = false;
private Guid? EditingTourId = null;
protected override async Task OnInitializedAsync()
{
TransferDestinations = (await TransferDataService.GetDestinationsAsync()).ToList();
Tours = (await TourService.GetAllAsync()).ToList();
}
private void EditTour(TourInfo tour)
{
newTour = new TourFormModel
{
TransferDestinationId = tour.TransferDestinationId,
Name = tour.Title,
Description = tour.FancyDescription,
ImageBytes = null,
ImageFileName = null
};
IsEditing = true;
EditingTourId = tour.Id;
UploadStatus = string.Empty;
}
private void CancelEdit()
{
newTour = new TourFormModel();
uploadedFile = null;
IsEditing = false;
EditingTourId = null;
UploadStatus = string.Empty;
}
// private async Task SaveTourAsync()
// {
// if (uploadedFile is not null)
// {
// using var stream = uploadedFile.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
// using var ms = new MemoryStream();
// await stream.CopyToAsync(ms);
// newTour.ImageBytes = ms.ToArray();
// newTour.ImageFileName = uploadedFile.Name;
// }
// TourInfo tourToCreate = new TourInfo();
// tourToCreate.TransferDestinationId = newTour.TransferDestinationId;
// tourToCreate.FancyDescription = newTour.Description;
// tourToCreate.Title = newTour.Name;
// tourToCreate.CoverImageUrl = "";
// if (tourToCreate.TransferDestinationId == Guid.Empty)
// {
// throw new InvalidOperationException("Transfer Destination must be selected.");
// }
// await TourService.CreateAsync(tourToCreate, uploadedFile);
// newTour = new();
// uploadedFile = null;
// UploadStatus = string.Empty;
// Tours = (await TourService.GetAllAsync()).ToList();
// }
private async Task HandleSubmit()
{
if (uploadedFile is not null)
{
using var stream = uploadedFile.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
using var ms = new MemoryStream();
await stream.CopyToAsync(ms);
newTour.ImageBytes = ms.ToArray();
newTour.ImageFileName = uploadedFile.Name;
}
if (IsEditing && EditingTourId.HasValue)
{
var updatedTour = new TourInfo
{
Id = EditingTourId.Value,
TransferDestinationId = newTour.TransferDestinationId,
Title = newTour.Name,
FancyDescription = newTour.Description,
CoverImageUrl = "" // assume your service handles image URL logic
};
await TourService.UpdateAsync(updatedTour, uploadedFile);
}
else
{
var tourToCreate = new TourInfo
{
TransferDestinationId = newTour.TransferDestinationId,
Title = newTour.Name,
FancyDescription = newTour.Description,
CoverImageUrl = newTour.ImageFileName
};
await TourService.CreateAsync(tourToCreate, uploadedFile);
}
// Reset form
newTour = new TourFormModel();
uploadedFile = null;
IsEditing = false;
EditingTourId = null;
UploadStatus = string.Empty;
Tours = (await TourService.GetAllAsync()).ToList();
}
private void HandleFileChange(InputFileChangeEventArgs e)
{
uploadedFile = e.File;
UploadStatus = $"Selected: {uploadedFile.Name}";
}
private async Task DeleteTourAsync(Guid id)
{
await TourService.DeleteAsync(id);
Tours = (await TourService.GetAllAsync()).ToList();
}
public class TourFormModel
{
[Required]
public Guid TransferDestinationId { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Tour name cannot exceed 100 characters.")]
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public byte[]? ImageBytes { get; set; }
public string? ImageFileName { get; set; }
}
}