using GoogleApi.Entities.Maps.Routes.Common; using Microsoft.AspNetCore.Mvc; using System.Text.Json; using TIAM.Entities.Transfers; using TIAMWebApp.Shared.Application.Models; namespace TIAMWebApp.Server.Controllers; [ApiController] [Route("api/tours")] public class TourAPIController : ControllerBase { private readonly IWebHostEnvironment _env; private readonly string _toursPath; private readonly string _coverPath; public TourAPIController(IWebHostEnvironment env) { _env = env; _toursPath = Path.Combine(_env.WebRootPath, "data", "tours"); _coverPath = Path.Combine(_env.WebRootPath, "uploads", "tourcovers"); Directory.CreateDirectory(_toursPath); Directory.CreateDirectory(_coverPath); } [HttpGet] public ActionResult> GetAll() { var files = Directory.GetFiles(_toursPath, "*.json"); var tours = files.Select(file => { var json = System.IO.File.ReadAllText(file); return JsonSerializer.Deserialize(json); }).Where(t => t != null).ToList(); return Ok(tours); } [HttpGet("{transferDestinationId}")] public ActionResult Get(Guid transferDestinationId) { var path = GetPath(transferDestinationId); if (!System.IO.File.Exists(path)) return NotFound(); var json = System.IO.File.ReadAllText(path); var tour = JsonSerializer.Deserialize(json); return Ok(tour); } [HttpPost] public async Task Create([FromForm] TourForm form) { var tour = new TourInfo { TransferDestinationId = Guid.Parse(form.TransferDestinationId), Title = form.Title, FancyDescription = form.Description, Created = DateTime.UtcNow, Modified = DateTime.UtcNow }; // Save cover image if exists if (form.CoverImage != null) { string fileName = $"{tour.TransferDestinationId}{Path.GetExtension(form.CoverImage.FileName)}"; string savePath = Path.Combine(_coverPath, fileName); Directory.CreateDirectory(_coverPath); // Ensure folder exists await using var fs = System.IO.File.Create(savePath); await form.CoverImage.CopyToAsync(fs); tour.CoverImageUrl = $"/uploads/tourcovers/{fileName}"; } // Save metadata as JSON var jsonPath = GetPath(tour.TransferDestinationId); var json = JsonSerializer.Serialize(tour, new JsonSerializerOptions { WriteIndented = true }); await System.IO.File.WriteAllTextAsync(jsonPath, json); return Ok(tour); } [HttpPut("{transferDestinationId}")] public async Task Update(Guid transferDestinationId, [FromBody] TourInfo updated) { var path = GetPath(transferDestinationId); if (!System.IO.File.Exists(path)) return NotFound(); updated.TransferDestinationId = transferDestinationId; updated.Modified = DateTime.UtcNow; var json = JsonSerializer.Serialize(updated, new JsonSerializerOptions { WriteIndented = true }); await System.IO.File.WriteAllTextAsync(path, json); return Ok(updated); } [HttpDelete("{transferDestinationId}")] public IActionResult Delete(Guid transferDestinationId) { var path = GetPath(transferDestinationId); if (!System.IO.File.Exists(path)) return NotFound(); System.IO.File.Delete(path); return NoContent(); } //[HttpPost("{id}/image")] //public async Task UploadTourImage(Guid id, IFormFile file) //{ // if (file == null || file.Length == 0) // return BadRequest("No file uploaded."); // var ext = Path.GetExtension(file.FileName); // var fileName = $"{id}{ext}"; // var filePath = Path.Combine(_coverPath, fileName); // Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); // using var stream = System.IO.File.Create(filePath); // await file.CopyToAsync(stream); // // Optionally: update metadata to store file path (e.g., in a JSON file or DB) // return Ok(); //} [HttpPost("{id}/image")] public async Task UploadTourImage(Guid id, IFormFile file) { if (file == null || file.Length == 0) return BadRequest("No file uploaded."); var ext = Path.GetExtension(file.FileName); var fileName = $"{id}{ext}"; //var folderPath = Path.Combine("wwwroot", "uploads", "tours", id.ToString()); //Directory.CreateDirectory(folderPath); var filePath = Path.Combine(_coverPath, fileName); using var stream = System.IO.File.Create(filePath); await file.CopyToAsync(stream); // ✅ Update TourInfo JSON var jsonPath = GetPath(id); // Same as in your PUT method if (!System.IO.File.Exists(jsonPath)) return NotFound("TourInfo JSON not found."); var json = await System.IO.File.ReadAllTextAsync(jsonPath); var tour = JsonSerializer.Deserialize(json); // Relative path to serve the image (adjust if needed) var relativeImagePath = Path.Combine("uploads", "tourcovers", fileName).Replace("\\", "/"); tour!.CoverImageUrl = "/" + relativeImagePath; tour.Modified = DateTime.UtcNow; var updatedJson = JsonSerializer.Serialize(tour, new JsonSerializerOptions { WriteIndented = true }); await System.IO.File.WriteAllTextAsync(jsonPath, updatedJson); return Ok(); } private string GetPath(Guid id) => Path.Combine(_toursPath, $"{id}.json"); }