using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RobotNet10.RobotApp.Client.Shared.SLAM;
using RobotNet10.RobotApp.Shared;
using RobotNet10.RobotApp.SLAM;
namespace RobotNet10.RobotApp.Controllers;
///
/// API Controller for map management operations
///
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class MapsController(ISLAMService slamService, ILogger logger) : ControllerBase
{
///
/// Get map info by name
///
/// Name of the map
/// MapInfoDto if found, NotFound otherwise
[HttpGet("{mapName}")]
public ActionResult GetMapInfo(string mapName)
{
try
{
var mapInfo = slamService.GetMapInfo(mapName);
if (mapInfo == null)
{
logger.LogWarning("MapsController: Map not found: {MapName}", mapName);
return NotFound($"Map '{mapName}' not found");
}
return Ok(MapMapInfoToDto(mapInfo));
}
catch (Exception ex)
{
logger.LogError(ex, "MapsController: Failed to get map info: {MapName}", mapName);
return StatusCode(500, "Failed to get map info");
}
}
///
/// Get map image by name
///
/// Name of the map
/// PNG image file
[HttpGet("{mapName}/image")]
public IActionResult GetMapImage(string mapName)
{
try
{
var imagePath = slamService.GetMapImagePath(mapName);
if (imagePath == null)
{
logger.LogWarning("MapsController: Map image not found: {MapName}", mapName);
return NotFound($"Map image for '{mapName}' not found");
}
var contentType = imagePath.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
? "image/png"
: "image/jpeg";
var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
return File(fileStream, contentType);
}
catch (Exception ex)
{
logger.LogError(ex, "MapsController: Failed to get map image: {MapName}", mapName);
return StatusCode(500, "Failed to get map image");
}
}
private static MapInfoDto MapMapInfoToDto(MapInfo mapInfo)
{
return new MapInfoDto
{
Name = mapInfo.Name,
CreatedDate = mapInfo.CreatedDate,
Resolution = mapInfo.Resolution,
Width = mapInfo.Size.Width,
Height = mapInfo.Size.Height,
TrajectoryNodeCount = mapInfo.TrajectoryNodeCount,
OriginX = mapInfo.Origin.Position.X,
OriginY = mapInfo.Origin.Position.Y,
};
}
}