diff --git a/packages/contracts/openapi.yaml b/packages/contracts/openapi.yaml index 189cc12..684c649 100644 --- a/packages/contracts/openapi.yaml +++ b/packages/contracts/openapi.yaml @@ -80,8 +80,32 @@ paths: type: integer format: int32 responses: - '200': - description: OK + '204': + description: No Content + '404': + description: Not Found + content: + text/plain: + schema: + type: string + application/json: + schema: + type: string + text/json: + schema: + type: string + '409': + description: Conflict + content: + text/plain: + schema: + $ref: '#/components/schemas/ConflictoDto' + application/json: + schema: + $ref: '#/components/schemas/ConflictoDto' + text/json: + schema: + $ref: '#/components/schemas/ConflictoDto' '/api/admin/escenas/{id}/imagen': post: tags: @@ -593,35 +617,42 @@ paths: text/json: schema: $ref: '#/components/schemas/PerfilDto' - /api/admin/pois: - post: + '/api/admin/pois/{id}': + get: tags: - AdminPois - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/CrearPoiRequest' - text/json: - schema: - $ref: '#/components/schemas/CrearPoiRequest' - application/*+json: - schema: - $ref: '#/components/schemas/CrearPoiRequest' + parameters: + - name: id + in: path + required: true + schema: + type: integer + format: int32 responses: '200': description: OK content: text/plain: schema: - $ref: '#/components/schemas/PoiDto' + $ref: '#/components/schemas/PoiDetalleAdminDto' application/json: schema: - $ref: '#/components/schemas/PoiDto' + $ref: '#/components/schemas/PoiDetalleAdminDto' text/json: schema: - $ref: '#/components/schemas/PoiDto' - '/api/admin/pois/{id}': + $ref: '#/components/schemas/PoiDetalleAdminDto' + '404': + description: Not Found + content: + text/plain: + schema: + type: string + application/json: + schema: + type: string + text/json: + schema: + type: string put: tags: - AdminPois @@ -669,6 +700,34 @@ paths: responses: '200': description: OK + /api/admin/pois: + post: + tags: + - AdminPois + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/CrearPoiRequest' + text/json: + schema: + $ref: '#/components/schemas/CrearPoiRequest' + application/*+json: + schema: + $ref: '#/components/schemas/CrearPoiRequest' + responses: + '200': + description: OK + content: + text/plain: + schema: + $ref: '#/components/schemas/PoiDto' + application/json: + schema: + $ref: '#/components/schemas/PoiDto' + text/json: + schema: + $ref: '#/components/schemas/PoiDto' /api/admin/rutas: post: tags: @@ -1118,6 +1177,16 @@ components: type: string nullable: true additionalProperties: false + ConflictoDto: + type: object + properties: + mensaje: + type: string + nullable: true + codigo: + type: string + nullable: true + additionalProperties: false ConteoTipoDto: type: object properties: @@ -1652,6 +1721,22 @@ components: $ref: '#/components/schemas/Medio' nullable: true additionalProperties: false + PoiDetalleAdminDto: + type: object + properties: + poi: + $ref: '#/components/schemas/PoiDto' + medios: + type: array + items: + $ref: '#/components/schemas/MedioDto' + nullable: true + escenas: + type: array + items: + $ref: '#/components/schemas/EscenaDto' + nullable: true + additionalProperties: false PoiDetalleDto: type: object properties: diff --git a/services/api/Controllers/AdminEscenasController.cs b/services/api/Controllers/AdminEscenasController.cs index c35bb33..0a68548 100644 --- a/services/api/Controllers/AdminEscenasController.cs +++ b/services/api/Controllers/AdminEscenasController.cs @@ -29,13 +29,26 @@ public async Task> ActualizarEscena(int id, [FromBody] A return Ok(escena); } + // Las respuestas se declaran para que el Swagger, y con él + // packages/contracts/openapi.yaml, las documente: sin esto dice «200 OK». [HttpDelete("{id:int}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] public async Task EliminarEscena(int id) { - var eliminado = await contenidoService.EliminarEscenaAsync(id); - if (!eliminado) return NotFound("La escena no existe."); + var resultado = await contenidoService.EliminarEscenaAsync(id); - return NoContent(); + return resultado switch + { + EliminarEscenaResultado.NoExiste => + NotFound("La escena no existe."), + EliminarEscenaResultado.EnUsoComoDestino => + Conflict(new ConflictoDto( + "No se puede eliminar la escena porque está enlazada como destino desde otra(s) escena(s). Elimina o reasigna esos hotspots primero.", + "escena_en_uso")), + _ => NoContent() + }; } /// diff --git a/services/api/Controllers/AdminPoisController.cs b/services/api/Controllers/AdminPoisController.cs index 162860c..8a644e0 100644 --- a/services/api/Controllers/AdminPoisController.cs +++ b/services/api/Controllers/AdminPoisController.cs @@ -9,6 +9,20 @@ namespace TourVirtualUTB.Api.Controllers; [RequiereClaveAdmin] public class AdminPoisController(IContenidoService contenidoService) : ControllerBase { + /// + /// Detalle de un POI (activo o inactivo) con medios y escenas. + /// + // Declarar el 404 hace que Swagger deje de deducir el 200: van los dos. + [HttpGet("{id:int}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task> ObtenerPoiAdmin(int id) + { + var detalle = await contenidoService.ObtenerPoiAdminPorIdAsync(id); + if (detalle is null) return NotFound("El punto de interés no existe."); + return Ok(detalle); + } + [HttpPost] public async Task> CrearPoi([FromBody] CrearPoiRequest request) { diff --git a/services/api/DTOs/ConflictoDto.cs b/services/api/DTOs/ConflictoDto.cs new file mode 100644 index 0000000..d49aa80 --- /dev/null +++ b/services/api/DTOs/ConflictoDto.cs @@ -0,0 +1,7 @@ +namespace TourVirtualUTB.Api.DTOs; + +/// +/// Cuerpo de un 409: por qué no se pudo hacer el cambio y un código estable +/// para que el panel lo reconozca sin leer el mensaje. +/// +public sealed record ConflictoDto(string Mensaje, string Codigo); diff --git a/services/api/DTOs/PoiDetalleAdminDto.cs b/services/api/DTOs/PoiDetalleAdminDto.cs new file mode 100644 index 0000000..0bdf9b2 --- /dev/null +++ b/services/api/DTOs/PoiDetalleAdminDto.cs @@ -0,0 +1,10 @@ +namespace TourVirtualUTB.Api.DTOs; + +/// +/// Detalle de un POI para el panel de administración: incluye inactivos, +/// medios y escenas asociadas. +/// +public sealed record PoiDetalleAdminDto( + PoiDto poi, + IReadOnlyList medios, + IReadOnlyList escenas); diff --git a/services/api/Services/ContenidoService.cs b/services/api/Services/ContenidoService.cs index 65dfe1e..8aa91f6 100644 --- a/services/api/Services/ContenidoService.cs +++ b/services/api/Services/ContenidoService.cs @@ -94,6 +94,50 @@ public async Task> ObtenerPoisAsync(int lugarId, Audiencia return new PoiDetalleDto(Dto.Poi(poi), medios.Select(Dto.Medio).ToList()); } + /// + /// Detalle para el panel: funciona con POIs activos e inactivos y + /// incluye las escenas del POI (además de los medios). + /// + public async Task ObtenerPoiAdminPorIdAsync(int poiId) + { + var poi = await db.Pois.AsNoTracking() + .Include(p => p.EscenaPrincipal) + .FirstOrDefaultAsync(p => p.Id == poiId); + + if (poi is null) return null; + + var medios = await db.Medios.AsNoTracking() + .Where(m => m.PoiId == poiId) + .OrderBy(m => m.Orden) + .ToListAsync(); + + var escenas = await db.Escenas.AsNoTracking() + .Where(e => e.PoiId == poiId) + .OrderBy(e => e.Orden) + .ToListAsync(); + + foreach (var medio in medios) + { + if (!string.IsNullOrEmpty(medio.Url)) + medio.Url = await almacenamiento.ObtenerUrlFirmadaAsync(medio.Url, TimeSpan.FromHours(2)); + if (!string.IsNullOrEmpty(medio.SubtitulosUrl)) + medio.SubtitulosUrl = await almacenamiento.ObtenerUrlFirmadaAsync(medio.SubtitulosUrl, TimeSpan.FromHours(2)); + } + + foreach (var escena in escenas) + { + if (!string.IsNullOrEmpty(escena.ImagenUrl)) + escena.ImagenUrl = await almacenamiento.ObtenerUrlFirmadaAsync(escena.ImagenUrl, TimeSpan.FromHours(2)); + if (!string.IsNullOrEmpty(escena.ImagenUrlBaja)) + escena.ImagenUrlBaja = await almacenamiento.ObtenerUrlFirmadaAsync(escena.ImagenUrlBaja, TimeSpan.FromHours(2)); + } + + return new PoiDetalleAdminDto( + Dto.Poi(poi), + medios.Select(Dto.Medio).ToList(), + escenas.Select(Dto.Escena).ToList()); + } + // Una escena sin panorámica no hace parada: la app trata como parada a todo // POI con escena_principal_id y ofrecería un 360 que no abre. Solo cambia la // respuesta (el POI viene sin seguimiento); en la base la escena sigue asignada. @@ -221,14 +265,19 @@ public async Task CrearEscenaAsync(CrearEscenaRequest request) return Dto.Escena(escena); } - public async Task EliminarEscenaAsync(int id) + public async Task EliminarEscenaAsync(int id) { var escena = await db.Escenas.FindAsync(id); - if (escena == null) return false; + if (escena == null) return EliminarEscenaResultado.NoExiste; + + // Hotspots de otras escenas que la usan como destino (OnDelete Restrict). + var referenciada = await db.Hotspots.AnyAsync(h => h.EscenaDestinoId == id); + if (referenciada) + return EliminarEscenaResultado.EnUsoComoDestino; db.Escenas.Remove(escena); await db.SaveChangesAsync(); - return true; + return EliminarEscenaResultado.Eliminada; } // --- MEDIOS (Multimedia) --- diff --git a/services/api/Services/EliminarEscenaResultado.cs b/services/api/Services/EliminarEscenaResultado.cs new file mode 100644 index 0000000..8ebf2e6 --- /dev/null +++ b/services/api/Services/EliminarEscenaResultado.cs @@ -0,0 +1,13 @@ +namespace TourVirtualUTB.Api.Services; + +/// Resultado de intentar borrar una escena. +public enum EliminarEscenaResultado +{ + Eliminada, + NoExiste, + /// + /// Hay hotspots de otras escenas que apuntan a esta como destino + /// (FK Restrict en EscenaDestinoId). + /// + EnUsoComoDestino +} diff --git a/services/api/Services/IContenidoService.cs b/services/api/Services/IContenidoService.cs index 0f5a81d..ae1c850 100644 --- a/services/api/Services/IContenidoService.cs +++ b/services/api/Services/IContenidoService.cs @@ -8,6 +8,8 @@ public interface IContenidoService Task> ObtenerPoisAsync(int lugarId, Audiencia audiencia); Task ObtenerRecorridoAsync(int lugarId, Audiencia audiencia); Task ObtenerPoiPorIdAsync(int poiId, Audiencia audiencia); + /// Detalle admin: POI activo o inactivo, con medios y escenas. + Task ObtenerPoiAdminPorIdAsync(int poiId); Task ObtenerRutaActivaAsync(int lugarId, Audiencia audiencia); Task ObtenerRutaMapaLugarAsync(int lugarId); Task ActualizarMapaLugarAsync(int lugarId, string rutaS3); @@ -22,7 +24,7 @@ public interface IContenidoService // Escenas Task CrearEscenaAsync(CrearEscenaRequest request); Task ActualizarEscenaAsync(int id, ActualizarEscenaRequest request); - Task EliminarEscenaAsync(int id); + Task EliminarEscenaAsync(int id); // Hotspots Task CrearHotspotAsync(CrearHotspotRequest request);