Ya perdí el hilo de los cambios pero ahi van.
This commit is contained in:
133
Backend/GestionIntegral.Api/Services/Radios/RitmoService.cs
Normal file
133
Backend/GestionIntegral.Api/Services/Radios/RitmoService.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using GestionIntegral.Api.Data.Repositories.Radios;
|
||||
using GestionIntegral.Api.Dtos.Radios;
|
||||
using GestionIntegral.Api.Models.Radios;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
// using GestionIntegral.Api.Data; // Para DbConnectionFactory si se usa transacción
|
||||
// using System.Data; // Para IsolationLevel si se usa transacción
|
||||
|
||||
namespace GestionIntegral.Api.Services.Radios
|
||||
{
|
||||
public class RitmoService : IRitmoService
|
||||
{
|
||||
private readonly IRitmoRepository _ritmoRepository;
|
||||
private readonly ILogger<RitmoService> _logger;
|
||||
// private readonly DbConnectionFactory _connectionFactory; // Si se implementa historial
|
||||
|
||||
public RitmoService(IRitmoRepository ritmoRepository, ILogger<RitmoService> logger /*, DbConnectionFactory cf */)
|
||||
{
|
||||
_ritmoRepository = ritmoRepository;
|
||||
_logger = logger;
|
||||
// _connectionFactory = cf;
|
||||
}
|
||||
|
||||
private RitmoDto MapToDto(Ritmo ritmo) => new RitmoDto
|
||||
{
|
||||
Id = ritmo.Id,
|
||||
NombreRitmo = ritmo.NombreRitmo
|
||||
};
|
||||
|
||||
public async Task<IEnumerable<RitmoDto>> ObtenerTodosAsync(string? nombreFilter)
|
||||
{
|
||||
var ritmos = await _ritmoRepository.GetAllAsync(nombreFilter);
|
||||
return ritmos.Select(MapToDto);
|
||||
}
|
||||
|
||||
public async Task<RitmoDto?> ObtenerPorIdAsync(int id)
|
||||
{
|
||||
var ritmo = await _ritmoRepository.GetByIdAsync(id);
|
||||
return ritmo == null ? null : MapToDto(ritmo);
|
||||
}
|
||||
|
||||
public async Task<(RitmoDto? Ritmo, string? Error)> CrearAsync(CreateRitmoDto createDto, int idUsuario)
|
||||
{
|
||||
if (await _ritmoRepository.ExistsByNameAsync(createDto.NombreRitmo))
|
||||
{
|
||||
return (null, "El nombre del ritmo ya existe.");
|
||||
}
|
||||
|
||||
var nuevoRitmo = new Ritmo { NombreRitmo = createDto.NombreRitmo };
|
||||
|
||||
// Sin historial, la transacción es opcional para una sola operación,
|
||||
// pero se deja la estructura por si se añade más lógica o historial.
|
||||
// using var connection = _connectionFactory.CreateConnection();
|
||||
// if (connection is System.Data.Common.DbConnection dbConn) await dbConn.OpenAsync(); else connection.Open();
|
||||
// using var transaction = connection.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var ritmoCreado = await _ritmoRepository.CreateAsync(nuevoRitmo /*, idUsuario, transaction */);
|
||||
if (ritmoCreado == null) return (null, "Error al crear el ritmo.");
|
||||
|
||||
// transaction.Commit();
|
||||
_logger.LogInformation("Ritmo ID {Id} creado por Usuario ID {UserId}.", ritmoCreado.Id, idUsuario);
|
||||
return (MapToDto(ritmoCreado), null);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// try { transaction.Rollback(); } catch {}
|
||||
_logger.LogError(ex, "Error CrearAsync Ritmo: {NombreRitmo}", createDto.NombreRitmo);
|
||||
return (null, $"Error interno: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool Exito, string? Error)> ActualizarAsync(int id, UpdateRitmoDto updateDto, int idUsuario)
|
||||
{
|
||||
var ritmoExistente = await _ritmoRepository.GetByIdAsync(id);
|
||||
if (ritmoExistente == null) return (false, "Ritmo no encontrado.");
|
||||
|
||||
if (ritmoExistente.NombreRitmo != updateDto.NombreRitmo && await _ritmoRepository.ExistsByNameAsync(updateDto.NombreRitmo, id))
|
||||
{
|
||||
return (false, "El nombre del ritmo ya existe para otro registro.");
|
||||
}
|
||||
|
||||
ritmoExistente.NombreRitmo = updateDto.NombreRitmo;
|
||||
|
||||
// Sin historial, la transacción es opcional...
|
||||
try
|
||||
{
|
||||
var actualizado = await _ritmoRepository.UpdateAsync(ritmoExistente /*, idUsuario, transaction */);
|
||||
if (!actualizado) return (false, "Error al actualizar el ritmo.");
|
||||
|
||||
// transaction.Commit();
|
||||
_logger.LogInformation("Ritmo ID {Id} actualizado por Usuario ID {UserId}.", id, idUsuario);
|
||||
return (true, null);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// try { transaction.Rollback(); } catch {}
|
||||
_logger.LogError(ex, "Error ActualizarAsync Ritmo ID: {Id}", id);
|
||||
return (false, $"Error interno: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool Exito, string? Error)> EliminarAsync(int id, int idUsuario)
|
||||
{
|
||||
var ritmoExistente = await _ritmoRepository.GetByIdAsync(id);
|
||||
if (ritmoExistente == null) return (false, "Ritmo no encontrado.");
|
||||
|
||||
if (await _ritmoRepository.IsInUseAsync(id))
|
||||
{
|
||||
return (false, "No se puede eliminar. El ritmo está asignado a una o más canciones.");
|
||||
}
|
||||
|
||||
// Sin historial, la transacción es opcional...
|
||||
try
|
||||
{
|
||||
var eliminado = await _ritmoRepository.DeleteAsync(id /*, idUsuario, transaction */);
|
||||
if (!eliminado) return (false, "Error al eliminar el ritmo.");
|
||||
|
||||
// transaction.Commit();
|
||||
_logger.LogInformation("Ritmo ID {Id} eliminado por Usuario ID {UserId}.", id, idUsuario);
|
||||
return (true, null);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// try { transaction.Rollback(); } catch {}
|
||||
_logger.LogError(ex, "Error EliminarAsync Ritmo ID: {Id}", id);
|
||||
return (false, $"Error interno: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user